Skip to content

Fix memory leaks and improve error handling#3

Open
hobostay wants to merge 1 commit intokessler:mainfrom
hobostay:fix/memory-leaks-and-error-handling
Open

Fix memory leaks and improve error handling#3
hobostay wants to merge 1 commit intokessler:mainfrom
hobostay:fix/memory-leaks-and-error-handling

Conversation

@hobostay
Copy link
Copy Markdown

@hobostay hobostay commented Apr 8, 2026

Summary

This PR addresses several reliability issues found in the codebase:

1. Memory leak in pendingToolResults Map

Problem: Tool execution promises had no timeout mechanism. If a tool execution failed or hung, the promise's resolve function would remain in the Map forever, causing a memory leak.

Fix:

  • Added 30-second timeout for tool execution
  • Map now stores both resolve function and timeout ID
  • Timeout properly cleans up the entry and rejects the promise
  • Tool execution errors are caught and returned as proper error responses

2. Race condition in offscreen document creation

Problem: If creating the offscreen document failed in background/offscreen-manager.ts, the creating Promise was never reset, causing subsequent calls to hang indefinitely.

Fix: Wrapped the await in try-finally to ensure the creating flag is always reset, even if creation fails.

3. Intl.DisplayNames error handling

Problem: The getCountry() function in entrypoints/offscreen/main.ts didn't handle cases where Intl.DisplayNames.of() returns undefined or throws an exception for invalid region codes.

Fix: Added try-catch block and proper null/undefined handling.

Testing

  • Verified build succeeds: pnpm build completes successfully
  • No TypeScript errors introduced
  • Changes are minimal and focused on reliability improvements

🤖 Generated with Claude Code

This commit addresses several reliability issues:

1. **Memory leak in pendingToolResults Map**: Tool execution promises
   now have a 30-second timeout and proper cleanup. Previously, if a
   tool execution failed or hung, the promise resolver would remain
   in the Map forever.

2. **Race condition in offscreen document creation**: If creating the
   offscreen document fails, the creating flag is now properly reset
   in a finally block, allowing subsequent calls to retry.

3. **Intl.DisplayNames error handling**: The getCountry() function now
   handles cases where Intl.DisplayNames.of() returns undefined or
   throws an exception.

4. **Improved tool execution error handling**: Tool execution errors
   are now caught and returned as proper error responses instead of
   causing unhandled promise rejections.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

1 participant