-
Notifications
You must be signed in to change notification settings - Fork 0
Description
Fatal Error: Cannot get return value of a generator that hasn't returned in CompletionsApi
Description
When an upstream AI API (e.g. x.ai, DeepSeek) drops the connection mid-stream via HTTP/2 INTERNAL_ERROR or an SSL unexpected EOF, the generator passed to Streamer::stream() never reaches its return statement. Calling Generator->getReturn() immediately after on an incomplete generator throws an uncaught exception, crashing the entire request with a Fatal Error.
Error
PHP Fatal error: Uncaught Exception: Cannot get return value of a generator that hasn't returned
in src/Presentation/RequestHandlers/Api/Ai/CompletionsApi.php:78
Root Cause
Streamer::stream() catches the upstream exception internally and sends an error SSE event to the client — which is correct. However, callback() in CompletionsApi unconditionally calls $generator->getReturn() afterwards, without checking whether the generator actually completed.
Steps to Reproduce
- Send a completion request that gets proxied to an AI provider.
- Have the provider drop the connection mid-stream (network interruption, provider-side timeout, HTTP/2 RST, etc.).
- Observe Fatal Error in PHP error log.
Fix
Wrap $generator->getReturn() in a try-catch(Throwable) block. If the generator did not complete, silently swallow the exception — the error SSE event was already dispatched by Streamer::stream().
Affected File
src/Presentation/RequestHandlers/Api/Ai/CompletionsApi.php