Skip to content

DnsServer: Delete stale Unix socket files before binding to prevent 'address already in use' errors on restart#2024

Open
IngmarStein wants to merge 1 commit into
TechnitiumSoftware:developfrom
IngmarStein:fix/unix-socket-startup
Open

DnsServer: Delete stale Unix socket files before binding to prevent 'address already in use' errors on restart#2024
IngmarStein wants to merge 1 commit into
TechnitiumSoftware:developfrom
IngmarStein:fix/unix-socket-startup

Conversation

@IngmarStein

Copy link
Copy Markdown
Contributor

When the DNS server is restarted, the Unix domain socket files from the previous run persist on disk. Kestrel's ListenUnixSocket() then fails with:

System.IO.IOException: Failed to bind to address http://unix:<path>: address already in use.
 ---> System.Net.Sockets.SocketException (48): Address already in use

This PR deletes the socket file before calling ListenUnixSocket() so that a stale file from a previous run does not prevent binding. DirectoryNotFoundException is silently ignored since it means the parent directory does not exist yet, and ListenUnixSocket will fail with a clearer error in that case.

Affected:

  • Web Service Unix socket in DnsWebService.cs
  • DNS-over-HTTPS Unix socket in DnsServer.cs

When the DNS server is restarted, Unix domain socket files from the
previous run persist on disk. Kestrel's ListenUnixSocket() then fails:

  System.IO.IOException: Failed to bind to address http://unix:<path>:
  address already in use.
  ---> System.Net.Sockets.SocketException (48): Address already in use

Delete the socket file before calling ListenUnixSocket() so that a stale
file from a previous run does not prevent binding. DirectoryNotFoundException
is silently ignored since it means the parent directory does not exist yet.

Also clean up socket files in StopWebServiceAsync() and StopDoHAsync() after
disposing the Kestrel host, as defense-in-depth for clean shutdowns.
@IngmarStein IngmarStein force-pushed the fix/unix-socket-startup branch from 16d4995 to cbb8180 Compare July 6, 2026 22:18
@ShreyasZare

Copy link
Copy Markdown
Member

Thanks for the PR. In my tests on both Linux and Windows, the socket file that gets created by Kestrel server gets removed too when the server stops. So not sure how you see the stale file there. Do you have any steps to reproduce this?

@IngmarStein

Copy link
Copy Markdown
Contributor Author

It happened to me every time I run brew services restart technitium-dns where launchd sends a SIGINT to the current process and restarts it. Maybe it's platform specific (no macOS). If we want to minimize the PR, we could leave the cleanup to just when ListenUnixSocket is about to be called as a safety net - it will fail if the file already exists.

@Apachez-

Apachez- commented Jul 7, 2026

Copy link
Copy Markdown

Always healthy to clean up such files before use since only a clean shutdown will properly remove them by itself.

If you had a crash or the host (when runned as container) suddently pulled the rug under the application stale files will remain and hinder a proper restart.

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.

3 participants