Skip to content

fix(server): stop dns-sd when the server process dies - #577

Merged
linroid merged 3 commits into
mainfrom
fix/mdns-stale-registration
Oct 17, 2026
Merged

linroid merged 3 commits into
mainfrom
fix/mdns-stale-registration

Conversation

@linroid

@linroid linroid commented Oct 17, 2026

Copy link
Copy Markdown
Owner

Problem

On macOS, NativeMdnsRegistrar started dns-sd -R as a plain child process and stopped it only in unregister(). When the server process was killed with kill -9 or crashed, no hook ran and dns-sd kept advertising a dead _ketch._tcp service. 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 restarted ketch server with kill -9.

Fix

dns-sd now runs under a small /bin/sh wrapper (stdinBoundCommand) that stops it once the wrapper's stdin pipe from the server reaches EOF:

trap '' INT QUIT
"$@" &
child=$!
trap 'kill "$child" 2>/dev/null; exit 143' HUP TERM
read -r _
kill "$child" 2>/dev/null
wait "$child"
  • Any exit of the server (kill -9, a crash, a normal exit) closes the pipe, so dns-sd gets SIGTERM. mDNSResponder withdraws the registration when its client goes away.
  • unregister() closes the pipe and waits up to 2 s for the wrapper (and so dns-sd) to exit. A register() 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 under NonCancellable, because KetchServer calls it in the finally of a cancelled coroutine.
  • The wrapper also stops dns-sd on SIGTERM/SIGHUP. It ignores SIGINT/SIGQUIT, as dns-sd already 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 orphan dns-sd. With it, the end is left to the server's exit.
  • Arguments go in as positional parameters, so the shell never parses service names or TXT values.
  • dns-sd's output goes to /dev/null. Nothing read it before either.

I chose this over registering through DNSServiceRegister via JNA. library:server has no JNA dependency, and the ketch server GraalVM native binary would need extra reflection metadata for it.

Tests

NativeMdnsRegistrarTest runs the wrapper with sleep standing in for dns-sd. It needs only /bin/sh, so it runs on the Linux CI (dash) as well as macOS:

  • a SIGKILLed process holding the pipe (ProcessBuilder.startPipeline, so the JVM holds no copy) stops the command
  • closing stdin (the unregister path) stops it
  • SIGTERM to the shell stops it
  • SIGINT leaves it running until stdin closes
  • shell metacharacters in arguments reach the command verbatim

A mutation check confirmed the tests catch a broken wrapper: removing the kill fails 3 of the 5 tests. ./gradlew :library:server:test passes.

Also checked by hand on macOS against the real dns-sd, under dash, bash and /bin/sh. A test service registered behind a sleep pipe holder disappeared from dns-sd -B once the holder was killed with kill -9.

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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-17T15:27:07.163254Z 6ad80f4 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@claude

claude Bot commented Oct 17, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@linroid
linroid merged commit 1ad0e33 into main Oct 17, 2026
12 checks passed
@linroid
linroid deleted the fix/mdns-stale-registration branch October 17, 2026 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant