Ensure listener is closed even if the daemon is killed unexpectedly

This commit is contained in:
2025-05-04 20:03:23 -04:00
parent 9314852f06
commit 5975c71b4f
2 changed files with 19 additions and 0 deletions
+11
View File
@@ -8,7 +8,9 @@ import (
"net"
"net/rpc"
"os"
"os/signal"
"strconv"
"syscall"
"time"
peercred "github.com/rwinkhart/peercred-mini"
@@ -35,6 +37,15 @@ func Start(passphrase string) {
defer listener.Close()
log.Printf("RPC daemon listening on unix://%s", socketPath)
// capture sigterms to ensure listener is closed
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigChan
listener.Close()
os.Exit(0)
}()
// accept connections (timeout after 3 minutes of inactivity)
for {
listener.(*net.UnixListener).SetDeadline(time.Now().Add(3 * time.Minute))