fix: prevent IPC socket fd leak into child processes - #388
Open
Anntoin wants to merge 1 commit into
Open
Conversation
Anntoin
force-pushed
the
fix/socket-cloexec
branch
5 times, most recently
from
August 10, 2026 14:14
d9fc1d3 to
f048c13
Compare
Use SOCK_CLOEXEC on all AF_UNIX sockets (listen, accept, connect) so that the @keymapperctl abstract socket fd is not inherited by child processes spawned via $(...) config commands (e.g. kitty, tofi). Previously, the inherited fd kept listening on the socket, preventing a restarted keymapper from rebinding it — resulting in 'Binding control port failed' errors and keybindings silently failing until the leaked processes were manually killed. In accept(), use accept4() with SOCK_CLOEXEC when available (detected via __GLIBC_PREREQ(2,10) for glibc; musl provides it without any feature-test macro) and fall back to accept() + fcntl(FD_CLOEXEC) otherwise, matching the existing platform split used for abstract socket addresses in this file. Assisted-by: Hermes:glm-5.2 [gh]
Anntoin
force-pushed
the
fix/socket-cloexec
branch
from
August 10, 2026 14:17
f048c13 to
07c3f12
Compare
Anntoin
marked this pull request as ready for review
August 10, 2026 14:22
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.
Problem
Keymapper creates an abstract Unix domain socket (
@keymapperctl) for IPC between the client and daemon. This socket fd is inherited by child processes spawned via$(...)config commands (e.g.kitty,tofi,swaymsg).These children keep the socket fd open and listening. When keymapper is restarted (e.g. by a service manager, or config reload), the new process cannot bind
@keymapperctlbecause the leaked processes still hold it. This producesBinding control port failederrors and keybindings stop working until all the leaked processes are manually killed.Root Cause
All
socket()calls inHost.cppuse plainSOCK_STREAMwithoutSOCK_CLOEXEC, so the fd survivesexec()into child processes.Fix
Add
SOCK_CLOEXECto all Unix socket calls inHost.cpp:listen():socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)accept(): useaccept4()withSOCK_CLOEXECwhen available (detected via__GLIBC_PREREQ(2,10)for glibc; musl provides it without any feature-test macro), fall back toaccept()+fcntl(FD_CLOEXEC)otherwiseconnect():SOCK_CLOEXECon both initial and retrysocket()callsTesting
accept4andSOCK_CLOEXECpresent in the built binary viaobjdump/nm -Dss -xlpoutput for@keymapperctlafter keymapper restartBinding control port failedCompatibility
accept4availability is detected via__GLIBC_PREREQ(2,10)for glibc. On musl it is available without_GNU_SOURCE. Other platforms (macOS, BSD) fall back toaccept()+fcntl(FD_CLOEXEC).CLOEXEConly affectsexec(), not normal fd lifetime.