Skip to content

Ensure response stream is only closed after request stream is closed #177

@tillrohrmann

Description

@tillrohrmann

Problem

When handling an invocation over HTTP, the SDK must not close the HTTP response stream before the request stream has been fully consumed/closed. If the response stream is closed while the request stream is still open, the runtime may interpret this as a connection abort and log warnings or trigger unnecessary retries.

This issue was discovered and fixed in the TypeScript SDK: restatedev/sdk-typescript#651

Expected behavior

After the SDK has flushed all output to the response stream, it should drain/await the request stream EOS (end-of-stream) before closing the response stream. This ensures a clean HTTP connection lifecycle:

  1. SDK finishes writing all response data
  2. SDK waits for the request stream to be fully closed (read until EOF or done)
  3. SDK closes the response stream

Reference implementation

In the TypeScript SDK fix, the change was to add a loop that reads the input stream until it's done (or errors) before calling outputWriter.close():

// Let's make sure we properly close the request stream before closing the response stream
let inputClosed = false;
while (!inputClosed) {
  try {
    const res = await inputReader.read();
    inputClosed = res.done;
  } catch (e) {
    inputClosed = true;
  }
}

// Close the response stream
await outputWriter.close();

Action items

  • Review the HTTP handler code to check if the response stream can be closed before the request stream is fully consumed
  • If susceptible, implement a fix to drain the request stream before closing the response stream
  • Verify with testing

Note: The Python SDK uses sdk-shared-core (WASM bindings). The fix may need to be in the Python HTTP handler layer rather than in the shared core itself.

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