[Server] Flush output buffers on stateless SSE streams - #520
Open
SammyTourani wants to merge 1 commit into
Open
SammyTourani wants to merge 1 commit into
SammyTourani wants to merge 1 commit into
Conversation
StatelessResponder called only flush(), which does not empty PHP's own output buffer. With output_buffering enabled, as php.ini-production and php.ini-development ship it, the frames of a subscriptions/listen stream (starting with the acknowledgement) stayed buffered until the stream closed, so clients waiting for the acknowledgement timed out. Call @ob_flush() before flush(), as StreamableHttpTransport already does. Fixes modelcontextprotocol#516
SammyTourani
requested review from
CodeWithKyrian,
Nyholm,
chr-hertel and
soyuka
as code owners
September 29, 2026 16:21
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #516
Fixes #516.
src/Server/Transport/Http/StatelessResponder.php: call@ob_flush()beforeflush()after each frame, the same two callsStreamableHttpTransportalready makes. The
@suppresses the noticeob_flush()raises when nooutput buffer is active.
tests/Integration/StatelessLifecycleTest.php:-d output_buffering=4096. Withoutthat, whether it buffers depends on the php.ini the machine loads.
testListenIsAcknowledgedAtOnceopenssubscriptions/listenandrequires the first
data:frame to be the acknowledgement. It reads with a5 s timeout, well inside the example's 20 s lifetime.
Verification
Command:
vendor/bin/phpunit --testsuite=unit && vendor/bin/phpunit --testsuite=integrationResult (PHP 8.4.23):
Failed asserting that null is identical to 'notifications/subscriptions/acknowledged'.With the fix it passes in 0.07 s.
it.
tests/Integration/StatelessLifecycleTest.phpgivesOK (11 tests, 39 assertions)and the unit suite givesOK (1582 tests, 4107 assertions).stateless-lifecycleexample underphp -d output_buffering=4096 -S …and timed each SSE line of asubscriptions/listenstream over a raw socket.closing result.
output_buffering=0, both versions deliver it at 0.01 s.php -Sworker stayed busy for another 19.59 s. With the fix it answered thenext request 0.02 s later.
php-cs-fixer fix --dry-run --diffon bothfiles found 0 of 2 files to fix, and
phpstanreported no errors.