Skip to content

std.posix: unlock nonblocking connect() use case #24002

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

iamtimmy
Copy link

@iamtimmy iamtimmy commented May 27, 2025

Match Windows WSAEALREADY with Linux EALREADY
return gracefully when socket reports it is connected

for easy reference the msdn page and the man page.

The msdn page also says that WSAEINVAL should be handled the same due to ambiguities. The WSAConnect api doesn't mention the same in the documentation. Is that a reason to change to WSAConnect?

@alexrp alexrp requested a review from squeek502 May 27, 2025 22:22
Copy link
Collaborator

@squeek502 squeek502 left a comment

Choose a reason for hiding this comment

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

Is it feasible to add a test for what this fixes in std/posix/test.zig?

@iamtimmy

This comment was marked as outdated.

@squeek502
Copy link
Collaborator

squeek502 commented May 28, 2025

Ah, the tests in net/test.zig might be a better reference (and maybe the added test should be put there instead?):

Actually, there might be an existing (but disabled, see #18315) test in place that might be fixed with your changes:

zig/lib/std/net/test.zig

Lines 380 to 406 in 4f3b59f

test "non-blocking tcp server" {
if (builtin.os.tag == .wasi) return error.SkipZigTest;
if (true) {
// https://github.com/ziglang/zig/issues/18315
return error.SkipZigTest;
}
const localhost = try net.Address.parseIp("127.0.0.1", 0);
var server = localhost.listen(.{ .force_nonblocking = true });
defer server.deinit();
const accept_err = server.accept();
try testing.expectError(error.WouldBlock, accept_err);
const socket_file = try net.tcpConnectToAddress(server.listen_address);
defer socket_file.close();
var client = try server.accept();
defer client.stream.close();
const stream = client.stream.writer();
try stream.print("hello from server\n", .{});
var buf: [100]u8 = undefined;
const len = try socket_file.read(&buf);
const msg = buf[0..len];
try testing.expect(mem.eql(u8, msg, "hello from server\n"));
}

(would need to be verified that it'll no longer be flaky, though; ideally that'd be something like reproducing the failure, making the failure consistent if possible, and then showing that the changes fix the failure)

@iamtimmy
Copy link
Author

I moved the test to test/net.zig and based on that other non-blocking tcp server test I managed to complete the test for this pr.

I think the issue with the non-blocking tcp server test might be related to a specific MacOS case in accept

@iamtimmy
Copy link
Author

iamtimmy commented May 29, 2025

CI Failed. Turns out Linux and BSD refuse the connection to a nonexistent socket...
I fixed the formatting and made that part of the test only run on Windows.

Any ideas for a scenario that works on mac and linux?

@@ -4274,6 +4274,7 @@ pub fn connect(sock: socket_t, sock_addr: *const sockaddr, len: socklen_t) Conne
const rc = windows.ws2_32.connect(sock, sock_addr, @intCast(len));
if (rc == 0) return;
switch (windows.ws2_32.WSAGetLastError()) {
.WSAEISCONN => return,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a reason WSAEISCONN/ISCONN shouldn't return an error (something like error.IsConnected)? Is there no use case for distinguishing between success and WSAEISCONN/ISCONN?

Copy link
Author

Choose a reason for hiding this comment

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

I don't know of one, I opted for this because it makes total sense to me. Could opt for an error as well.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Will need to wait for a Zig core team member to weigh in.

Match Windows `WSAEALREADY` with Linux `EALREADY`
return gracefully when socket reports it is connected
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.

2 participants