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

KTOR-8135 Socket accept not throwing error on socket close on native #4637

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Thomas-Vos
Copy link
Contributor

KTOR-8135 Socket accept not throwing error on socket close on native

Test failed before on native macosArm64 but was successful on JVM, so native specific bug

@bjhham bjhham self-requested a review January 31, 2025 08:16
@@ -68,6 +68,7 @@ private class ServerSocketImpl(
}

override fun close() {
incoming.close(IOException("Server socket closed"))
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's better to use cancel instead of close here
this will also allow to revert incoming to use ReceiveChannel to respect the contract

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the close makes sense here to keep the exception in the same family as JVM (currently throws a java.nio.channels.ClosedChannelException) since cancel would throw a cancellation exception instead. Maybe it ought to go on line 66 in lieu of the cancel call, or maybe we should just use cancellation exceptions everywhere instead of IOException, though it seems leaving it with IOException would be the least disruptive change for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I went for IOException to keep it consistent with JVM. I don't think a CancellationException makes sense because calling accept on a closed socket should not be ignored as it is an error.

I didn't put it in line 66 as I am not sure whether this could be called in situations other than a close.

Copy link
Contributor Author

@Thomas-Vos Thomas-Vos Feb 2, 2025

Choose a reason for hiding this comment

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

However, there are more inconsistencies. For example attachForReading/attachForWriting on sockets. On some platforms they are stopped (with CancellationException), but on some platforms nothing happens. On iOS this even causes TCP sockets to hang when closed, which I would say is a bug, https://youtrack.jetbrains.com/issue/KTOR-8144. Trying to improve this in my other PR but having some issues with tests failing.

@@ -68,6 +68,7 @@ private class ServerSocketImpl(
}

override fun close() {
incoming.close(IOException("Server socket closed"))
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the close makes sense here to keep the exception in the same family as JVM (currently throws a java.nio.channels.ClosedChannelException) since cancel would throw a cancellation exception instead. Maybe it ought to go on line 66 in lieu of the cancel call, or maybe we should just use cancellation exceptions everywhere instead of IOException, though it seems leaving it with IOException would be the least disruptive change for now.

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.

3 participants