trying to debug the race condition
added locking which fixes bugs

i really kind of don't understand why we need locking here as we only
read and check the lock which should prevent all writing