Repository navigation
Add Task#wait_cancel_deferred. - #478
willcosgrove wants to merge 1 commit into
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LTGVwkVUKFBQfa98fZ8zRJ
|
It looks like I was wrong about the H18s being eliminated on Heroku. It has reduced but not gone away completely. I'm still investigating but it appears like it may be some kind of race condition between Heroku's SIGTERM that it sends to all processes and the signal propagation from async-container to its child processes. The child processes see two interrupts so it treats it as a force quit. I'm not sure where the right place is to fix that. |
|
Following up on my last comment, which muddied things: the remaining H18s turned out to be a separate problem, unrelated to this PR. Heroku sends SIGTERM to every process, so workers get a second Interrupt mid-drain and all in-flight responses are cut off. I've written that up at socketry/async-container#80. With that worked around, this PR did its job: we ran it in production for several weeks, and SSE streams ended cleanly on every deploy. So I think this is ready for review whenever you have time. On the Falcon side I have a small middleware built on it ( |
Background: socketry/falcon#368.
Following your suggestion there, this exposes the deferred cancel. The request is asked to stop: async-http's
defer_cancelrecords the cancel on the connection task. The issue sawcancel_deferred?stayfalsebecause the streaming body runs in a child task (started withFiber.schedule), and even on the right task the flag can only be polled.Task#wait_cancel_deferredwaits until a cancel has been deferred, on this task or the nearest ancestor insidedefer_cancel, and returns the cancel's cause. It checks parent tasks because a deferring task's children are part of the work it's protecting, which is how the response body relates to the connection task.I worked on this with Claude. And I admit I don't know the problem space super well, so if the naming feels off that's probably why. Based on my limited understanding
wait_cancel_deferredseemed to fit, but I have no particular attachment to it and would be happy to change it if you have a better name in mind.I was able to use this patch in production to solve the problem. However I wasn't completely happy with how much knowledge the app code needed of how falcon uses async. So I worked on a patch to falcon as well that exposes this through a callback in
env.Proposed use in Falcon
Rack apps shouldn't need to know about tasks or
defer_cancel, so Falcon could expose this through the env, following therack.response_finishedpattern (branch):An SSE endpoint then only needs:
I've tested both the
asyncchange, and the proposedfalconhook in our production app and the the SSE stream closes cleanly right after SIGTERM, and the worker exits without being killed. On Heroku, deploys no longer produce H18s.Types of Changes
Contribution