-
Notifications
You must be signed in to change notification settings - Fork 2
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
sockets.close behaviour when closed
already rejected
#24
Comments
IMO the |
This is exactly what I expect it to be at the beginning. This seems like a more reasonable behaviour. (Though I don't know why it's different at Workers.) Maybe at least point this difference out in the docs? |
Let's separate the operation of the If I call const p1 = socket.close(); // initiates closing, returns closed
const p2 = socket.close(); // closing already initiated, returns closed
p1 === p2; // true The promise returned by When the socket close completes, the The second function close() {
if (!closing) startClosing();
return this.closed;
}
function startClosing() {
closing = true;
if (hasPendingWrites) pendingWrites.then(() => doClose());
else doClose();
} If the Separately, to what @kentonv was saying, initiating a close does mean waiting for all currently pending writes to complete and be flushed. So, for instance, if I have: const writer = socket.writable.getWriter();
const w1 = writer.write(enc.encode('hello'));
const w2 = writer.write(enc.encode('there'));
const p1 = writer.close(); // p1 resolves only after w1 and w2 are resolved
const w3 = writer.write(enc.encode('!!')); // rejects immediately because close has been called, even tho closed maybe has not yet resolved
await Promise.all([w1, w2, p1]); The call to If either of the pending prior writes (w1 or w2) fail, then that failure should cascade to all pending writes and the pending close (e.g. if w1 rejects, w2 and p1 will reject with the same error. w3 will have already been rejected since the additional write-after-close is not allowed). In other words, |
From cloudflare/workerd#1305 we've realised that we should define what should happen when
close
is called on a socket that already had itsclosed
promise rejected.When the
closed
promise is resolved then the answer is easy:close
does nothing.But when
closed
is rejected, we have a couple of options:close
throws the exception inclosed
close
doesn't throw and leaves theclosed
promise unchanged (so it remains rejected)Workerd currently does a third option, which I think just leads to gotchas so I want to change it, where
close
doesn't throw but also resolves theclosed
promise which means the exception can be lost.The text was updated successfully, but these errors were encountered: