Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

MQ37
Copy link

@MQ37 MQ37 commented Sep 11, 2025

A critical production bug was identified in the Apify MCP Server causing server crashes due to the ERR_STREAM_WRITE_AFTER_END error. This error occurred when multiple concurrent HTTP requests with the same ID were processed simultaneously, leading to race conditions in the StreamableHTTPServerTransport.

Motivation and Context

To prevent the ERR_STREAM_WRITE_AFTER_END from crashing the node process.

How Has This Been Tested?

Manually replicated using script that triggered the crash, fixed in the SDK and then verified that the script no longer crashes the server.

Breaking Changes

No.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

This error was almost always triggered by the Claude.ai MCP connectors and crashed the server:

Sep 10 02:47:49 apify-mcp-server-56dd9854bf-cxxc4 apify-mcp-server INFO {"time":"2025-09-10T00:47:49.054Z","level":"INFO","msg":"MCP API","mth":"POST","rt":"/","tr":"HTTP","ip":"xxx","userAgent":"Claude-User","body":"{\"method\":\"initialize\",\"params\":{\"protocolVersion\":\"2025-06-18\",\"capabilities\":{},\"clientInfo\":{\"name\":\"Anthropic/ClaudeAI\",\"version\":\"1.0.0\"}},\"jsonrpc\":\"2.0\",\"id\":0}"}
Sep 10 02:47:49 apify-mcp-server-56dd9854bf-cxxc4 apify-mcp-server INFO {"time":"2025-09-10T00:47:49.154Z","level":"INFO","msg":"Session initialized","sessionId":"a14f0708-fcfe-4788-9afb-78b5cca580d8","clientsCount":178,"tr":"HTTP"}
Sep 10 02:47:49 apify-mcp-server-56dd9854bf-cxxc4 apify-mcp-server INFO {"time":"2025-09-10T00:47:49.248Z","level":"INFO","msg":"MCP API","mth":"POST","rt":"/","tr":"HTTP","ip":"xxx","userAgent":"Claude-User","body":"{\"method\":\"notifications/initialized\",\"jsonrpc\":\"2.0\"}"}
Sep 10 02:47:49 apify-mcp-server-56dd9854bf-cxxc4 apify-mcp-server INFO {"time":"2025-09-10T00:47:49.330Z","level":"INFO","msg":"MCP API","mth":"POST","rt":"/","tr":"HTTP","ip":"xxx","userAgent":"Claude-User","body":"{\"method\":\"prompts/list\",\"jsonrpc\":\"2.0\",\"id\":1}"}
Sep 10 02:47:49 apify-mcp-server-56dd9854bf-cxxc4 apify-mcp-server INFO {"time":"2025-09-10T00:47:49.331Z","level":"INFO","msg":"MCP API","mth":"POST","rt":"/","tr":"HTTP","ip":"xxx","userAgent":"Claude-User","body":"{\"method\":\"tools/list\",\"jsonrpc\":\"2.0\",\"id\":3}"}
Sep 10 02:47:49 apify-mcp-server-56dd9854bf-cxxc4 apify-mcp-server INFO {"time":"2025-09-10T00:47:49.332Z","level":"INFO","msg":"MCP API","mth":"POST","rt":"/","tr":"HTTP","ip":"xxx","userAgent":"Claude-User","body":"{\"method\":\"prompts/list\",\"jsonrpc\":\"2.0\",\"id\":1}"}
Wednesday, Sep 10th 2025, 2:47am
Sep 10 02:47:49 apify-mcp-server-56dd9854bf-cxxc4 apify-mcp-server err node:events:502
      throw er; // Unhandled 'error' event
      ^
Sep 10 02:47:49 apify-mcp-server-56dd9854bf-cxxc4 apify-mcp-server Error Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at write_ (node:_http_outgoing:955:11)
    at ServerResponse.write (node:_http_outgoing:904:15)
    at StreamableHTTPServerTransport.writeSSEEvent (file:///home/node/node_modules/@modelcontextprotocol/sdk/dist/esm/server/streamableHttp.js:238:20)
    at StreamableHTTPServerTransport.send (file:///home/node/node_modules/@modelcontextprotocol/sdk/dist/esm/server/streamableHttp.js:565:22)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
Sep 10 02:47:49 apify-mcp-server-56dd9854bf-cxxc4 apify-mcp-server err Emitted 'error' event on ServerResponse instance at:
    at emitErrorNt (node:_http_outgoing:927:9)
    at process.processTicksAndRejections (node:internal/process/task_queues:91:21) {
  code: 'ERR_STREAM_WRITE_AFTER_END'
}
Sep 10 02:47:49 apify-mcp-server-56dd9854bf-cxxc4 apify-mcp-server Node.js v22.13.1

After patch is applied the server prints errors and continues to run:

DEBUG Processing request {"sessionId":"03190050-dd9e-44b6-802c-6bbbce839904","tr":"HTTP","body":"{\"jsonrpc\":\"2.0\",\"id\":9,\"method\":\"prompts/list\"}"}
[MCP Error] Error: Failed to send response: Error: No connection established for request ID: 2
    at <anonymous> (/home/mq/workdir/apify/mcp.apify.com/typescript-sdk/src/shared/protocol.ts:444:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
[MCP Error] Error: Failed to send response: Error: No connection established for request ID: 3
    at <anonymous> (/home/mq/workdir/apify/mcp.apify.com/typescript-sdk/src/shared/protocol.ts:444:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
[MCP Error] Error: Failed to send response: Error: No connection established for request ID: 4
    at <anonymous> (/home/mq/workdir/apify/mcp.apify.com/typescript-sdk/src/shared/protocol.ts:444:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
[MCP Error] Error: Failed to send response: Error: No connection established for request ID: 5
    at <anonymous> (/home/mq/workdir/apify/mcp.apify.com/typescript-sdk/src/shared/protocol.ts:444:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
[MCP Error] Error: Failed to send response: Error: No connection established for request ID: 7
    at <anonymous> (/home/mq/workdir/apify/mcp.apify.com/typescript-sdk/src/shared/protocol.ts:444:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
[MCP Error] Error: Failed to send response: Error: No connection established for request ID: 8
    at <anonymous> (/home/mq/workdir/apify/mcp.apify.com/typescript-sdk/src/shared/protocol.ts:444:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)

@MQ37 MQ37 requested a review from a team as a code owner September 11, 2025 08:43
@MQ37 MQ37 requested a review from dsp-ant September 11, 2025 08:43
@MQ37 MQ37 changed the title fix: prevent streamable http wite after end crashing the node process fix: prevent streamable http wite after end from crashing the node process Sep 12, 2025
@ochafik ochafik self-requested a review September 15, 2025 11:46
Copy link
Contributor

@ochafik ochafik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @MQ37, thanks for sending this! Left some comments :-)

@MQ37
Copy link
Author

MQ37 commented Sep 15, 2025

Hi @MQ37, thanks for sending this! Left some comments :-)

Thank you for review 👍 I simplified the fix and the error handler only is sufficient.

@MQ37 MQ37 requested a review from ochafik September 16, 2025 15:17
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.

2 participants