[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [snowflake/master] Update server shutdown procedure.



commit 019e2cea23f7a78f8ef2132ce7d2ad64b365af24
Author: David Fifield <david@xxxxxxxxxxxxxxx>
Date:   Wed Mar 21 00:53:31 2018 -0700

    Update server shutdown procedure.
    
    Ignore SIGINT, honor TOR_PT_EXIT_ON_STDIN_CLOSE.
---
 server-webrtc/snowflake.go | 29 +++++++++++++++--------------
 server/server.go           | 11 +++++++++++
 2 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/server-webrtc/snowflake.go b/server-webrtc/snowflake.go
index 0484d94..4d6641a 100644
--- a/server-webrtc/snowflake.go
+++ b/server-webrtc/snowflake.go
@@ -5,6 +5,7 @@ import (
 	"flag"
 	"fmt"
 	"io"
+	"io/ioutil"
 	"log"
 	"net"
 	"os"
@@ -291,9 +292,19 @@ func main() {
 	var numHandlers int = 0
 	var sig os.Signal
 	sigChan := make(chan os.Signal, 1)
-	signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
+	signal.Notify(sigChan, syscall.SIGTERM)
 
-	// wait for first signal
+	if os.Getenv("TOR_PT_EXIT_ON_STDIN_CLOSE") == "1" {
+		// This environment variable means we should treat EOF on stdin
+		// just like SIGTERM: https://bugs.torproject.org/15435.
+		go func() {
+			io.Copy(ioutil.Discard, os.Stdin)
+			log.Printf("synthesizing SIGTERM because of stdin close")
+			sigChan <- syscall.SIGTERM
+		}()
+	}
+
+	// keep track of handlers and wait for a signal
 	sig = nil
 	for sig == nil {
 		select {
@@ -303,17 +314,7 @@ func main() {
 		}
 	}
 
-	if sig == syscall.SIGTERM {
-		return
-	}
-
-	// wait for second signal or no more handlers
-	sig = nil
-	for sig == nil && numHandlers != 0 {
-		select {
-		case n := <-handlerChan:
-			numHandlers += n
-		case sig = <-sigChan:
-		}
+	for numHandlers > 0 {
+		numHandlers += <-handlerChan
 	}
 }
diff --git a/server/server.go b/server/server.go
index 7c5a205..03095af 100644
--- a/server/server.go
+++ b/server/server.go
@@ -8,6 +8,7 @@ import (
 	"flag"
 	"fmt"
 	"io"
+	"io/ioutil"
 	"log"
 	"net"
 	"net/http"
@@ -378,6 +379,16 @@ func main() {
 	sigChan := make(chan os.Signal, 1)
 	signal.Notify(sigChan, syscall.SIGTERM)
 
+	if os.Getenv("TOR_PT_EXIT_ON_STDIN_CLOSE") == "1" {
+		// This environment variable means we should treat EOF on stdin
+		// just like SIGTERM: https://bugs.torproject.org/15435.
+		go func() {
+			io.Copy(ioutil.Discard, os.Stdin)
+			log.Printf("synthesizing SIGTERM because of stdin close")
+			sigChan <- syscall.SIGTERM
+		}()
+	}
+
 	// keep track of handlers and wait for a signal
 	sig = nil
 	for sig == nil {

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits