Skip to content

Commit

Permalink
Wait jsonrpc main write loop to finish
Browse files Browse the repository at this point in the history
  • Loading branch information
Yevhenii Voievodin authored and Yevhenii Voievodin committed Jun 20, 2017
1 parent 823461c commit 7a0c1de
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions agents/go-agents/core/jsonrpc/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func NewTunnel(conn NativeConn, dispatcher ReqDispatcher) *Tunnel {
reqDispatcher: dispatcher,
q: q,
}
tunnel.closer = &closer{tunnel: tunnel}
tunnel.closer = &closer{tunnel: tunnel, outClosed: make(chan bool, 1)}
return tunnel
}

Expand Down Expand Up @@ -281,6 +281,7 @@ func (tun *Tunnel) mainWriteLoop() {
}
}
}
tun.closer.outClosed <- true
}

func (tun *Tunnel) mainReadLoop() {
Expand Down Expand Up @@ -413,16 +414,21 @@ type draft struct {
}

type closer struct {
once sync.Once
tunnel *Tunnel
once sync.Once
tunnel *Tunnel
outClosed chan bool
// 0 - not closed, 1 - closed
closed int32
}

func (closer *closer) closeOnce() {
closer.once.Do(func() {
atomic.StoreInt32(&closer.closed, 1)

close(closer.tunnel.jsonOut)
// wait write loop to complete
<- closer.outClosed

closer.tunnel.q.stopWatching()
if err := closer.tunnel.conn.Close(); err != nil {
log.Printf("Error while closing connection, %s", err.Error())
Expand Down

0 comments on commit 7a0c1de

Please sign in to comment.