Fix race condition in the event implementation