Fixed race condition in unregistering a handler