fix(socket): SO_REUSEADDR on UDP for post-suspend rebind - #121
Merged
got3nks merged 1 commit intoJun 13, 2026
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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::OnReceivedrives aDestroySocket()+CreateSocket()self-recovery cycle whenever a read callback returns an error or!IsOk(). On Linux, aftersystemd-suspendreturns, 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: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_REUSEADDRon the UDP socket betweenopen()andbind()inCAsioUDPSocketImpl::CreateSocket. The TCP acceptor path at LibSocketAsio.cpp:995 already setsreuse_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