Skip to content

Commit 6827fe5

Browse files
committed
Fix thread affinity in production request pipeline
Consolidate the request pipeline into a single executor call so that request_started, before-middleware, view dispatch, after-middleware, and request_finished all run on the same thread. This preserves thread-local DB connection assumptions (threading.local). Move request_finished from response.close() into the pipeline so it fires on the correct thread. The test client now calls the sync pipeline directly, removing the need for _SameThreadExecutor and unconditional asyncio.run().
1 parent e47efeb commit 6827fe5

File tree

3 files changed

+193
-221
lines changed

3 files changed

+193
-221
lines changed

plain/plain/http/response.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from http.cookies import SimpleCookie
1515
from typing import IO, Any
1616

17-
from plain import signals
1817
from plain.http.cookie import sign_cookie_value
1918
from plain.json import PlainJSONEncoder
2019
from plain.utils import timezone
@@ -135,9 +134,6 @@ def __init__(
135134
"'content_type' parameter is provided."
136135
)
137136
self._resource_closers = []
138-
# This parameter is set by the handler. It's necessary to preserve the
139-
# historical behavior of request_finished.
140-
self._handler_class = None
141137
self.cookies = SimpleCookie()
142138
self.closed = False
143139
if status_code is not None:
@@ -320,7 +316,6 @@ def close(self) -> None:
320316
# Free resources that were still referenced.
321317
self._resource_closers.clear()
322318
self.closed = True
323-
signals.request_finished.send(sender=self._handler_class)
324319

325320

326321
class Response(ResponseBase):

0 commit comments

Comments
 (0)