Fixes rare crash in multi-threaded environments