core: fix a race condition and a deadlock