hfm: lock hive.contetxs when hived removes events
There was a race condition between attaching a context and removing events by the hived:
-
during attaching a context it's event id is changed from unreachabe_event_id to 0, to block removing events before a new event id will be found for a context. Immediatly after attach find next event is executed, and some event id is found
-
in the meantime hived may remove uneccessary events based on context id, which at the beginning was ureachable_event_id, so all the events will be removed
Finally context got an event id which is being removed by hived, and in consequence will violent FK between contexts and events.
The solution is make a lock between attaching a context and removing events.