Skip to content

Receive a stdio server message that arrives in several reads - #68

Open
skybleak333 wants to merge 1 commit into
logiscape:mainfrom
skybleak333:client-stdio-partial-lines
Open

skybleak333 wants to merge 1 commit into
logiscape:mainfrom
skybleak333:client-stdio-partial-lines

Conversation

@skybleak333

Copy link
Copy Markdown

Summary

The client's stdio transport now keeps the part of a server message that has arrived until its newline comes, so a large response is received whole.

Motivation

StdioTransport reads the server's stdout without blocking, and there fgets() also returns the part of a line that has arrived so far. receive() collected the chunks in a local $buffer, so a part read in one call was dropped when the pipe ran dry, and it decoded any buffer that ended with } or ]. The rest of the message then arrived alone and failed to decode.

With a stdio server whose tool returns a string of the requested length, and the SDK client calling it ($client->connect() + callTool(), macOS, PHP 8.4):

result length before after
1 KB to 300 KB received received
500 KB, 700 KB, 1 MB JsonException: Syntax error received
3 MB — received

receive() now keeps the unfinished line in the read stream and decodes it once its newline arrives, which is how the stdio transport delimits messages. A last message without a trailing newline is still decoded when the server closes stdout.

Type of change

  • Bug fix (non-breaking change that fixes an issue)

Versioning impact

  • Patch (v1.7.X) — non-breaking bug fix or minor feature/improvement

Test plan

  • composer check passes locally
  • composer conformance passes locally — not run: the suite drives the HTTP transport, this change is in the stdio one
  • Relevant unit tests added or updated: StdioTransportPartialLineTest starts a small PHP script as the server, which writes a response in two parts with a pause between them (both tests fail without the change)
  • Smoke-tested with a real stdio server and the SDK client as described above

Compatibility

  • Change preserves core-feature compatibility under cPanel / Apache / PHP-FPM
  • No new required runtime dependencies

Documentation

  • Added an entry to CHANGELOG.md under [Unreleased]

Additional notes

#66 fixes the same fgets() behaviour on the server side. The two PRs touch different files, but both add a ### Fixed section under [Unreleased] in the changelog, so whichever lands second needs that merged; happy to rebase.

receive() kept the part of a line that fgets() returned only within one call, and decoded any buffer ending with } or ], so a large response failed with a JsonException.
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