Fixes possible deadlock in threads.