Repository navigation
fix(libkernel): clear reported local address after UDP disconnect - #12467
Conversation
…d on macOS On macOS, connect to AF_UNSPEC on a connected datagram socket dissolves the association but returns -1, with EAFNOSUPPORT for IPv4 and EINVAL for IPv6. Disconnect took that as a failure, so connect to port 0 on a connected datagram socket failed with 47 instead of EADDRNOTAVAIL (49). Treat the call as successful when the socket is no longer connected. Co-authored-by: marion909 <103169397+marion909@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
rooklz
left a comment
There was a problem hiding this comment.
macOS evidence for the disconnect path (the body notes macOS was not run). This is a non-blocking note: the gap predates this PR, but it means the new CheckDissolvedName cases cannot pass on a macOS libs build.
Host primitive, macOS 26.2 arm64, plain C probe. Each case is a connected UDP socket followed by connect({sa_family = AF_UNSPEC}, sizeof(sockaddr)), which is the exact call Disconnect makes:
| family | binding | connect(AF_UNSPEC) |
getpeername after |
getsockname after |
|---|---|---|---|---|
| inet | implicit / 127.0.0.1:0 / 127.0.0.1:<port> |
-1, errno 47 | -1/57 (dissolved) | 0.0.0.0:<port> |
| inet6 | implicit / [::1]:0 / [::1]:<port> |
-1, errno 22 (len 16; len 28 gives 47) | -1/57 (dissolved) | [::]:<port> |
XNU therefore dissolves the association and also resets the local address to the wildcard while keeping the port, as FreeBSD's udp_disconnect does. It still reports failure. A later bind to the wildcard address fails with EINVAL on macOS and succeeds on Linux.
Disconnect / ZeroPortConnect copied verbatim from fb97e38f16 (and from main 17f8dfad51), with real host sockets and only NativeListening stubbed:
| host | path | guest rc / errno | localAddressCleared |
|---|---|---|---|
| Linux (ubuntu 24.04 arm64 container, g++) PR + main | AF_UNSPEC | 0 | PR: 1 |
| Linux | port 0 | -1 / 49 | PR: 1 |
| macOS (clang++) PR + main | inet AF_UNSPEC | -1 / 47 (correct only by coincidence: it is the host errno) | 0 |
| macOS | inet port 0 | -1 / 47 (expected 49) | 0 |
| macOS | inet6 AF_UNSPEC | -1 / 22 (expected 47) | 0 |
| macOS | inet6 port 0 | -1 / 22 (expected 49) | 0 |
On macOS the early return Fail(NativeError()) fires every time. The new flag is never set, so getsockname keeps whatever the host reports, and the port-0 disconnect returns the host errno instead of 49. Because XNU already reports the wildcard address there, only the return values are wrong on macOS today. Even so, CheckDissolvedName (connect(...) == -1 && errno == (portZero ? 49 : 47)) would fail on its inet port-0 iteration and on both inet6 iterations. The existing inet connect_nid_postfix(dissolved, unspecified) == -1 && errno == 47 passes only because the host errno happens to be 47. The macOS CI lanes do not build guest_udp, which is why nobody has seen this.
A one-line tolerance that gave the expected row on both hosts in the harness (macOS: all 4 rows 0 / 49 with cleared=1; Linux: unchanged):
- if (::connect(socket.value, &unspecified, sizeof(unspecified)) != 0) return Fail(NativeError());
+ if (::connect(socket.value, &unspecified, sizeof(unspecified)) != 0 && PeerConnected(socket)) return Fail(NativeError());PeerConnected already exists. The wildcard re-bind that follows only runs when the port reads 0, and on macOS the port is kept, so the EINVAL re-bind is never reached. Whether this belongs in this PR or a follow-up is your call. If it goes in a follow-up, a line in the AF_UNSPEC debt entry about the macOS return value would keep the record accurate.
I did not run the full libs build on macOS, only the copied functions, so this is a COMMENT and not a review verdict. The PR's own logic (clear on a successful connect/bind, shared through dup via the shared Socket) reads correctly against FreeBSD 11.4's udp_disconnect/in_pcbconnect.
MarcZX10
left a comment
There was a problem hiding this comment.
On macOS, the native connect(AF_UNSPEC) can dissolve a UDP association and reset the local address while still returning an error. The macOS 26.2 arm64 probes in the existing review comment reproduce this for both IPv4 and IPv6; the copied Disconnect path returns before setting localAddressCleared.
This also makes the new CheckDissolvedName cases fail on macOS: the port-zero case expects guest error 49, while XNU returns host errno 47 (IPv4) or 22 (IPv6); the IPv6 AF_UNSPEC case likewise receives 22 instead of guest error 47. The existing IPv4 AF_UNSPEC assertion passes only because the host errno happens to equal 47. These tests are currently omitted from the macOS CI lane, so the green hosted checks do not cover this.
Please account for the host's “disconnected but returned error” result (for example, distinguish it from a still-connected socket before returning the native error), preserve the guest error mapping, and run the UDP regression on macOS or add a focused platform-independent test for the mapping. If this is intentionally deferred, the PR should avoid asserting cross-platform behavior it does not provide and document the macOS gap in the debt entry.
|
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
Preserve the local port while reporting a wildcard address after a datagram association is dissolved. Share the state across descriptor aliases and clear it on a successful bind or reconnect. Adapted from the fix branch linked in issue boykopovar#8681, with expanded binding, reconnect and descriptor-alias regression coverage. Co-authored-by: xXPBSXx <20614131+xXPBSXx@users.noreply.github.com>
Keep native UDP bind, connect and disconnect operations coherent with the shared reported-address state. Reuse the existing connect lock and avoid additional locking on stream sockets. Add a controlled overlapping disconnect/reconnect regression and check truncated address writes after disconnection. Clear an obsolete IPv6 scope ID when reporting the wildcard address.
Reuse the macOS disconnect prerequisite in the common POSIX path. Proceed only when getpeername positively reports ENOTCONN, retaining the original connect error for connected or indeterminate sockets. Exercise actual UDP sockets with injected EAFNOSUPPORT and EINVAL results for IPv4 and IPv6, both disconnect entry points, all three binding modes, and still-connected or failed peer-query outcomes.
a73515e to
9a4d246
Compare
|
@MarcZX10 @rooklz The disconnect-result feedback is addressed in the current head, The PR now inherits #10544 as an explicit dependency. Its post-error verification is shared by the POSIX path: processing continues only when Added Validation on the updated branch: 14/14 focused CTest cases passed, plus 3/3 with AddressSanitizer and UBSan, with no sanitizer diagnostics. Conventions and test-registration audits passed. Native macOS hardware was unavailable; the fixture simulates the reported macOS error results on Linux and exercises the shared production implementation. The PR description records this limitation and the dependency. Could you please re-review the current head? Thank you for the probes and the review. |
MarcZX10
left a comment
There was a problem hiding this comment.
Re-checked — current head approved
The revised disconnect path handles POSIX hosts that dissolve a UDP association but return an error: it proceeds only when a follow-up getpeername proves the socket is disconnected, and otherwise preserves the original error. The new Linux regression exercises disconnected, still-connected, and indeterminate peer-query outcomes across the IPv4/IPv6 and binding cases. The synchronization changes also protect the shared address state during disconnect, bind, reconnect, and name lookup.
What I ran
- Reviewed current head
9a4d2462d3e5fe2fc38a49348bd1a45cf55458f3againstmain319621ba2e578bd053b0baba2877fe70520e0737. - Inspected the complete diff, including the common POSIX error-handling path and the new Linux-only integration test.
- Ran
git diff --check refs/review/base...refs/review/pr-12467. - Generated a merge preview with
git merge-tree --write-tree refs/review/base refs/review/pr-12467. - Refreshed hosted checks.
Results
| Check | Result |
|---|---|
git diff --check |
pass |
Merge preview against current main |
clean; tree 9e7ff9c0a4312277b793e0f6848d360ffdb5e286 |
| Hosted conventions, labels, eligibility, report | pass |
| Hosted Windows workflow | pass |
| Hosted Linux/product and specialized jobs | skipped |
| Focused Linux CTest and ASan/UBSan runs | author reports 14/14 CTest cases and 3/3 sanitizer tests passing; I did not independently rerun them |
Findings
Blocking
None found in the current diff.
Non-blocking
The Linux integration test exercises the common POSIX branch, but it does not replace a native macOS build/test run. The PR records that macOS was unavailable; the error-result behavior has been independently probed on macOS in an existing review comment.
Sources
- Disconnect and local-address handling at
9a4d2462d3 - UDP disconnect-result regression test at
9a4d2462d3 - macOS native-socket probe in the existing review thread
Not verified
I did not run a local C++ build, CTest, sanitizer run, native macOS or Windows test, or console test on this head. The hosted Windows workflow passed; hosted Linux/product jobs were skipped.
To get this to a good state
No blocking change requested. The exact-head diff, merge preview, hosted checks, and reported focused tests support approval.
|
Correction to the macOS probe source link in my review: the intended source is rooklz’s existing review. The review ID in my previous link was incorrect. |
|
Checked the disconnect and reported-name paths against the diff and FreeBSD 11.4 |
What
Correct the local address reported by
getsocknameafter a UDP association is dissolved, preserving the assigned port. The logical wildcard state is shared by duplicated descriptors and cleared on successful bind or reconnect. Native UDP address operations and their state updates use the existing socket mutex, so overlapping disconnects and reconnects cannot leave stale state. Stream calls retain main's locking. IPv6 wildcard reporting clears the obsolete scope ID.Depends on #10544 (
5cc1ea83ca93b66816eb1ce4211bd5cbe49d21db), included as an ancestor. Its macOS post-error disconnect verification is shared by POSIX hosts so the same production path can be tested on Linux. A native disconnect error is tolerated only whengetpeernameconfirmsENOTCONN. A still-connected socket or an indeterminate peer query preserves the original native error. Successful disconnection then follows the existing guest error mapping: 47 for a correctly sizedAF_UNSPECaddress, 49 for a port-zero destination. The Windows native disconnect branch is unchanged.The initial reported-name implementation is adapted from the branch linked in issue #8681, with the original author credited as co-author. Behavior references: FreeBSD 11.4
udp_disconnect,udp6_disconnectandin6_getsockaddr/in6_sockaddr. No new console measurement is claimed.Regression coverage includes IPv4/IPv6, implicit/port-zero/explicit-port bindings, descriptor aliases, reconnection, datagram delivery and truncated outputs with trailing sentinels. Linux integration fixtures cover overlapping native disconnect/reconnect operations and 72 host-result cases: injected
EAFNOSUPPORT/EINVALafter actual disconnection, genuine failure while still connected, and a failed post-error peer query, for both guest disconnect entry points and all binding modes. The latter fixture models the macOS return values while calling the shared production POSIX implementation.The debt entry retains the native-binding limitation: a host socket may keep its explicit local address, and the documented Windows
sendtofailure to non-loopback destinations remains.Tested
Debian 13 container on Fedora 44 x86-64, GCC 14.2.0, CMake 3.31.6, Ninja. Head
9a4d2462d3e5fe2fc38a49348bd1a45cf55458f3, based onmainb9eafb0c495d273e0bcf80aacc51350db6861ede; prerequisite #10544 is included as a merge parent.cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON; builtlibc,libkerneland the selected guest test executables.guest_udp,guest_udp_name_concurrency,guest_udp_disconnect_result,guest_socket_port_zero,guest_dup,guest_socket_pair,guest_recvmsg_memory,guest_socket_descriptor_flags,guest_socket_address_length,guest_socket_options,guest_socket_options_multicast,guest_socket_read_write,guest_socket_descriptors,guest_socket_connect_listening.a73515ead1'sUdp.cppagainst the same runtime objects and linked it into an isolated library. The newguest_udp_disconnect_resultregression aborts (CTest exit 8). The corrected implementation passes. Earlier negative controls also established the sequential reported-name and concurrent-transition regressions.guest_udp,guest_udp_name_concurrency,guest_udp_disconnect_result. A separate RelWithDebInfo build uses-DANYPS5_ENABLE_ASAN=ONand-DCMAKE_CXX_FLAGS="-fsanitize=undefined -fno-sanitize=vptr -fno-sanitize-recover=undefined", withASAN_OPTIONS=detect_leaks=0andUBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1.git diff --checkpassed.python3 tools/pr_overlap.py --branch fix/udp-disconnect-local-address --jobs 4fetched 1,355 open heads and compared the other 1,353 after excluding this head and its inherited prerequisite fix(libkernel): count a dissolved datagram association as disconnected on macOS #10544. No conflicts were reported among comparable heads. The tool could not compare 34 heads already conflicting withmain; manual review of their shared-file changes found no implementation of this behavior. fix(libkernel): count a dissolved datagram association as disconnected on macOS #10544 remains an explicit dependency.macOS hardware was not available. The macOS error-result mapping is covered by the Linux fault-injection fixture above; this is not a native macOS build or run. Windows, console hardware and games were not run. Only the selected runtime libraries and tests were built; no all-libraries build or hosted runtime-CI pass is claimed. The pre-existing unusable-pointer gap in
getsocknameremains separate work in PR #11281.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)