Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
agent: wait session to be fully shutdown
Browse files Browse the repository at this point in the history
So that we don't have any other goroutine on the channel
before retrying.

Signed-off-by: Peng Tao <[email protected]>
  • Loading branch information
bergwolf committed Aug 29, 2018
1 parent 55f1480 commit 0865c98
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 6 additions & 0 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func (c *vSockChannel) teardown() error {
type serialChannel struct {
serialPath string
serialConn *os.File
waitCh <-chan struct{}
}

func (c *serialChannel) setup() error {
Expand Down Expand Up @@ -200,11 +201,16 @@ func (c *serialChannel) listen() (net.Listener, error) {
if err != nil {
return nil, err
}
c.waitCh = session.CloseChan()

return session, nil
}

func (c *serialChannel) teardown() error {
// wait for the session to be fully shutdown first
if c.waitCh != nil {
<-c.waitCh
}
return c.serialConn.Close()
}

Expand Down
11 changes: 9 additions & 2 deletions channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,20 @@ func TestWaitSerialChannel(t *testing.T) {
}

func TestListenSerialChannel(t *testing.T) {
_, f, err := os.Pipe()
f, _, err := os.Pipe()
assert.Nil(t, err, "%v", err)

c := &serialChannel{serialConn: f}

_, err = c.listen()
l, err := c.listen()
assert.Nil(t, err, "%v", err)
assert.NotNil(t, l, "listen should not return nil listener")

err = l.Close()
assert.Nil(t, err, "%v", err)

err = c.teardown()
assert.Error(t, err, "connection should be already closed")
}

func TestTeardownSerialChannel(t *testing.T) {
Expand Down

0 comments on commit 0865c98

Please sign in to comment.