Fix trashing of the masking state in STM (#5238)