Acquire locks while cleaning up stale ones

This fixes #328