[tor-commits] [goptlib/master] Don't wait for handlers to finish after SIGTERM in examples.

dcf at torproject.org dcf at torproject.org
Thu Jan 17 05:48:47 UTC 2019


commit 15f83653abbcced9003c96cc14edc5b2f82e0e0e
Author: David Fifield <david at bamsoftware.com>
Date:   Wed Jan 16 22:45:46 2019 -0700

    Don't wait for handlers to finish after SIGTERM in examples.
    
    The requirement to wait until handlers are finished, as well as the
    complicated signal counting logic, was removed from pt-spec.txt in 2014.
    
    https://bugs.torproject.org/26389#comment:5
---
 examples/dummy-client/dummy-client.go | 25 ++-----------------------
 examples/dummy-server/dummy-server.go | 25 ++-----------------------
 2 files changed, 4 insertions(+), 46 deletions(-)

diff --git a/examples/dummy-client/dummy-client.go b/examples/dummy-client/dummy-client.go
index 4812a9b..b1a9647 100644
--- a/examples/dummy-client/dummy-client.go
+++ b/examples/dummy-client/dummy-client.go
@@ -24,10 +24,6 @@ import "git.torproject.org/pluggable-transports/goptlib.git"
 
 var ptInfo pt.ClientInfo
 
-// When a connection handler starts, +1 is written to this channel; when it
-// ends, -1 is written.
-var handlerChan = make(chan int)
-
 func copyLoop(a, b net.Conn) {
 	var wg sync.WaitGroup
 	wg.Add(2)
@@ -45,11 +41,6 @@ func copyLoop(a, b net.Conn) {
 }
 
 func handler(conn *pt.SocksConn) error {
-	handlerChan <- 1
-	defer func() {
-		handlerChan <- -1
-	}()
-
 	defer conn.Close()
 	remote, err := net.Dial("tcp", conn.Req.Target)
 	if err != nil {
@@ -112,8 +103,6 @@ func main() {
 	}
 	pt.CmethodsDone()
 
-	var numHandlers int = 0
-	var sig os.Signal
 	sigChan := make(chan os.Signal, 1)
 	signal.Notify(sigChan, syscall.SIGTERM)
 
@@ -126,21 +115,11 @@ func main() {
 		}()
 	}
 
-	// keep track of handlers and wait for a signal
-	sig = nil
-	for sig == nil {
-		select {
-		case n := <-handlerChan:
-			numHandlers += n
-		case sig = <-sigChan:
-		}
-	}
+	// wait for a signal
+	<-sigChan
 
 	// signal received, shut down
 	for _, ln := range listeners {
 		ln.Close()
 	}
-	for numHandlers > 0 {
-		numHandlers += <-handlerChan
-	}
 }
diff --git a/examples/dummy-server/dummy-server.go b/examples/dummy-server/dummy-server.go
index db24fab..f3b0c8f 100644
--- a/examples/dummy-server/dummy-server.go
+++ b/examples/dummy-server/dummy-server.go
@@ -25,10 +25,6 @@ import "git.torproject.org/pluggable-transports/goptlib.git"
 
 var ptInfo pt.ServerInfo
 
-// When a connection handler starts, +1 is written to this channel; when it
-// ends, -1 is written.
-var handlerChan = make(chan int)
-
 func copyLoop(a, b net.Conn) {
 	var wg sync.WaitGroup
 	wg.Add(2)
@@ -48,11 +44,6 @@ func copyLoop(a, b net.Conn) {
 func handler(conn net.Conn) error {
 	defer conn.Close()
 
-	handlerChan <- 1
-	defer func() {
-		handlerChan <- -1
-	}()
-
 	or, err := pt.DialOr(&ptInfo, conn.RemoteAddr().String(), "dummy")
 	if err != nil {
 		return err
@@ -104,8 +95,6 @@ func main() {
 	}
 	pt.SmethodsDone()
 
-	var numHandlers int = 0
-	var sig os.Signal
 	sigChan := make(chan os.Signal, 1)
 	signal.Notify(sigChan, syscall.SIGTERM)
 
@@ -118,21 +107,11 @@ func main() {
 		}()
 	}
 
-	// keep track of handlers and wait for a signal
-	sig = nil
-	for sig == nil {
-		select {
-		case n := <-handlerChan:
-			numHandlers += n
-		case sig = <-sigChan:
-		}
-	}
+	// wait for a signal
+	<-sigChan
 
 	// signal received, shut down
 	for _, ln := range listeners {
 		ln.Close()
 	}
-	for numHandlers > 0 {
-		numHandlers += <-handlerChan
-	}
 }



More information about the tor-commits mailing list