Browse Source

fix a bug that would cause the client to hang when shutting down abnormally

Alan Shreve 12 years ago
parent
commit
92e125b137
2 changed files with 10 additions and 9 deletions
  1. 9 8
      src/ngrok/client/controller.go
  2. 1 1
      src/ngrok/client/model.go

+ 9 - 8
src/ngrok/client/controller.go

@@ -179,22 +179,21 @@ func (ctl *Controller) Run(opts *Options) {
 
 	ctl.Go(func() { ctl.model.Run(opts.server, opts.authtoken, ctl, reqTunnel, opts.localaddr) })
 
-	quitMessage := ""
-	defer func() {
-		ctl.doShutdown()
-		fmt.Println(quitMessage)
-	}()
-
 	updates := ctl.updates.Reg()
 	defer ctl.updates.UnReg(updates)
 
+	done := make(chan int)
 	for {
 		select {
 		case obj := <-ctl.cmds:
 			switch cmd := obj.(type) {
 			case cmdQuit:
-				quitMessage = cmd.message
-				return
+				msg := cmd.message
+				go func() {
+					ctl.doShutdown()
+					fmt.Println(msg)
+					done <- 1
+				}()
 
 			case cmdPlayRequest:
 				ctl.Go(func() { ctl.model.PlayRequest(cmd.tunnel, cmd.payload) })
@@ -204,6 +203,8 @@ func (ctl *Controller) Run(opts *Options) {
 			state = obj.(mvc.State)
 
 		case ctl.state <- state:
+		case <-done:
+			return
 		}
 	}
 }

+ 1 - 1
src/ngrok/client/model.go

@@ -200,7 +200,6 @@ func (c *ClientModel) control(reqTunnel *msg.ReqTunnel, localaddr string) {
 	}
 
 	c.id = authResp.ClientId
-	c.connStatus = mvc.ConnOnline
 	c.serverVersion = authResp.MmVersion
 	c.Info("Authenticated with server, client id: %v", c.id)
 	c.update()
@@ -255,6 +254,7 @@ func (c *ClientModel) control(reqTunnel *msg.ReqTunnel, localaddr string) {
 			}
 
 			c.tunnels[tunnel.PublicUrl] = tunnel
+			c.connStatus = mvc.ConnOnline
 			c.Info("Tunnel established at %v", tunnel.PublicUrl)
 			c.update()