Repository navigation
fix(server): stop dns-sd when the server process dies - #577
Merged
Merged
Conversation
NativeMdnsRegistrar started dns-sd -R as a plain child and stopped it only in unregister(), so a server killed with kill -9 (or crashing) left dns-sd advertising a dead _ketch._tcp service, and a restarted server registered a second one. dns-sd now runs under a small /bin/sh wrapper that stops it once the wrapper's stdin pipe from the server ends. unregister() closes the pipe and waits for dns-sd to exit; any exit of the server process closes it too. The wrapper also stops dns-sd on SIGTERM and SIGHUP and ignores SIGINT, so a Ctrl-C leaves the end to the server's own exit.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
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
On macOS,
NativeMdnsRegistrarstarteddns-sd -Ras a plain child process and stopped it only inunregister(). When the server process was killed withkill -9or crashed, no hook ran anddns-sdkept advertising a dead_ketch._tcpservice. A restarted server then registered a second "Ketch", and LAN clients listed a stale device. This came up while field-testing the torrent engine (#570), where test agents restartedketch serverwithkill -9.Fix
dns-sdnow runs under a small/bin/shwrapper (stdinBoundCommand) that stops it once the wrapper's stdin pipe from the server reaches EOF:kill -9, a crash, a normal exit) closes the pipe, sodns-sdgetsSIGTERM. mDNSResponder withdraws the registration when its client goes away.unregister()closes the pipe and waits up to 2 s for the wrapper (and sodns-sd) to exit. Aregister()that follows therefore never finds the old registration still holding the name. If the wrapper does not exit in time, it falls back to killing the wrapper and its descendants. It runs underNonCancellable, becauseKetchServercalls it in thefinallyof a cancelled coroutine.dns-sdonSIGTERM/SIGHUP. It ignoresSIGINT/SIGQUIT, asdns-sdalready does: a shell without job control starts background commands with them ignored. Without that, a Ctrl-C in a terminal would kill the shell and orphandns-sd. With it, the end is left to the server's exit.dns-sd's output goes to/dev/null. Nothing read it before either.I chose this over registering through
DNSServiceRegistervia JNA.library:serverhas no JNA dependency, and theketch serverGraalVM native binary would need extra reflection metadata for it.Tests
NativeMdnsRegistrarTestruns the wrapper withsleepstanding in fordns-sd. It needs only/bin/sh, so it runs on the Linux CI (dash) as well as macOS:ProcessBuilder.startPipeline, so the JVM holds no copy) stops the commandunregisterpath) stops itSIGTERMto the shell stops itSIGINTleaves it running until stdin closesA mutation check confirmed the tests catch a broken wrapper: removing the
killfails 3 of the 5 tests../gradlew :library:server:testpasses.Also checked by hand on macOS against the real
dns-sd, under dash, bash and/bin/sh. A test service registered behind asleeppipe holder disappeared fromdns-sd -Bonce the holder was killed withkill -9.