Skip to content

Commit 61bd6b8

Browse files
committed
Catch exceptions from streaming generators and send stream stop flag; send stop flag if RR has stopped the stream; add comments
1 parent 0e7c7c9 commit 61bd6b8

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/HttpWorker.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,37 @@ private function respondStream(int $status, Generator $body, array $headers = []
9393

9494
do {
9595
if (!$body->valid()) {
96+
// End of generator
9697
$content = (string)$body->getReturn();
9798
if ($endOfStream === false && $content === '') {
99+
// We don't need to send an empty frame if the stream is not ended
98100
return;
99101
}
100102
$worker->respond(new Payload($content, $head, $endOfStream));
101103
break;
102104
}
105+
103106
$content = (string)$body->current();
104107
if ($worker->getPayload(StreamStop::class) !== null) {
105108
$body->throw(new StreamStoppedException());
109+
110+
// RoadRunner is waiting for a Stream Stop Frame to confirm that the stream is closed
111+
// and the worker doesn't hang
112+
$worker->respond(new Payload(''));
106113
return;
107114
}
115+
116+
// Send a chunk of data
108117
$worker->respond(new Payload($content, $head, false));
109-
$body->next();
110118
$head = null;
119+
120+
try {
121+
$body->next();
122+
} catch (\Throwable) {
123+
// Stop the stream if an exception is thrown from the generator
124+
$worker->respond(new Payload(''));
125+
return;
126+
}
111127
} while (true);
112128
}
113129

src/HttpWorkerInterface.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public function waitRequest(): ?Request;
2525
* @param Generator<mixed, scalar|Stringable, mixed, Stringable|scalar|null>|string $body Body of response.
2626
* If the body is a generator, then each yielded value will be sent as a separated stream chunk.
2727
* Returned value will be sent as a last stream package.
28-
* Note: Stream response is experimental feature and isn't supported by RoadRunner yet.
29-
* But you can try to use RoadRunner 2.9-alpha to test it.
28+
* Note: Stream response is supported by RoadRunner since version 2023.3
3029
* @param HeadersList|array $headers An associative array of the message's headers. Each key MUST be a header name,
3130
* and each value MUST be an array of strings for that header.
3231
*/

0 commit comments

Comments
 (0)