Skip to content

RedirectHandler leaves streaming responses open on redirect errors, exhausting the connection pool #900

Open

Description

Environment

  • CPython 3.10.12, Linux
  • Current main: 37b270459f391768df698142c741945365cd3191
  • Local microsoft-kiota-http and microsoft-kiota-abstractions 1.18.0 sources

Bug

RedirectHandler.send() leaves the last streaming response open when it raises RedirectError after exhausting the redirect limit. The same happens if _build_redirect_request() raises, for example because the redirect changes schemes and allow_redirect_on_scheme_change is false, the location has an invalid port, or a custom sensitive-header callback raises.

The successful redirect path calls await response.aclose() only after constructing the next request. The error paths exit before reaching that cleanup, and the caller never receives the response to close it.

Reproduction

With a localhost HTTP/1.1 endpoint returning a 302 and a nonempty body, a real httpx.AsyncHTTPTransport limited to one connection, and RedirectHandlerOption(max_redirect=0):

  1. Send a request through RedirectHandler to the redirect endpoint.
  2. Catch the expected RedirectError.
  3. Send an ordinary request through the same handler/transport to a 200 endpoint.

On current main the second request raises httpx.PoolTimeout. Closing the discarded response before propagating the first error makes that second request return 200. This uses a local server, not Microsoft Graph or a live tenant.

A small in-memory check also shows the missing cleanup:

import asyncio
import httpx
from kiota_http.middleware.redirect_handler import RedirectHandler
from kiota_http.middleware.options import RedirectHandlerOption

class Body(httpx.AsyncByteStream):
    closed = False
    async def __aiter__(self):
        yield b"unread body"
    async def aclose(self):
        self.closed = True

async def main():
    body = Body()
    async def respond(request):
        return httpx.Response(302, headers={"Location": "/loop"},
                              stream=body, request=request)
    try:
        await RedirectHandler(RedirectHandlerOption(max_redirect=0)).send(
            httpx.Request("GET", "https://example.org/start"),
            httpx.MockTransport(respond),
        )
    except Exception as exc:
        print(type(exc).__name__, body.closed)

asyncio.run(main())
# Current: RedirectError False
# Expected: RedirectError True

Proposed scope

Close responses that are being discarded on redirect-limit or request-construction errors, while keeping returned responses open for the caller. In particular, successful final responses, disabled redirects, and an increment() override that stops redirect handling must retain their current ownership behavior.

A local candidate has nine regression/control cases: six fail on main, all nine pass with the candidate. The native HTTPX package suite passes before (227 tests) and after (236 tests). These are Linux/Python 3.10 results, not full-monorepo or hosted-CI validation.

This is separate from #565, which closes responses discarded by RetryHandler, and #735, which ends redirect tracing spans.

Would you welcome a focused PR for this cleanup?

AI assistance was used for diagnosis, patch preparation and test execution.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    • Status
      Needs Triage 🔍

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions