gc: run pre-detach operations under lock