Fix double unsubscribe removing other bus handlers - #784
Open
keshavojha11 wants to merge 1 commit into
Open
Conversation
InMemoryBus.subscribe returned an unsubscribe closure that ran splice(indexOf(handler), 1) without guarding against indexOf === -1. Calling unsubscribe twice (or after the handler was already removed) made indexOf return -1, so splice(-1, 1) removed the last remaining handler instead. Guard the index before splicing. Adds a focused vitest regression test.
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.
Summary
InMemoryBus.subscribe()returned an unsubscribe closure that removed the handler with an unguardedsplice:When the handler was no longer present — e.g.
unsubscribe()called twice —indexOfreturned-1, sosplice(-1, 1)removed the last subscriber for thatrunIdinstead. That silently drops a live, unrelated handler, which stops receiving run events.The fix guards the index before splicing, matching the pattern already used by the other buses in this package (e.g.
background-tasks/bus.ts):Fixes #491.
Testing
Added a focused regression test at
apps/x/packages/core/src/application/lib/bus.test.ts:runId.Behavior:
unsubscribe()call runssplice(-1, 1)and removes B, so B receives nothing (AssertionError: expected [] to have a length of 1 but got +0).Commands run:
vitest run src/application/lib/bus.test.ts→ 1 passed (was 1 failed before the fix)pnpm typecheck→ clean, exit 0Scope
apps/x/packages/core/src/application/lib/bus.tsand its new test file are touched.apps/cli/src/application/lib/bus.ts(CLI Bus.unsubscribe has same splice(-1) bug as desktop app #492) was intentionally left untouched — not in scope for this PR.