From b4f4b29a037fd2444423835da5999f39525f3d24 Mon Sep 17 00:00:00 2001
From: David Fifield <david@bamsoftware.com>
Date: Fri, 11 Oct 2019 16:50:25 -0600
Subject: [PATCH] Stop counting handlers before terminating.

The requirement to do so is obsolete and has already been removed from
other pluggable transports.

https://bugs.torproject.org/32046
---
 client/lib/snowflake.go    |  8 --------
 client/snowflake.go        | 14 +-------------
 server-webrtc/snowflake.go | 26 ++------------------------
 server/server.go           | 25 ++-----------------------
 4 files changed, 5 insertions(+), 68 deletions(-)

diff --git a/client/lib/snowflake.go b/client/lib/snowflake.go
index 1c9c34d..2e68e36 100644
--- a/client/lib/snowflake.go
+++ b/client/lib/snowflake.go
@@ -13,17 +13,9 @@ const (
 	SnowflakeTimeout = 30
 )
 
-// HandlerChan - When a connection handler starts, +1 is written to this channel; when it
-// ends, -1 is written.
-var HandlerChan = make(chan int)
-
 // Given an accepted SOCKS connection, establish a WebRTC connection to the
 // remote peer and exchange traffic.
 func Handler(socks SocksConnector, snowflakes SnowflakeCollector) error {
-	HandlerChan <- 1
-	defer func() {
-		HandlerChan <- -1
-	}()
 	// Obtain an available WebRTC remote. May block.
 	snowflake := snowflakes.Pop()
 	if nil == snowflake {
diff --git a/client/snowflake.go b/client/snowflake.go
index 18b704f..959f83c 100644
--- a/client/snowflake.go
+++ b/client/snowflake.go
@@ -184,8 +184,6 @@ func main() {
 	}
 	pt.CmethodsDone()
 
-	var numHandlers int
-	var sig os.Signal
 	sigChan := make(chan os.Signal, 1)
 	signal.Notify(sigChan, syscall.SIGTERM)
 
@@ -202,22 +200,12 @@ func main() {
 	}
 
 	// keep track of handlers and wait for a signal
-	sig = nil
-	for sig == nil {
-		select {
-		case n := <-sf.HandlerChan:
-			numHandlers += n
-		case sig = <-sigChan:
-		}
-	}
+	<-sigChan
 
 	// signal received, shut down
 	for _, ln := range listeners {
 		ln.Close()
 	}
 	snowflakes.End()
-	for numHandlers > 0 {
-		numHandlers += <-sf.HandlerChan
-	}
 	log.Println("snowflake is done.")
 }
diff --git a/server-webrtc/snowflake.go b/server-webrtc/snowflake.go
index cf1c888..d5b1604 100644
--- a/server-webrtc/snowflake.go
+++ b/server-webrtc/snowflake.go
@@ -21,10 +21,6 @@ var ptMethodName = "snowflake"
 var ptInfo pt.ServerInfo
 var logFile *os.File
 
-// When a datachannel handler starts, +1 is written to this channel;
-// when it ends, -1 is written.
-var handlerChan = make(chan int)
-
 func copyLoop(WebRTC, ORPort net.Conn) {
 	var wg sync.WaitGroup
 	wg.Add(2)
@@ -100,11 +96,6 @@ func (c *webRTCConn) SetWriteDeadline(t time.Time) error {
 func datachannelHandler(conn *webRTCConn) {
 	defer conn.Close()
 
-	handlerChan <- 1
-	defer func() {
-		handlerChan <- -1
-	}()
-
 	or, err := pt.DialOr(&ptInfo, "", ptMethodName) // TODO: Extended OR
 	if err != nil {
 		log.Printf("Failed to connect to ORPort: " + err.Error())
@@ -246,8 +237,6 @@ func main() {
 	}
 	pt.SmethodsDone()
 
-	var numHandlers int
-	var sig os.Signal
 	sigChan := make(chan os.Signal, 1)
 	signal.Notify(sigChan, syscall.SIGTERM)
 
@@ -263,17 +252,6 @@ 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:
-		}
-	}
-
-	for numHandlers > 0 {
-		numHandlers += <-handlerChan
-	}
+	// wait for a signal
+	<-sigChan
 }
diff --git a/server/server.go b/server/server.go
index 5d15df6..b1b566a 100644
--- a/server/server.go
+++ b/server/server.go
@@ -37,10 +37,6 @@ const listenAndServeErrorTimeout = 100 * time.Millisecond
 
 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 usage() {
 	fmt.Fprintf(os.Stderr, `Usage: %s [OPTIONS]
 
@@ -157,11 +153,6 @@ func webSocketHandler(ws *websocket.WebSocket) {
 	conn := newWebSocketConn(ws)
 	defer conn.Close()
 
-	handlerChan <- 1
-	defer func() {
-		handlerChan <- -1
-	}()
-
 	// Pass the address of client as the remote address of incoming connection
 	clientIPParam := ws.Request().URL.Query().Get("client_ip")
 	addr := clientAddr(clientIPParam)
@@ -390,8 +381,6 @@ func main() {
 	}
 	pt.SmethodsDone()
 
-	var numHandlers int
-	var sig os.Signal
 	sigChan := make(chan os.Signal, 1)
 	signal.Notify(sigChan, syscall.SIGTERM)
 
@@ -407,22 +396,12 @@ 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
+	sig := <-sigChan
 
 	// signal received, shut down
 	log.Printf("caught signal %q, exiting", sig)
 	for _, server := range servers {
 		server.Close()
 	}
-	for numHandlers > 0 {
-		numHandlers += <-handlerChan
-	}
 }
-- 
2.20.1

