Skip to content

fix #247 - connection state leak #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()