core: clean up the lock cleanup code, tests