Skip to content

Conversation

@ArtemYurov
Copy link

Problem:

  • MCP server disconnects after ~60 seconds of inactivity
  • fread() blocks and returns false when no data available
  • This triggers "Failed to read from STDIN" error and breaks the listening loop

Solution (Variant 2):

  • Use stream_select() with 1-second timeout to check data availability before reading
  • Remove feof() check, rely entirely on stream_select() and $this->closing flag
  • Enable graceful shutdown by checking $this->closing flag every second
  • Prevents blocking while maintaining responsive shutdown

Testing:
✅ Tested for 150+ seconds without disconnections
✅ Compatible with macOS and other UNIX systems
✅ Graceful shutdown works correctly

Changes:

  • Added stream_select() before fread() to wait for available data
  • Replaced while (!\feof($this->input)) with while (!$this->closing)
  • Short 1-second timeout allows periodic check of $this->closing flag
  • Fixed $deferred->resolve() to pass null argument (line 144)

Comparison to Variant 1:

  • Simpler code without feof() check
  • Relies entirely on stream_select() for EOF detection
  • May have edge cases on systems where stream_select() doesn't detect EOF properly

…m_select only)

Problem:
- MCP server disconnects after ~60 seconds of inactivity
- fread() blocks and returns false when no data available
- This triggers "Failed to read from STDIN" error and breaks the listening loop

Solution (Variant 2):
- Use stream_select() with 1-second timeout to check data availability before reading
- Remove feof() check, rely entirely on stream_select() and $this->closing flag
- Enable graceful shutdown by checking $this->closing flag every second
- Prevents blocking while maintaining responsive shutdown

Testing:
✅ Tested for 150+ seconds without disconnections
✅ Compatible with macOS and other UNIX systems
✅ Graceful shutdown works correctly

Changes:
- Added stream_select() before fread() to wait for available data
- Replaced `while (!\feof($this->input))` with `while (!$this->closing)`
- Short 1-second timeout allows periodic check of $this->closing flag
- Fixed $deferred->resolve() to pass null argument (line 144)

Comparison to Variant 1:
- Simpler code without feof() check
- Relies entirely on stream_select() for EOF detection
- May have edge cases on systems where stream_select() doesn't detect EOF properly
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