Detect/handle closed client connections
See original GitHub issueIt appears like starlette does not cancel inner processing for when a client disconnects.
With the following code it will keep on fetching from the remote, although the client has disconnected already, e.g. when cancelling a curl call.
@app.route(…)
async def foo(request):
…
if content_type.startswith("video/"):
bytes_total = req.headers["Content-Length"]
logger.info("Streaming {} bytes...", bytes_total)
async def generator(bytes_total):
import aiohttp
async with aiohttp.ClientSession() as session:
chunk = 0
chunk_size = 102400
bytes_read = 0
async with session.request(method, str(url), chunked=chunk_size) as resp:
async for data, end_of_http_chunk in resp.content.iter_chunks():
chunk += 1
bytes_read += chunk_size
logger.info("chunk {} {}/{}", chunk, bytes_read, bytes_total)
yield data
return StreamingResponse(generator(bytes_total), media_type=content_type)
It might be useful if the handler could be notified of this, in case it wants to handle this, but in general I think a late middleware, responsible for sending could handle this maybe?!
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Handling abandoned/disconnected Sockets - Stack Overflow
the connections will be closed (assuming TCP). You will know that those connections are dead. Exactly how depends on your coding.
Read more >Action 2: investigate closed client connection - IBM
2610-204 The client connection is closed due to incorrect message (incorrect message code). Diagnosis: The 2610-204 message is logged by the RMC subsystem, ......
Read more >what is TCP Half Open Connection and TCP half closed ...
TCP has a vulnerability in that the final FIN packet sent to a client can be potentially dropped by routers/networks resulting in a...
Read more >How to detect when the client closes the connection?
We need a separate process that does nothing else but checking whether a connection has been closed by the client (of course, this...
Read more >Managing Client Connections - Vertica
This section explains how you can control the way clients connect to your database. Vertica gives you several settings to control client connections:....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think the following would also do the trick, for a non-blocking receive:
message = await asyncio.wait_for(receive, timeout=0)
.Right. The thing is that the disconnect needs to be handled in an event driven way, rather than raising an exception on
send()
.Sure, so I think what you’d need is something like:
Not wild about how akward it is to do a await either this thing or that thing and switch on either, but there we are.