Skip to content

fix(socket): SO_REUSEADDR on UDP for post-suspend rebind - #121

Merged
got3nks merged 1 commit into
amule-org:masterfrom
got3nks:fix/udp-reuseaddr-after-suspend
Jun 13, 2026
Merged

fix(socket): SO_REUSEADDR on UDP for post-suspend rebind#121
got3nks merged 1 commit into
amule-org:masterfrom
got3nks:fix/udp-reuseaddr-after-suspend

Conversation

@got3nks

@got3nks got3nks commented Jun 12, 2026

Copy link
Copy Markdown

Summary

Refs #103. Fixes a Kad / ed2k-client-UDP wedge after suspend/resume on Linux (reported by @demanuel against the Flatpak prerelease).

Root cause

CMuleUDPSocket::OnReceive drives a DestroySocket() + CreateSocket() self-recovery cycle whenever a read callback returns an error or !IsOk(). On Linux, after systemd-suspend returns, the first post-resume read sometimes delivers a transient error that hits this path. The destroy step closes the previous fd, but the kernel can still consider the binding live for a short window, so the immediate rebind fails:

Error creating UDP socket 0.0.0.0 4672 : Address already in use
MuleUDPSocket: Failed to create valid Client UDP-Socket

With the rebind failing, Kad never gets a working UDP socket back ("Kad: Connecting" forever) and ed2k client UDP is broken until the user fully restarts amule. demanuel's log + ss -tan | grep :4661 (empty) at #103 ruled out the earlier TCP-stuck-dead hypothesis and pointed straight at the UDP rebind path.

Fix

Set SO_REUSEADDR on the UDP socket between open() and bind() in CAsioUDPSocketImpl::CreateSocket. The TCP acceptor path at LibSocketAsio.cpp:995 already sets reuse_address(true) for the equivalent reason; this is the same fix applied to the UDP path.

No behavioural change for the cold-start case — the option only matters when there's a previous binding the kernel hasn't fully released yet.

Status

Draft. Pending real-world confirmation from @demanuel that a suspend/resume cycle no longer wedges Kad. Packaging artifacts will be built from this branch on the fork (got3nks/amule) so demanuel can download a Flatpak/AppImage/Windows zip without building from source.

Test plan

  • macOS build clean.
  • Linux suspend/resume reproduction by reporter.
  • CI build matrix on this PR.

After resume from suspend, CMuleUDPSocket::OnReceive's read callback
sometimes delivers an error (or sees !m_socket->IsOk()) and drives a
DestroySocket() + CreateSocket() cycle to recover. The destroy step
closes the previous fd, but on some kernels the binding is still
considered live for a short window, so the immediate rebind in the
new socket hits EADDRINUSE:

  Error creating UDP socket 0.0.0.0 4672 : Address already in use
  MuleUDPSocket: Failed to create valid Client UDP-Socket

With the rebind failing, Kad never gets a working UDP socket back
("Kad: Connecting" forever) and ed2k client UDP is broken until the
user fully restarts amule.

Set SO_REUSEADDR on the UDP socket between open() and bind() so the
close-then-bind sequence is permitted by the kernel. The TCP acceptor
path already sets reuse_address(true) for the same reason; this just
applies the same option to the UDP path.

Refs amule-org#103.
@got3nks
got3nks marked this pull request as ready for review June 13, 2026 12:38
@got3nks
got3nks merged commit 4be99ad into amule-org:master Jun 13, 2026
10 checks passed
@got3nks
got3nks deleted the fix/udp-reuseaddr-after-suspend branch June 13, 2026 12:42
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.

1 participant