fix(bus): guard indexOf in InMemoryBus unsubscribe to prevent splice(-1)#605
Open
serhiizghama wants to merge 2 commits into
Open
fix(bus): guard indexOf in InMemoryBus unsubscribe to prevent splice(-1)#605serhiizghama wants to merge 2 commits into
serhiizghama wants to merge 2 commits into
Conversation
The unsubscribe closure called splice(indexOf(handler), 1) without checking for -1. A double-unsubscribe (or unsubscribing after the runId entry was cleared) made indexOf return -1, so splice(-1, 1) silently removed the last remaining handler instead of doing nothing. Guard against a missing runId entry and a -1 index so a repeated unsubscribe is a no-op, matching the pattern already used by the other buses in the package.
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.
Problem
Closes #491.
The desktop app's
InMemoryBus(apps/x/packages/core/src/application/lib/bus.ts) builds its unsubscribe closure as:indexOfis not checked for-1. Ifunsubscribe()is called twice (or after the handler is already gone),indexOfreturns-1andsplice(-1, 1)silently removes the last handler in the array — a different, still-active subscriber.Reproduction:
aandbto the samerunId.a's unsubscribe.a's unsubscribe again.bis now gone, so it no longer receives published events.Solution
Guard both the missing-
runIdentry and the-1index so a repeated unsubscribe is a true no-op:This matches the pattern already used by the other buses in the same package (
background-tasks/bus.ts,knowledge/live-note/bus.ts).Note: the CLI bus has an identical bug tracked separately in #492 — this PR only touches the desktop app bus for #491.
Testing
Added
bus.test.tscovering event delivery, single unsubscribe, and the double-unsubscribe no-op regression.npm run build(tsc) passes.