Skip to content

Commit

Permalink
fix connection state leak
Browse files Browse the repository at this point in the history
  • Loading branch information
provinzkraut committed Feb 1, 2025
1 parent 84d06b8 commit 8ac99e4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/hypercorn/asyncio/tcp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async def run(self) -> None:
self.config,
self.context,
task_group,
ConnectionState(self.state.copy()),
ConnectionState(self.state),
ssl,
client,
server,
Expand Down
2 changes: 1 addition & 1 deletion src/hypercorn/protocol/http_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ async def handle(self, event: Event) -> None:
"headers": event.headers,
"client": self.client,
"server": self.server,
"state": event.state,
"state": event.state.copy(),
"extensions": {},
}

Expand Down
2 changes: 1 addition & 1 deletion src/hypercorn/protocol/ws_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ async def handle(self, event: Event) -> None:
"headers": event.headers,
"client": self.client,
"server": self.server,
"state": event.state,
"state": event.state.copy(),
"subprotocols": self.handshake.subprotocols or [],
"extensions": {"websocket.http.response": {}},
}
Expand Down
2 changes: 1 addition & 1 deletion src/hypercorn/trio/tcp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async def run(self) -> None:
self.config,
self.context,
task_group,
ConnectionState(self.state.copy()),
ConnectionState(self.state),
ssl,
client,
server,
Expand Down
43 changes: 43 additions & 0 deletions tests/e2e/test_httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,46 @@ async def serve() -> None:
result.raise_for_status()

shutdown.set()


@pytest.mark.trio
async def test_handle_isolate_state():
config = Config()
config.bind = ["0.0.0.0:1234"]
config.accesslog = "-" # Log to stdout/err
config.errorlog = "-"

async def app(scope, receive, send):
assert scope["type"] == "http"

await send(
{
"type": "http.response.start",
"status": 200,
"headers": [[b"content-type", b"text/plain"]],
}
)
await send(
{"type": "http.response.body", "body": scope["state"].get("key", b"")}
)
scope["state"]["key"] = b"one"

async with trio.open_nursery() as nursery:
shutdown = trio.Event()

async def serve() -> None:
await hypercorn.trio.serve(app, config, shutdown_trigger=shutdown.wait)

nursery.start_soon(serve)

await trio.testing.wait_all_tasks_blocked()

client = httpx.AsyncClient()

result = await client.get("http://0.0.0.0:1234/")
assert result.content == b""

result = await client.get("http://0.0.0.0:1234/")
assert result.content == b""

shutdown.set()

0 comments on commit 8ac99e4

Please sign in to comment.