Fix race in cleanupthread

Merged Jørgen Lind requested to merge feature/jorgen.lind/cleanupthread_deadlock_fix into master

Merge request reports