Skip to content

Fatal Error: Cannot get return value of a generator that hasn't returned in CompletionsApi #6

@xinpehr

Description

@xinpehr

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

  1. Send a completion request that gets proxied to an AI provider.
  2. Have the provider drop the connection mid-stream (network interruption, provider-side timeout, HTTP/2 RST, etc.).
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions