Skip to content

fix: keep the websocket client attached to a living AuthorizedApiContext - #207

Merged
thorbenziemek merged 1 commit into
mainfrom
fix/ws-client-destroyed-api-context
Nov 10, 2026
Merged

thorbenziemek merged 1 commit into
mainfrom
fix/ws-client-destroyed-api-context

Conversation

@thorbenziemek

Copy link
Copy Markdown
Contributor

Fixes #206.

The problem

AuthorizedApiContext attached itself to the websocket client in its constructor, unconditionally. A child context is constructed with its parent's shared _wsClient, so every createChildContext() quietly took the binding over — and children are commonly created per request and destroyed right after. The client was therefore left holding a destroyed context.

That context has exactly one job: refetching the status of a decoupled operation after an interrupted connection. Since 1.12.0 a request on a destroyed context rejects with BrokerizeAbortError, and the rejection is only logged, so the symptom was silent — after a reconnect during a decoupled authorization, no further status updates arrived.

The fix

The binding now follows the lifetime of the client rather than the construction order:

  • the first context to see the client attaches and keeps it, so the context that created it wins and children leave it alone;
  • a context detaches itself in destroy() if it is the attached one, so a later context can take over instead of a dead reference being left behind.

_setAuthorizedApiContext accepts null for that, and _getAuthorizedApiContext was added as its counterpart. Both are internal (underscore-prefixed) and do not show up in the API report. A websocket client that does not implement them — an application may pass its own — is handled as before.

Tests

test/authorizedApiContext.test.js covers the child-context case, the handover after a destroy, and a client without the binding methods. Verified that the first two fail on main and pass with this change.

🤖 Generated with Claude Code

The websocket client holds one context to refetch a decoupled operation status
with after an interrupted connection. Every context attached itself in its
constructor, so each `createChildContext()` took the binding over - and a child
is typically created per request and destroyed right after. The client was
therefore left pointing at a destroyed context, whose requests reject with
`BrokerizeAbortError` since 1.12.0, which only ever got logged: after a
reconnect during a decoupled authorization, no further status updates arrived.

Now the first context attaches and keeps the client, so children leave it alone,
and a context detaches itself when it is destroyed instead of leaving a dead
reference behind.

Fixes #206

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@thorbenziemek
thorbenziemek merged commit b939e87 into main Nov 10, 2026
2 checks passed
@thorbenziemek
thorbenziemek deleted the fix/ws-client-destroyed-api-context branch November 10, 2026 12:16
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.

Websocket client holds a destroyed AuthorizedApiContext after createChildContext()

1 participant