A lightweight Windows service that cleanly closes selected TCP connections during system shutdown.
When Windows shuts down, some TCP connections to servers like Samba aren't properly closed. The server sees a "ghost" session that persists until it times out.
This service registers for the Windows SERVICE_CONTROL_PRESHUTDOWN notification and forcefully closes matching TCP connections by deleting them from the TCP connection table via the SetTcpEntry API. The server immediately sees the disconnect and cleans up the session.
Place shutdown-conn-closer.conf next to the executable. Each line is a rule in host:port format, where either side can be a wildcard *:
# Close all SMB connections
*:445
# Close all connections to a specific host
192.168.1.10:*
# Close only SMB to a specific host
192.168.1.10:445
Lines starting with # are comments.
shutdown-conn-closer.exe --install
sc start ShutdownConnClosershutdown-conn-closer.exe --uninstallRun in test mode to see which connections would be closed without actually closing them:
shutdown-conn-closer.exe --testcargo install cargo-xwin
rustup target add x86_64-pc-windows-msvc
cargo xwin build --target x86_64-pc-windows-msvc --releasesudo apt install mingw-w64
rustup target add x86_64-pc-windows-gnu
cargo build --target x86_64-pc-windows-gnu --releaseNote: GNU-linked binaries may trigger antivirus false positives. MSVC is recommended.
cargo build --releaseThe service writes to shutdown-conn-closer.log next to the executable.
- The service runs as LocalSystem which has privileges to close any TCP connection.
- The preshutdown timeout is set to 30 seconds. In practice it finishes in under a second.
- With Fast Startup enabled, the service only receives notifications during full shutdown (
shutdown /s /f /t 0) or restart — not during hybrid shutdown via the Start menu.