Skip to content

fix: Add retry logic for Bun socket connection errors#110

Merged
konard merged 6 commits intomainfrom
issue-109-1e6d7f1b4e4d
Jan 10, 2026
Merged

fix: Add retry logic for Bun socket connection errors#110
konard merged 6 commits intomainfrom
issue-109-1e6d7f1b4e4d

Conversation

@konard
Copy link
Copy Markdown
Contributor

@konard konard commented Jan 10, 2026

Summary

Adds automatic retry logic for socket connection errors that occur due to Bun's known 10-second idle timeout issue in Bun.serve() contexts.

Problem

When using the agent CLI with streaming API providers (e.g., opencode/grok-code), connections frequently fail after approximately 10-12 seconds with:

Error: The socket connection was closed unexpectedly. For more information, pass `verbose: true` in the second argument to fetch()

This is a known Bun limitation (oven-sh/bun#14439) where the default idleTimeout of 10 seconds causes long-running streaming connections to be dropped.

Solution

  1. New Error Type: Added SocketConnectionError in message-v2.ts that is always marked as retryable
  2. Error Detection: Updated fromError() to detect socket errors by message pattern
  3. Retry Logic: Enhanced processor.ts to retry socket errors up to 3 times
  4. Backoff Strategy: Uses exponential backoff (1s → 2s → 4s) specific to socket errors

Changes

  • js/src/session/message-v2.ts - Add SocketConnectionError type and detection logic
  • js/src/session/processor.ts - Add retry handling for socket connection errors
  • js/src/session/retry.ts - Add socket-specific retry configuration and delay function
  • js/tests/socket-retry.test.js - Unit tests for socket error detection and retry logic
  • docs/case-studies/issue-109/ - Case study documentation with timeline and analysis
  • js/experiments/socket-error-simulation.ts - Experiment script to verify fix behavior

Test Plan

  • Unit tests pass (bun test tests/socket-retry.test.js)
  • Lint checks pass (bun run lint)
  • Format checks pass (bun run format:check)
  • Experiment script validates error detection and retry delays

References

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #109
@konard konard self-assigned this Jan 10, 2026
Addresses issue #109 where streaming API responses fail after ~10 seconds
due to Bun's default idle timeout in Bun.serve() context.

Changes:
- Add SocketConnectionError type in message-v2.ts with isRetryable: true
- Detect socket errors by message pattern ("socket connection was closed")
- Update processor.ts to retry socket errors up to 3 times
- Add socket-specific retry configuration in retry.ts with exponential backoff

The fix follows the existing retry pattern for APIError but with socket-specific
timing (1s, 2s, 4s delays) to handle transient Bun socket timeouts.

References:
- oven-sh/bun#14439 (Bun 10s idle timeout)
- link-assistant/hive-mind#1098 (original report)
Adds comprehensive tests for:
- Socket error detection from error messages
- SocketConnectionError type properties
- Retry configuration constants
- Socket error delay calculation with exponential backoff
Documents the socket connection error investigation:
- Timeline of events
- Root cause analysis (Bun 10s idle timeout)
- Solution implemented
- References to related issues

Data files:
- README.md with comprehensive case study
- execution-log.json with detailed event timeline
Adds experiments/socket-error-simulation.ts to verify:
- Socket error detection works correctly
- Retry configuration values
- Exponential backoff delay calculation
- Error type differentiation

Useful for testing and demonstrating the fix behavior.
@konard konard changed the title [WIP] Socket connection closed unexpectedly during streaming API responses with Bun fix: Add retry logic for Bun socket connection errors Jan 10, 2026
@konard konard marked this pull request as ready for review January 10, 2026 22:24
@konard
Copy link
Copy Markdown
Contributor Author

konard commented Jan 10, 2026

🤖 Solution Draft Log

This log file contains the complete execution trace of the AI solution draft process.

💰 Cost estimation:

  • Public pricing estimate: $5.560882 USD
  • Calculated by Anthropic: $3.892794 USD
  • Difference: $-1.668088 (-30.00%)
    📎 Log file uploaded as GitHub Gist (1041KB)
    🔗 View complete solution draft log

Now working session is ended, feel free to review and add any feedback on the solution draft.

@konard konard merged commit 4f7a0d4 into main Jan 10, 2026
8 checks passed
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.

Socket connection closed unexpectedly during streaming API responses with Bun

1 participant