Skip to content

fix: guard indexOf result in Bus.unsubscribe to prevent splice(-1)#526

Open
deepak0x wants to merge 1 commit intorowboatlabs:mainfrom
deepak0x:fix/492-bus-unsubscribe-splice-guard
Open

fix: guard indexOf result in Bus.unsubscribe to prevent splice(-1)#526
deepak0x wants to merge 1 commit intorowboatlabs:mainfrom
deepak0x:fix/492-bus-unsubscribe-splice-guard

Conversation

@deepak0x
Copy link
Copy Markdown

Closes #492

Problem

Bus.unsubscribe() called splice(indexOf(handler), 1) without checking the result of indexOf. When the same unsubscribe function was called twice, the second call found no match and returned -1. splice(-1, 1) silently removed the last subscriber in the array — the wrong handler.

Fix

Store the indexOf result and only call splice when idx !== -1, making a double-unsubscribe a safe no-op.

Files changed

  • apps/cli/src/application/lib/bus.ts
  • apps/x/packages/core/src/application/lib/bus.ts

Closes rowboatlabs#492

When unsubscribe() was called twice for the same handler, indexOf()
returned -1 on the second call. splice(-1, 1) silently removed the
last subscriber in the array — the wrong handler.

Fix: store the indexOf result and only call splice when idx !== -1,
making double-unsubscribe a safe no-op.

Affected files:
  apps/cli/src/application/lib/bus.ts
  apps/x/packages/core/src/application/lib/bus.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI Bus.unsubscribe has same splice(-1) bug as desktop app

1 participant