fix: defer re-entrant publishes of the same event - #9840
Draft
GuillaumeRx wants to merge 1 commit into
Draft
Conversation
GuillaumeRx
force-pushed
the
gr/messenger-defer-reentrant-publish
branch
3 times, most recently
from
August 12, 2026 12:20
e18db6f to
b2a6b5e
Compare
`Messenger` delivered a re-entrant publish of the same event inline, so when a subscriber published the event it was handling (directly, or indirectly through a delegated messenger), the in-progress publish would resume afterwards and re-deliver its now-stale payload to the subscribers it had not yet reached. A subscriber such as a UI store bridging controller state could therefore end up observing an older payload after a newer one. Defer a publish of an event that is already being published on the same messenger, then drain the deferred publishes in order once the in-progress publish finishes. Re-entrant publishes of a different event still run inline.
GuillaumeRx
force-pushed
the
gr/messenger-defer-reentrant-publish
branch
from
August 12, 2026 12:30
b2a6b5e to
2ab366a
Compare
Gudahtt
reviewed
Aug 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Explanation
Messengerdelivered a re-entrant publish of the same event inline. When a subscriber published the event it was currently handling — directly, or indirectly through a delegated messenger (for example astateChangesubscriber that calls an action which triggersupdate()on the same controller) — the nested publish ran to completion, and then the in-progress publish resumed its subscriber loop and re-delivered its now-stale payload to the subscribers it had not yet reached. A downstream subscriber such as a UI store bridging controller state could therefore end up observing an older payload after a newer one.This surfaced in the extension as a network filter that stayed on a just-added network even though the enabled-network map had already been restored: the restore's
stateChangere-enteredpublish, and the stale "switched" payload was the last thing the UI received.This change makes
#publishdefer a publish of an event that is already being published on the same messenger, then drain the deferred publishes in order once the in-progress publish finishes. Because the event stays marked as "publishing" while draining, further re-entrant publishes of that event are deferred and drained too. Re-entrant publishes of a different event still run inline. The fix covers delegated messengers as well, since delegated publishes funnel through the same#publish.Note this is a small behavioral change: a re-entrant publish of the same event is now delivered after the current publish completes (still synchronously, within the same tick) rather than nested inside it.
References
addNetworktoLegacyBackgroundApiServicemetamask-extension#44142, where this was worked around at the call site (restoring the map outside thestateChangepublish viaawait).Checklist