GUACAMOLE-1625: Close sockets inherited by connection processes. - #698
Merged
Conversation
Contributor
Contributor
|
I'm concerned about the potential performance impact of looping through all file descriptors at connection start. Would |
Author
Yeah, fair point. I'll push an update soon. |
Author
added 2 commits
August 6, 2026 10:48
Connection processes inherit every descriptor open in the parent, as guacd_create_proc() closes only its own end of the socketpair it shares with the new child. For a busy parent, this means the parent's end of every other connection's user socketpair, the parent's end of every other connection process' socketpair, and the socket guacd listens on. FD_CLOEXEC cannot help, as connection processes are forked but never exec'd. Those inherited descriptors keep unrelated sockets referenced after the parent has closed them, so their peers never observe EOF. A write which should fail with EPIPE instead blocks forever, and that blocked write is performed while holding a read lock on the client's list of users, so guac_client_remove_user() can never acquire the corresponding write lock. The count of connected users therefore never reaches zero, guacd_proc_stop() is never called, and the process never exits. The effect compounds, as the more connections are open, the more descriptors each new process inherits. A parent with 469 such processes was observed holding tens of gigabytes, with descriptor counts rising from 98 in the oldest process to 756 in the youngest, and a single socket held by 40 of 40 sampled processes. Close inherited sockets within the newly-forked process. Only sockets are closed, as descriptors for anything else may be held by libraries unaware that they are being closed, and reusing such a number for a later socket would silently redirect their reads and writes.
Testing each descriptor individually costs a syscall per descriptor, which close_range() avoids entirely. Where it is unavailable, the cost is bounded by closing only those descriptors which are actually open, as the limit on the number of descriptors may be orders of magnitude greater than the number in use. A C library may lack a wrapper for close_range() while the kernel still provides the system call, in which case it is invoked directly. That is the case for musl, and thus for the guacd Docker image. Descriptors are no longer tested to determine whether they refer to sockets. Every descriptor inherited here is a socket, and neither libuuid nor OpenSSL retains one across the fork, both obtaining randomness through getrandom().
mike-jumper
approved these changes
Aug 6, 2026
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.


Connection processes inherit every descriptor open in the parent, as guacd_create_proc() closes only its own end of the socketpair it shares with the new child. For a busy parent, this means the parent's end of every other connection's user socketpair, the parent's end of every other connection process' socketpair, and the socket guacd listens on. FD_CLOEXEC cannot help, as connection processes are forked but never exec'd.
Those inherited descriptors keep unrelated sockets referenced after the parent has closed them, so their peers never observe EOF. A write which should fail with EPIPE instead blocks forever, and that blocked write is performed while holding a read lock on the client's list of users, so guac_client_remove_user() can never acquire the corresponding write lock. The count of connected users therefore never reaches zero, guacd_proc_stop() is never called, and the process never exits.
The effect compounds, as the more connections are open, the more descriptors each new process inherits. A parent with 469 such processes was observed holding tens of gigabytes, with descriptor counts rising from 98 in the oldest process to 756 in the youngest, and a single socket held by 40 of 40 sampled processes.
Close inherited sockets within the newly-forked process. Only sockets are closed, as descriptors for anything else may be held by libraries unaware that they are being closed, and reusing such a number for a later socket would silently redirect their reads and writes.