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

Skip TCP_NODELAY on sockets that NIO doesn't explicitly support #2476

Merged
merged 5 commits into from Jul 21, 2023

Conversation

simonjbeaumont
Copy link
Contributor

Motivation:

NIO automatically calls setsockopt with TCP_NODELAY unless the protocol family is PF_UNIX. However, since NIO offers API that allows users to create sockets out of band (withConnectedSocket(_:) and withBoundSocket(_:)), it cannot know whether the socket supports this option or not.

Modifications:

This PR inverts the filter so that it is only added for PF_INET and PF_INET6. Any other socket, e.g. PF_UNIX, or any socket that NIO doesn't explicitly handle.

Result:

TCP_NODELAY is skipped for sockets that NIO doesn't know about.

Signed-off-by: Si Beaumont <beaumont@apple.com>
@simonjbeaumont
Copy link
Contributor Author

Hm, these tests pass on macOS. Was a bit of a fiddle working out how we can test this. I tried playing with the SAL-backed tests but didn't get very far.

Anyone got thoughts on if and how we should test this?

@Lukasa Lukasa added the patch-version-bump-only For PRs that when merged will only cause a bump of the patch version, ie. 1.0.x -> 1.0.(x+1) label Jul 18, 2023
@@ -2770,39 +2770,66 @@ public final class ChannelTests: XCTestCase {
XCTAssertEqual(1, counter.errorCaughtCalls)
}

func testTCP_NODELAYisOnByDefault() throws {
func _testTCP_NODELAYis(
enabledByDefault: Bool,
Copy link
Contributor

Choose a reason for hiding this comment

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

This kind of "narrative style" function name is discouraged in Swift APIs, and in this particular instance I think it's hard to read. I think it'd be better to rename this to _testTCP_NODELAYDefaultValue(value:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Signed-off-by: Si Beaumont <beaumont@apple.com>
Signed-off-by: Si Beaumont <beaumont@apple.com>
@Lukasa
Copy link
Contributor

Lukasa commented Jul 18, 2023

Nice, Linux doesn't support getsockopt here either. Ok, we can make the SAL work, but we should sync up so I can help you drive it.

Signed-off-by: Si Beaumont <beaumont@apple.com>
@simonjbeaumont
Copy link
Contributor Author

Nice, Linux doesn't support getsockopt here either.

Yep, seems like it; this is the logs from CI:

14:29:15 /code/Tests/NIOPosixTests/ChannelTests.swift:2835: error: ChannelTests.testTCP_NODELAYisOffByDefaultForUnixSockets : XCTAssertEqual threw error "getsockopt(socket:level:optionName:optionValue:optionLen:): Operation not supported (errno: 95)" -

Ok, we can make the SAL work, but we should sync up so I can help you drive it.

I took a look and I don't think that SAL is going to be an option for testing this change, in its current form.

This is because HookedSocket: Socket overrides Socket.setOption but the filtering is (and was before this PR, too) happening in BaseSocket.setOption.

At this stage I'll remove the check for the skipping of this option for unix domain sockets. The feature of skipping for unix domain sockets isn't new in this PR and there wasn't a test for it already, and it's not clear how to test it.

I can keep the update to the test that checks that we do automatically add TCP_NODELAY on AF_INET6 sockets to give us confidence I didn't regress that it should be enabled for both AF_INET and AF_INET6 sockets, and the existing test was just for AF_INET. That being said, the test will be skipped on hosts without IPv6 support anyway.

@Lukasa Lukasa merged commit 1de708f into apple:main Jul 21, 2023
6 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
patch-version-bump-only For PRs that when merged will only cause a bump of the patch version, ie. 1.0.x -> 1.0.(x+1)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants