From a5108bc6cc1cd72151f007817713d880820fbd7f Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Sat, 12 Apr 2025 19:53:02 -0400 Subject: [PATCH] Implement inactivity timer on Windows --- daemon/2serverUNIXGeneric.go | 1 + daemon/2serverWindows.go | 28 ++++++++++++++++++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/daemon/2serverUNIXGeneric.go b/daemon/2serverUNIXGeneric.go index 055c7ce..b1b433d 100644 --- a/daemon/2serverUNIXGeneric.go +++ b/daemon/2serverUNIXGeneric.go @@ -47,6 +47,7 @@ func Run() { if err != nil { if err.(net.Error).Timeout() { log.Println("Three minutes have passed without any connections. Exiting...") + listener.Close() os.Exit(0) } log.Printf("Accept error: %v", err) diff --git a/daemon/2serverWindows.go b/daemon/2serverWindows.go index f026ebc..7df12e8 100644 --- a/daemon/2serverWindows.go +++ b/daemon/2serverWindows.go @@ -8,6 +8,7 @@ import ( "net" "net/rpc" "os" + "time" "github.com/Microsoft/go-winio" // For Windows named pipes "github.com/rwinkhart/peercred-mini" @@ -44,21 +45,32 @@ func Run() { defer listener.Close() log.Printf("RPC daemon listening on %s", socketPath) - // accept connections (timeout after 3 minutes of inactivity) - for { - // set deadline for accepting new connections - //listener.SetDeadline(time.Now().Add(3 * time.Minute)) TODO FIX + // create 3-minute inactivity timer + timer := time.NewTimer(3 * time.Minute) + killTimer := make(chan struct{}) + go func() { + select { + case <-timer.C: + log.Println("Three minutes have passed without any connections. Exiting...") + listener.Close() + os.Exit(0) + case <-killTimer: + return + } + }() + // accept connections + for { conn, err := listener.Accept() if err != nil { - if os.IsTimeout(err) { - log.Println("Three minutes have passed without any connections. Exiting...") - os.Exit(0) - } log.Printf("Accept error: %v", err) + close(killTimer) continue } + // reset timer after connection is accepted + timer.Reset(3 * time.Minute) + // use a goroutine to check the client's identity go handleConn(conn) }