-
Notifications
You must be signed in to change notification settings - Fork 440
Reproduction of request cancellation in workerd #5062
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
base: main
Are you sure you want to change the base?
Conversation
|
||
console.log('Starting long-running task'); | ||
// Simulate a long-running task so we can test aborting | ||
await new Promise((resolve) => setTimeout(resolve, 3000)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try also adding:
ctx.waitUntil(new Promise((resolve) => setTimeout(resolve, 4000)));
Without the waitUntil(), as soon as the client disconnects, the entire request context is torn down immediately, and hence the abort event doesn't get a chance to be delivered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just gave that a try in 07ecdbd, but I'm still not seeing any logs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fwiw, ctx.waitUntil(scheduler.wait(3000))
would do the same thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't seem to work locally for me either. I'll try putting a debugger on it, unless anyone has any leads.
How are you testing the abort? Simply by disconnecting the requester? while the timeout is still pending? |
At least that's what I'm doing when trying to repro. It's a bit of an odd situation. The request is fully sent, and no response is sent at all. The demos I provided would have a streaming response, and the client would abort while reading, etc. |
Try starting to stream a response rather than just setting a timeout. Just have it where every second or so it writes a new chunk. Use the const { writable, readable } = new IdentityTransformStream();
const writer = writable.getWriter();
const enc = new TextEncoder();
setInterval(() => writer.write(enc.encode('ping')), 1000);
ctx.waitUntil(scheduler.wait(10_000)); // 10 seconds
return new Response(readable); And see if that makes a difference |
@jasnell Yep, that's the "right way" and what I showed in my examples and demos. |
Yep, it should. If the event is working when data is being streamed but not when we're just waiting for a timeout then that suggests the logic for detecting the disconnect is flawed. Likely it's probably only actually detecting the disconnect when the next chunk of data is being written to the socket. |
This seems quite plausible... |
FWIW, I have tried the exact example from the changelog but it doesn't work as well. I just simplified the example here. |
@edmundhung I cannot reproduce that locally. I just tried the changlelog example on the latest wrangler and it still works for me. |
@npaun Right. Just test it again and your demo works locally. Thanks for confirming. |
I have been testing request cancellation support in Wrangler and noticed that Workerd does not log the
Request was aborted
message either. Is that expected?The same worker does log the message in production.