allow event listeners to be nested #1513
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What kind of change does this PR introduce? (check at least one)
Does this PR introduce a breaking change? (check one)
The PR fulfills these requirements:
fix: #xxx[,#xxx]
, where "xxx" is the issue number)If adding a new feature, the PR's description includes:
Other information:
This builds upon #1446 and #1506. The core issue is that event handler closures are ran inside of
Listeners::trigger
while it has a lock on the handlers. There are 4 exposed event functions thatMutex::lock()
the listeners:Listeners::listen
,Listeners::once
,Listeners::unlisten
, andListeners::trigger
. If any of those 4 are called from inside the event handler closure, then a deadlock will occur because the closure runner (Listeners::trigger
) already locked it.I saw a few non-breaking solutions to this:
Listeners::trigger
, after getting them from the lock, and then use their closures.once
deadlock #1446, spawn an async task to wait for the lock later. This downside is that when that will be executed is unknown, and can cause things likeListeners::once
being able to be called multiple times if called soon enough.Mutex::try_lock()
locking inside those mentioned 4 functions, adding to a pending queue if the handlers are currently already locked.Listeners::trigger
, has been modified so that if any event handler closure was executed, all pending events will be flushed in the order they were added.A side-effect of the chosen solution is that nested event calls technically don't happen during the closure - but after it. This is something we should document so that users expect this behavior. In all cases I can think of right now, this side-effect doesn't matter much since the execution order of different closures was already non-deterministic (another thing we should document).
fix/nested-events:
dev:
[deadlocks]