fixed a few race conditions during thread/process termination leading to dead-locks