fix: make disconnect() have stable reference to prevent unexpected cleanup#21
Conversation
…eanup The disconnect useCallback had onError in its dependency array, which caused it to get a new identity whenever onError changed. This was problematic when users had disconnect in a useEffect dependency array with inline callbacks: 1. Component renders with inline onError callback 2. disconnect gets new identity (onError changed) 3. useEffect cleanup runs, calling disconnect() 4. clientRef.current becomes null 5. Subsequent mute()/unmute()/disconnect() calls fail silently The fix removes onError from disconnect's dependencies since the error is thrown and can be caught by the caller. Also adds helpful warning logs when functions are called without an active client. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2085a4c616
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| await client.disconnect(); | ||
| } catch (error) { | ||
| console.error('Failed to disconnect from agent:', error); | ||
| onError?.(error as Error); | ||
| throw error; |
There was a problem hiding this comment.
Call onError when disconnect fails
In disconnect’s catch block the error is only logged and rethrown, whereas previously onError was invoked. Callers that rely on onError for user-visible error handling (and often call disconnect() from a useEffect cleanup without awaiting it) will now miss disconnect failures entirely, which is a behavioral regression from the previous API contract.
Useful? React with 👍 / 👎.
Summary
mute(),unmute(), anddisconnect()functions wouldn't update reactive statedisconnecthavingonErrorin its dependency array, causing it to get a new identity when users passed inline callbacksdisconnectwas used in auseEffectdependency array, this triggered unexpected cleanup calls that nullifiedclientRef.currentTest plan
isMutedstateFixes the bug reported where calling
mute(),unmute(), ordisconnect()would execute without errors but not update the corresponding reactive state values.🤖 Generated with Claude Code