Skip to content

Latest commit

 

History

History
85 lines (55 loc) · 2.16 KB

File metadata and controls

85 lines (55 loc) · 2.16 KB

Shutdown Connection Closer

A lightweight Windows service that cleanly closes selected TCP connections during system shutdown.

Problem

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.

Solution

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.

Configuration

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.

Installation

shutdown-conn-closer.exe --install
sc start ShutdownConnCloser

Uninstallation

shutdown-conn-closer.exe --uninstall

Testing

Run in test mode to see which connections would be closed without actually closing them:

shutdown-conn-closer.exe --test

Building

Cross-compile from Linux (MSVC, recommended)

cargo install cargo-xwin
rustup target add x86_64-pc-windows-msvc
cargo xwin build --target x86_64-pc-windows-msvc --release

Cross-compile from Linux (GNU)

sudo apt install mingw-w64
rustup target add x86_64-pc-windows-gnu
cargo build --target x86_64-pc-windows-gnu --release

Note: GNU-linked binaries may trigger antivirus false positives. MSVC is recommended.

Native on Windows

cargo build --release

Logging

The service writes to shutdown-conn-closer.log next to the executable.

Notes

  • 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.