Open ojasjoshi opened 4 years ago
A potential fix here would be to add a mutex lock guard within EventT::Connect to make it thread-safe.
Thank you for the ticket. I think the suggestion of adding a mutex to protect the connections
map sounds reasonable.
The performance lag occuring from mutex locking EventT::Connect should not be significant
If we're very concerned about this, we could use the profiler recently added to compare some common scenario before and after adding the mutex.
Stack trace:
A call to
EventT::Connect
from any subscriber creates anew EventConnection(true, _subscriber)
between the event and the subscriber. A call toEvent::Signal
duringworldUpdateBeginLoop
then executes callbacks to all theEventConnection
's added to this event.A race condition occurs between
EventT::Signal
andEventT::Connect
when the newEventConnection
is not initialized withinEventT::Connect
but can be referenced byEventT::Signal
viathis->connections
map, leading to a segfault.Reference from
EventT::Connect
A potential fix here would be to add a mutex lock guard within
EventT::Connect
to make it thread-safe. Such a mutex (std::lock_guard<std::mutex> lock(this->mutex)
) already exists forEventT::CleanUp
but is missing in all otherEventT
methods. The performance lag occuring from mutex lockingEventT::Connect
should not be significant considering that a call toConnect
occurs at-most once for every subscriber and also considering the design thought behind adding a mutex lock toCleanUp
(a call toCleanUp
is made during everySignal
loop, which should be significantly more than calls toConnect
).