Skip to content

Commit

Permalink
Reset lifespan state on each request (#1903)
Browse files Browse the repository at this point in the history
* Reset lifespan state on each request

* Only instantiate the protocol once on the lifespan test
  • Loading branch information
Kludex committed Mar 16, 2023
1 parent c459165 commit 4601d24
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 8 deletions.
5 changes: 2 additions & 3 deletions tests/protocols/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def get_connected_protocol(
protocol = protocol_cls(
config=config,
server_state=server_state,
app_state=lifespan.state.copy(),
app_state=lifespan.state,
_loop=loop,
)
protocol.connection_made(transport)
Expand Down Expand Up @@ -849,7 +849,6 @@ def receive_all(sock):
app = Response("Hello, world", media_type="text/plain")

def send_fragmented_req(path):

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("127.0.0.1", unused_tcp_port))
d = (
Expand Down Expand Up @@ -1015,8 +1014,8 @@ async def app(scope, receive, send):
# in the lifespan tests
lifespan.state.update({"a": 123, "b": [1]})

protocol = get_connected_protocol(app, protocol_cls, lifespan=lifespan)
for _ in range(2):
protocol = get_connected_protocol(app, protocol_cls, lifespan=lifespan)
protocol.data_received(SIMPLE_GET_REQUEST)
await protocol.loop.run_one()
assert b"HTTP/1.1 200 OK" in protocol.transport.buffer
Expand Down
2 changes: 1 addition & 1 deletion uvicorn/protocols/http/h11_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def handle_events(self) -> None:
"raw_path": raw_path,
"query_string": query_string,
"headers": self.headers,
"state": self.app_state,
"state": self.app_state.copy(),
}

upgrade = self._get_upgrade()
Expand Down
2 changes: 1 addition & 1 deletion uvicorn/protocols/http/httptools_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def on_message_begin(self) -> None:
"scheme": self.scheme,
"root_path": self.root_path,
"headers": self.headers,
"state": self.app_state,
"state": self.app_state.copy(),
}

# Parser callbacks
Expand Down
2 changes: 1 addition & 1 deletion uvicorn/protocols/websockets/websockets_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ async def process_request(
"query_string": query_string.encode("ascii"),
"headers": asgi_headers,
"subprotocols": subprotocols,
"state": self.app_state,
"state": self.app_state.copy(),
}
task = self.loop.create_task(self.run_asgi())
task.add_done_callback(self.on_task_complete)
Expand Down
2 changes: 1 addition & 1 deletion uvicorn/protocols/websockets/wsproto_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def handle_connect(self, event: events.Request) -> None:
"headers": headers,
"subprotocols": event.subprotocols,
"extensions": None,
"state": self.app_state,
"state": self.app_state.copy(),
}
self.queue.put_nowait({"type": "websocket.connect"})
task = self.loop.create_task(self.run_asgi())
Expand Down
2 changes: 1 addition & 1 deletion uvicorn/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def create_protocol(
return config.http_protocol_class( # type: ignore[call-arg]
config=config,
server_state=self.server_state,
app_state=self.lifespan.state.copy(),
app_state=self.lifespan.state,
_loop=_loop,
)

Expand Down

0 comments on commit 4601d24

Please sign in to comment.