Fixed potential deadlock on termination