fix: guard against null socket/encryption — 'undefined is not a function'#907
Open
chris-yyau wants to merge 1 commit intoslopus:mainfrom
Open
fix: guard against null socket/encryption — 'undefined is not a function'#907chris-yyau wants to merge 1 commit intoslopus:mainfrom
chris-yyau wants to merge 1 commit intoslopus:mainfrom
Conversation
…t a function" Three fixes for intermittent "undefined is not a function" crashes: 1. **apiSocket.ts**: Replace non-null assertions (`this.socket!`, `this.encryption!`) with explicit null checks using local variable capture to prevent TOCTOU races across `await` boundaries. Throws descriptive errors instead of crashing. 2. **happyMcpStdioBridge.ts**: Add promise lock to `ensureHttpClient()` with `try/finally` cleanup to prevent race conditions and stale rejected promises. 3. **startHappyServer.ts**: Add missing error handler on HTTP server binding. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
There was a problem hiding this comment.
Pull request overview
This PR hardens the Happy app/CLI against intermittent crashes and hangs caused by stale/null references during disconnects and by concurrent initialization paths.
Changes:
- Add explicit null guards and local socket capture in
ApiSocketRPC/send paths to prevent “undefined is not a function” after disconnects. - Serialize MCP HTTP client creation in the STDIO bridge using a shared
connectPromiseto prevent duplicate clients under concurrent tool calls. - Reject
startHappyServer()startup on servererrorevents so port/bind failures don’t hang silently.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| packages/happy-cli/src/codex/happyMcpStdioBridge.ts | Adds a promise-based lock to prevent concurrent MCP client creation. |
| packages/happy-cli/src/claude/utils/startHappyServer.ts | Adds an error listener to reject server startup on bind/listen failures. |
| packages/happy-app/sources/sync/apiSocket.ts | Replaces non-null assertions with runtime guards and safer socket usage across await. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
4 tasks
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
Fixes intermittent "undefined is not a function" crashes caused by calling methods on null socket/encryption references after WebSocket disconnects.
apiSocket.ts: Replacethis.socket!/this.encryption!non-null assertions with explicit null guards using local variable capture to prevent TOCTOU races acrossawaityield pointshappyMcpStdioBridge.ts: Add promise lock withtry/finallyto prevent concurrentensureHttpClient()calls from creating duplicate MCP clientsstartHappyServer.ts: Addserver.on('error')handler with logging so port conflicts reject instead of hangingTest plan
change_titleMCP tool works normallychange_titlecalls don't create duplicate clients🤖 Generated with Claude Code via Happy