Skip to content

fix(libkernel): report the wildcard local address after a UDP association is dissolved - #8682

Closed
xXPBSXx wants to merge 2 commits into
boykopovar:mainfrom
xXPBSXx:fix/udp-disconnect-clears-local-address
Closed

xXPBSXx wants to merge 2 commits into
boykopovar:mainfrom
xXPBSXx:fix/udp-disconnect-clears-local-address

Conversation

@xXPBSXx

@xXPBSXx xXPBSXx commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

What

udp_disconnect resets the local address to INADDR_ANY and keeps the port. Dissolving the association with connect to AF_UNSPEC left the host socket's local address in place, so getsockname still returned 127.0.0.1 (Windows; Linux for a socket bound to an explicit port). The socket now remembers that its association was dissolved and getsockname reports the wildcard address (IPv4 and IPv6) until the next bind or connect. The host socket itself is unchanged; the Technical debt entry for connect to AF_UNSPEC says so. guest_udp runs the check on Windows now.
Fixes #8681
Overlap: git merge-tree has conflicts with #8499 (two), #8448 (one) and #7511 (one) in Udp.cpp; the other open socket PRs (#8670, #8492, #8483, #8474, #8463, #7850, #7090) are clean. Whichever lands second rebases. pr_overlap was not run.

Tested

Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, on current upstream/main + this commit: guest_udp fails without the change to Udp.cpp and passes with it; 10 other socket tests pass. python tools/check_conventions.py --base upstream/main: passes. Not tested: Linux/macOS, the IPv6 path, a console.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw; no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

Overlap note: #8344 adds independent Socket::peer tracking (socketpair getpeername) in Udp.cpp; this PR adds Socket::localAddressCleared. No behavior duplicate, but the Socket member hunks must be kept together if both land.

…tion is dissolved

udp_disconnect resets the local address to INADDR_ANY and keeps the port.
Dissolving the association with connect to AF_UNSPEC left the host socket's
local address in place, so getsockname still returned 127.0.0.1 on Windows.
The socket remembers that its association was dissolved and getsockname
reports the wildcard address until the next bind or connect. guest_udp now
checks that on Windows too.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The stored flag and getsockname adjustment cover both AF_INET and AF_INET6, and the description says the wildcard address is reported for both families. The new regression only exercises an IPv4 socket, so the IPv6 branch currently has no coverage (the description also notes it was not tested). Could you add an IPv6 case where the host supports it, checking the address becomes :: while the bound port is preserved after connect(AF_UNSPEC)?

@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

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.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 7511 docs/dev/debt/libkernel.md L201-202
PR 8344 core/libs/prx/libkernel/Socket/src/Udp.cpp L120
PR 9105 core/libs/prx/libkernel/Socket/src/Udp.cpp L300-310
docs/dev/debt/libkernel.md L202-203
PR 9108 core/libs/prx/libkernel/Socket/src/Udp.cpp L1270-1271
PR 9169 core/libs/prx/libkernel/Socket/src/Udp.cpp L1270-1271

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.

Pull request Shared files not compared
PR 4630 docs/dev/debt/libkernel.md
PR 5021 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 6044 core/libs/tests/GuestUdp.cpp
PR 6149 docs/dev/debt/libkernel.md
PR 6469 docs/dev/debt/libkernel.md
PR 6740 docs/dev/debt/libkernel.md
PR 6879 docs/dev/debt/libkernel.md
PR 6936 docs/dev/debt/libkernel.md
PR 7105 docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7192 docs/dev/debt/libkernel.md
PR 7201 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 7447 docs/dev/debt/libkernel.md
PR 7509 docs/dev/debt/libkernel.md
PR 7653 docs/dev/debt/libkernel.md
PR 8520 docs/dev/debt/libkernel.md
PR 8547 docs/dev/debt/libkernel.md
PR 8726 docs/dev/debt/libkernel.md
PR 9099 docs/dev/debt/libkernel.md
PR 9137 docs/dev/debt/libkernel.md

… is dissolved

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@xXPBSXx

xXPBSXx commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @MarcZX10: added the IPv6 case in f6d8cca. After connect(AF_UNSPEC) on an AF_INET6 socket bound to ::1, getsockname keeps the port and reports ::; the test skips hosts without IPv6 or ::1. With only the IPv6 branch of the fix removed, guest_udp fails on Windows; with it, it passes. Not run on Linux/macOS.

AI-assisted (Claude Code).

@github-actions github-actions Bot added the fix error Closes an issue (Fixes #N) label Oct 20, 2026

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The current head f6d8cca311 records the cleared local-address state for both IPv4 and IPv6, then reports the wildcard address while preserving the host-assigned port. The new IPv6 regression checks the port and ::, and the author reports Windows failure when only the IPv6 fix is removed and a pass with it. Hosted build/runtime jobs are path-skipped, so I am relying on the reported Windows run; Linux/macOS remain untested. I also left a note about the CMake-style merge-order concern in the UDP implementation: #8344 adds an independent Socket::peer member in the same struct and should be included in the overlap notes.

@MarcZX10

Copy link
Copy Markdown
Contributor

One overlap entry appears to be missing from the PR description: the overlap bot flags #8344 on Udp.cpp. I checked that diff; it adds independent Socket::peer tracking for socketpair getpeername, while this PR adds localAddressCleared. There is no behavior duplicate, but the Socket member hunks need to be retained together if both land. Please include #8344 in the manual merge-order notes.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@xXPBSXx Thanks. The FreeBSD reference (udp_disconnect resets the local address and keeps the port) is right, and the Windows test coverage is useful.

One concern. When the port is non-zero, Disconnect leaves the host socket bound where it was (e.g. 127.0.0.1:port), and the PR only changes what getsockname reports. After an AF_UNSPEC disconnect, a guest that reads getsockname, sees the wildcard address, and then sendtos a non-loopback destination still gets the host's loopback-bound failure, while getsockname said it could. The project's rule is that a call does what it reports or throws, and a reported state the host doesn't have is harder to diagnose than the current answer.

Two options. (1) Actually move the host socket to the wildcard address: for example, create a new host UDP socket, bind it to wildcard:port, copy the stored options, and swap it in under the socket's lock. (2) If a title needs only the getsockname value, name the title and the call sequence, and keep the debt note explicit that sends still use the old address. Which title reads the local address after dissolving the association?

@xXPBSXx

xXPBSXx commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

@oneandonlydean you are right, and I measured it before answering. Probe (plain Winsock, Windows 10 Pro 19045, WinLibs GCC 15.2.0): an UDP socket connected to a loopback peer, then connect(AF_UNSPEC), then getsockname, then sendto to this machine's LAN address:

socket before the disconnect host getsockname after it sendto to the LAN address
bound implicitly (the first connect picked the address) 0.0.0.0:<port> succeeds (1 byte)
explicitly bound to 127.0.0.1 127.0.0.1:<port> fails, WSAENETUNREACH (10051)

So the masking only changes the answer for the explicitly bound case, and there it reports a state the host does not have: exactly the failure mode you describe. (The implicit case already reports the wildcard address on main.)

Which title reads the local address after dissolving the association? I do not know of one. I found this by comparing against in_pcbdisconnect, not from a title run, and I cannot name a title or call sequence. So I will not claim option 2.

Option 1 is not something I want to land blind. The host handle in Socket::value is read without a lock in about 50 places (recv, poll, send), and the socket options are set directly on the native handle, so a new socket would need every option the guest set replayed (broadcast, buffer sizes, multicast memberships, TTL/TOS, nonblocking) and a bind of the same port while the old socket still owns it. A guest thread blocked in recvfrom on the old handle would also fail. That is a much larger change than this PR, with no title behind it.

Proposal: I close this PR rather than leave a reported state without a host counterpart. If you prefer to keep a record, I can open a docs-only PR that adds the divergence to the libkernel debt file (after AF_UNSPEC, an explicitly bound UDP socket keeps its bound address; FreeBSD resets it to the wildcard), with the table above as evidence. Tell me which one you want, or if you want option 1 anyway I will do it as a separate PR with a sendto non-loopback test.

AI-assisted (Claude Code).

@xXPBSXx

xXPBSXx commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

Two corrections to my reply above:

  • The reset of the local address to the wildcard is in FreeBSD 11.4's udp_disconnect (udp_usrreq.c), not in in_pcbdisconnect (which only clears the foreign address and port). I named the wrong function.
  • The first row of the table (implicitly bound socket) is what the host's Winsock reports in the probe, not a run of main; only the explicitly bound row is the case this PR changes. I will attach the probe output as a log from a fresh run and correct the table if it differs.

AI-assisted (Claude Code).

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@xXPBSXx Thanks for measuring it. The table settles it: the masking changes the answer only for the explicitly bound socket, and there the host is still on 127.0.0.1. For an implicitly bound socket, main already passes the host's 0.0.0.0:<port> through getsockname.

So yes, please close this one, and the docs-only PR is welcome. Put it in docs/dev/debt/libkernel.md: after connect(AF_UNSPEC), a UDP socket explicitly bound to an address keeps that address (and getsockname reports it), where FreeBSD's udp_disconnect resets it to the wildcard and keeps the port. Say it was found by comparing against in_pcbdisconnect, not on a title, and link your probe table.

We don't want option 1 without a title: replaying every stored option and swapping the handle under concurrent recvfrom/send is a lot of risk for a value no known title reads. If a title turns up that needs it, the sendto non-loopback test you describe is the right place to start.

@adriadam10

Copy link
Copy Markdown
Collaborator

Closing as agreed with @oneandonlydean above: the PR makes getsockname report the wildcard address after connect(AF_UNSPEC) on an explicitly bound UDP socket, but the host socket stays bound to that address, so a reported state has no host counterpart and no known title needs it. Swapping the host handle is too risky without a title.

A docs-only PR is welcome: add to docs/dev/debt/libkernel.md that after connect(AF_UNSPEC) an explicitly bound UDP socket keeps its address (and getsockname reports it), where FreeBSD's udp_disconnect resets it to the wildcard and keeps the port, found by comparing against FreeBSD and not on a title, with the probe table linked. Feel free to reopen if a title turns up that needs it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs fix error Closes an issue (Fixes #N) libkernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

libkernel: getsockname still returns the old local address after a UDP association is dissolved

4 participants