Repository navigation
Fix relay scheduler cleanup between runs - #46
Merged
sjanusz-r7 merged 2 commits intoOct 14, 2026
Merged
Conversation
Signed-off-by: mjatin2007 <mjatin2007@users.noreply.github.com>
5 of 11 tasks
mjatin2007
added a commit
to mjatin2007/rex-core
that referenced
this pull request
Oct 13, 2026
Signed-off-by: mjatin2007 <mjatin2007@users.noreply.github.com>
Signed-off-by: mjatin2007 <mjatin2007@users.noreply.github.com>
Contributor
|
Thanks for this! This PR paired with the linked Metasploit Framework PR resulted in no leaking sockets and no leaking file descriptors, tested using I can merge this in, rex-core would then be bumped automatically. You can then bump the gem in the original framework PR, and we can get the original issue closed. Thank you! |
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.
Description
RelayManagercurrently callsFiberScheduler#reset!whenever its worker exits. The scheduler is closed first, butreset!immediately allocates a new signaling pipe and leaves both descriptors attached to the idle manager until garbage collection. In the Metasploit MSSQL connection path, repeated logins can therefore retain descriptors after the TCP socket, channel socket pair, and relay worker have closed; the increase per login depends on whether the connection path creates one or more idle managers.This change leaves the signaling pipe unallocated after reset and recreates it lazily when new work is scheduled. It starts an idle worker behind a gate before accepting the first relay, then serializes relay submission with the worker's pending-work and teardown decision. An external submission waits once teardown commits, while a callback already running on the worker can submit follow-up work without blocking.
FiberScheduler#closepreserves the first work error, drains remaining accepted work, and releases the pipe in anensurepath.Related to rapid7/metasploit-framework#22002.
Verification
rex-coresuite: 127 examples, 0 failures. The final 18-job matrix passed on Ruby 3.2, 3.3, and 3.4 across Ubuntu 22.04, Ubuntu 24.04,ubuntu-latest, Windows Server 2022, Windows Server 2025, and Intel macOS 15. The suite also passed natively on Apple Silicon macOS 26.6.1 with Ruby 3.3.12.349fe64increased/dev/fdfrom 7 to 27; this candidate remained at 7. One hundred sequential reuses and 50 four-thread submission groups also remained at their candidate baselines.rex-core0.1.36 from 7 to 17 descriptors, followed by five successful-login cleanups to 27; this candidate remained at 7 throughout. Every connection negotiated TLS 1.2 withECDHE-RSA-AES128-GCM-SHA256, and successful sessions reportedencrypt_option = TRUE.TDSENCRYPTION=false: the serverENCRYPT_REQreconnect path changed the observed client state to encrypted. Five wrong-password attempts increased the 0.1.36 control from 7 to 27 descriptors, followed by five successful-login cleanups to 47; this candidate remained at 7 throughout.Both live SQL comparisons ran with garbage collection disabled for the released control and candidate. They used SQL Server 2022 in a Linux container and a Linux arm64 Ruby 3.3.8 client. The native macOS resource test exercised
rex-coredirectly. Windows validation reported here consists of the completedrex-coresuite, not a process-handle probe. No live SQL test ran from macOS or Windows. The self-signed SQL Server certificate validates TLS negotiation and cleanup, not certificate-authentication behavior. I have not reproduced the reporter's exact Metasploit/macOS environment, exercised NTLM or Kerberos, run the whole Metasploit suite, or established that this mechanism accounts for every descriptor in the reported 12-to-24 change.Reviewer Notes
add_relayreleases a newly created worker's gate in anensure, including when lazy pipe creation or scheduling raises; the worker can therefore finish teardown and the manager remains reusable.unblockreturningfalsewhen the signaling pipe is nil relies on the invariant that nil means the scheduler is reset/closed with no live blocked fiber. Close intentionally drains accepted work before propagating its first error; work that continually reschedules itself can therefore delay close.FiberScheduler#closepreserves the first error observed within that drain, rather than the earlier superseded worker error.AI Usage Disclosure
OpenAI Codex coordinated source analysis, drafted code and tests, and ran the validation. The contributor reviewed the public changes before publication.