Skip to content
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

Refactors Sockets closed promise handling. #237

Merged
merged 1 commit into from
Jan 16, 2023

Conversation

dom96
Copy link
Collaborator

@dom96 dom96 commented Dec 22, 2022

This is a more basic version of #205. The main change in semantics here is that the closed promise will now have any errors propagated to it (from the readable/writable stream close calls). The rest is a major refactor to reuse the existing newPromisedStream.

Test Plan

$ bazel test //src/ew/tests:sockets/sockets-tcp-test.ew-test-bin --config=asan --test_timeout 5

@dom96 dom96 force-pushed the dominik/cleanup-sockets-streams branch from ca3d4fa to f79c885 Compare December 22, 2022 18:13
src/workerd/api/system-streams.c++ Outdated Show resolved Hide resolved
src/workerd/api/system-streams.c++ Outdated Show resolved Hide resolved
src/workerd/api/system-streams.c++ Outdated Show resolved Hide resolved
src/workerd/api/sockets.c++ Outdated Show resolved Hide resolved
src/workerd/api/sockets.h Outdated Show resolved Hide resolved
src/workerd/api/sockets.h Outdated Show resolved Hide resolved
src/workerd/api/sockets.c++ Outdated Show resolved Hide resolved
@dom96 dom96 force-pushed the dominik/cleanup-sockets-streams branch 3 times, most recently from a5277b5 to 171059b Compare December 23, 2022 18:36
@dom96
Copy link
Collaborator Author

dom96 commented Dec 23, 2022

Last push was a rebase.

Ready for another review

src/workerd/api/sockets.c++ Outdated Show resolved Hide resolved
@kentonv
Copy link
Member

kentonv commented Jan 2, 2023

Please note my reply to a previous thread (which had been marked resolved): #237 (comment)

@dom96
Copy link
Collaborator Author

dom96 commented Jan 4, 2023

@kentonv responded.

@dom96 dom96 force-pushed the dominik/cleanup-sockets-streams branch from 171059b to fc8da39 Compare January 9, 2023 12:39
@dom96
Copy link
Collaborator Author

dom96 commented Jan 9, 2023

Rebased

@dom96 dom96 force-pushed the dominik/cleanup-sockets-streams branch from fc8da39 to 46c7aa2 Compare January 9, 2023 15:54
@dom96
Copy link
Collaborator Author

dom96 commented Jan 9, 2023

Ready for review.

Copy link
Member

@jasnell jasnell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but let's also have @kentonv or @harrishancock review and sign off.

@dom96 dom96 force-pushed the dominik/cleanup-sockets-streams branch from 46c7aa2 to b315ad3 Compare January 12, 2023 16:18
@dom96
Copy link
Collaborator Author

dom96 commented Jan 12, 2023

Rebased

@dom96 dom96 force-pushed the dominik/cleanup-sockets-streams branch from b315ad3 to 3979484 Compare January 12, 2023 16:59
src/workerd/api/sockets.h Outdated Show resolved Hide resolved
// better way?
auto& context = IoContext::current();
return context.awaitIo(kj::mv(closeFulfiller->promise));
jsg::MemoizedIdentity<jsg::Promise<void>>& getClosed() {
Copy link
Member

@kentonv kentonv Jan 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like, at present, this promise is only resolved when the application calls close() explicitly -- not when the remote side disconnects. Is that correct?

If so, it seems like it's not accomplishing a whole lot to offer a way to listen for closure. The application can set that up itself and save us the trouble. So I'd remove getClosed() in that case.

If the intent is that this promise should also tell the application when the remote end disconnects the socket, then I think you should add code that listens on whenWriteDisconnected() and fulfills the promise at that point. (Or maybe it should wait on both whenWriteDisconnected() and EOF or error on the read side, and only resolve when both are true... I'm not really sure... I'd be satisfied with waiting for whenWriteDisconnected() only.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it's the current behaviour in workerd HEAD as well. I split this PR off from #205 to not block on this and instead focus on the refactoring.

I will follow up on this but first we need to decide how we want the semantics to work exactly.

@dom96 dom96 force-pushed the dominik/cleanup-sockets-streams branch 3 times, most recently from c4c410b to f71e02a Compare January 13, 2023 16:16
resolveFulfiller(nullptr);
return js.resolvedPromise();
}, [this](jsg::Lock& js, jsg::Value err) { return errorHandler(js, kj::mv(err)); });
}, [this](jsg::Lock& js, jsg::Value err) { return errorHandler(js, kj::mv(err)); });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it looks like any exceptions caught from cancel() or abort() propagate to the closed promise, but do NOT propagate to the result of close() itself.

Is that what we want? I'm not really sure when cancel() or abort() would throw, but it would seem to me that the exception should propagate out of close(). Meanwhile it's not clear to me whether the closed promise should signal errors or just signal that the socket is now closed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I'd allow the error to propagate to the closed promise but I would explicitly mark it handled using markAsHandled so that if someone isn't paying attention it's perfectly ok.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meanwhile it's not clear to me whether the closed promise should signal errors or just signal that the socket is now closed.

It's not clear to me either. Though I trust James' opinion here.

This is another case that I think we need to discuss in more depth. I want us to do so on this issue and many others, after discussion we may end up with another decision (especially if we decide to conform to other specs) so let's not worry too much about making it perfect in this PR.

src/workerd/api/sockets.h Outdated Show resolved Hide resolved
@dom96 dom96 force-pushed the dominik/cleanup-sockets-streams branch from f71e02a to 32a91b6 Compare January 13, 2023 20:07
@jasnell
Copy link
Member

jasnell commented Jan 13, 2023

@dom96 ... PR needs a rebase.

@dom96 dom96 force-pushed the dominik/cleanup-sockets-streams branch from 32a91b6 to ca99307 Compare January 13, 2023 20:25
@dom96 dom96 merged commit 14837fd into main Jan 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants