Fix races

This fixes several races found by running the race detector on CI.
Down on line 58 there's another lock/unlock pair flanking a delete.  I don't think there's any way that delete can blow up, but reading a lock/unlock that doesn't use the defer pattern made me blink.  Should we use defers out of convention, is this not a worry?