Skip to content

Fix ASGI streaming response start messages - #309

Open
pr3etk wants to merge 2 commits into
laurentS:masterfrom
pr3etk:fix/asgi-middleware-streaming
Open

pr3etk wants to merge 2 commits into
laurentS:masterfrom
pr3etk:fix/asgi-middleware-streaming

Conversation

@pr3etk

@pr3etk pr3etk commented Sep 30, 2026 •

Copy link
Copy Markdown

Streaming Middleware Fix Report

Issue

SlowAPIASGIMiddleware forwarded the stored http.response.start ASGI message whenever it received an http.response.body message. Streaming responses emit multiple body messages, so the middleware sent the response-start message once per chunk. ASGI servers such as Uvicorn reject repeated response-start messages.

Changes

  • Added a response_started flag to _ASGIMiddlewareResponder.
  • Forwarded the response-start message, including rate-limit headers and error status handling, only on the first body message.
  • Continued forwarding every body message unchanged.
  • Added a regression test using FastAPI and StreamingResponse with three chunks.

Verification

  • Before the fix, the regression test failed with assert 4 == 1 because four response-start messages were emitted.
  • After the fix, pytest tests/test_streaming.py -q passed: 1 passed.
  • The full suite after the fix reported 92 passed, 12 failed.
  • All 12 failures were the same pre-existing Starlette compatibility failure: AttributeError: 'Starlette' object has no attribute 'route' in tests/test_starlette_extension.py.

Scope

No unrelated Starlette compatibility failures were changed. The fix is limited to ensuring that an ASGI response-start message is sent exactly once for streaming responses.

Copilot AI balanced review requested due to automatic review settings September 30, 2026 14:38

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@pr3etk
pr3etk requested a balanced review from Copilot October 3, 2026 23:18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@pr3etk
pr3etk requested a balanced review from Copilot October 3, 2026 23:18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@pr3etk
pr3etk requested a balanced review from Copilot October 3, 2026 23:19

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@pr3etk
pr3etk requested a balanced review from Copilot October 6, 2026 12:35

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@pr3etk
pr3etk requested a balanced review from Copilot October 6, 2026 13:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@pr3etk pr3etk left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review findings\n\nNo correctness, regression, security, test, or maintainability issues found.\n\nReviewed slowapi/middleware.py:166-179: the response-start message is now emitted once while every response-body message continues to be forwarded. Reviewed ests/test_streaming.py:13-55, which covers a three-chunk StreamingResponse and rate-limit headers.\n\nValidation on the PR head:\n- pytest tests/test_streaming.py -q: 1 passed\n- Full suite: 92 passed, 12 pre-existing Starlette compatibility failures at ests/test_starlette_extension.py:178,211,236,257 because the installed Starlette lacks

oute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants