-
-
Notifications
You must be signed in to change notification settings - Fork 931
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
Background tasks are cancelled if the client closes connection #1438
Labels
bug
Something isn't working
Comments
I looked the code that |
The PR that solves the issue has all the details. EDIT: My PR had some regressions. I've closed it. |
1 task
The background tasks need to be shielded. diff --git a/starlette/background.py b/starlette/background.py
index 4aaf7ae..db9b38a 100644
--- a/starlette/background.py
+++ b/starlette/background.py
@@ -1,6 +1,8 @@
import sys
import typing
+import anyio
+
if sys.version_info >= (3, 10): # pragma: no cover
from typing import ParamSpec
else: # pragma: no cover
@@ -22,10 +24,11 @@ class BackgroundTask:
self.is_async = is_async_callable(func)
async def __call__(self) -> None:
- if self.is_async:
- await self.func(*self.args, **self.kwargs)
- else:
- await run_in_threadpool(self.func, *self.args, **self.kwargs)
+ with anyio.CancelScope(shield=True):
+ if self.is_async:
+ await self.func(*self.args, **self.kwargs)
+ else:
+ await run_in_threadpool(self.func, *self.args, **self.kwargs) |
This was referenced Jun 26, 2022
jhominal
added a commit
to jhominal/starlette
that referenced
this issue
Jul 2, 2022
…ecv_stream + http.disconnect in receive fixes encode#1438
jhominal
added a commit
to jhominal/starlette
that referenced
this issue
Jul 2, 2022
…ecv_stream + http.disconnect in receive fixes encode#1438
jhominal
added a commit
to jhominal/starlette
that referenced
this issue
Jul 2, 2022
…ecv_stream + http.disconnect in receive fixes encode#1438
jhominal
added a commit
to jhominal/starlette
that referenced
this issue
Jul 2, 2022
…ecv_stream + http.disconnect in receive fixes encode#1438
jhominal
added a commit
to jhominal/starlette
that referenced
this issue
Jul 2, 2022
…ecv_stream + http.disconnect in receive fixes encode#1438
jhominal
added a commit
to jhominal/starlette
that referenced
this issue
Aug 16, 2022
…ecv_stream + http.disconnect in receive fixes encode#1438
jhominal
added a commit
to jhominal/starlette
that referenced
this issue
Sep 3, 2022
…ecv_stream + http.disconnect in receive fixes encode#1438
Kludex
added a commit
that referenced
this issue
Sep 24, 2022
…ct`+`recv_stream.close` (#1715) * replace BaseMiddleware cancellation after request send with closing recv_stream + http.disconnect in receive fixes #1438 * Add no cover pragma on pytest.fail in tests/middleware/test_base.py Co-authored-by: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> * make http_disconnect_while_sending test more robust in the face of scheduling issues * Fix issue with running middleware context manager Reported in #1678 (comment) Co-authored-by: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
9 tasks
aminalaee
pushed a commit
that referenced
this issue
Feb 13, 2023
…ct`+`recv_stream.close` (#1715) * replace BaseMiddleware cancellation after request send with closing recv_stream + http.disconnect in receive fixes #1438 * Add no cover pragma on pytest.fail in tests/middleware/test_base.py Co-authored-by: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> * make http_disconnect_while_sending test more robust in the face of scheduling issues * Fix issue with running middleware context manager Reported in #1678 (comment) Co-authored-by: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Checklist
master
.Describe the bug
When the HTTP client closes the TCP socket immediately after receiving the HTTP response, background tasks are cancelled.
This bug only happens when running the ASGI under uvicorn, and only if at least one HTTP Middleware is defined in the user middleware chain.
Steps to reproduce the bug
repro.py
:uvicorn
(either uvloop or regular asyncio will reproduce the issue) on localhost:8000Expected behavior
The client script gets the HTTP response, and both background tasks should complete successfully.
The expected behavior will be detectable by the following content in standard output:
Actual behavior
Background task 1 is interrupted at the
await
point and background task 2 is never started.That results in the following content in the output (when running the
repro.py
application):Debugging material
No response
Environment
Additional context
passthrough
middleware, the bug goes away.hypercorn
, the bug goes away.uvloop
or not.time.sleep(10)
) maintains the TCP connection open, the bug goes away.The text was updated successfully, but these errors were encountered: