Implement race-resistant stale lock detection

Fix #4