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

Commit

Permalink
channel: add serial yamux channel close timeout
Browse files Browse the repository at this point in the history
So that if serial yamux session fails to close somehow,
we can catch it.

Fixes: #359

Signed-off-by: Peng Tao <[email protected]>
  • Loading branch information
bergwolf committed Sep 5, 2018
1 parent 7c95a50 commit 1d559a7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
9 changes: 8 additions & 1 deletion channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
var (
channelExistMaxTries = 200
channelExistWaitTime = 50 * time.Millisecond
channelCloseTimeout = 5 * time.Second
isAFVSockSupportedFunc = isAFVSockSupported
)

Expand Down Expand Up @@ -209,7 +210,13 @@ func (c *serialChannel) listen() (net.Listener, error) {
func (c *serialChannel) teardown() error {
// wait for the session to be fully shutdown first
if c.waitCh != nil {
<-c.waitCh
t := time.NewTimer(channelCloseTimeout)
select {
case <-c.waitCh:
t.Stop()
case <-t.C:
return fmt.Errorf("timeout waiting for yamux channel to close")
}
}
return c.serialConn.Close()
}
Expand Down
15 changes: 15 additions & 0 deletions channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -75,6 +76,20 @@ func TestTeardownSerialChannel(t *testing.T) {
assert.Nil(t, err, "%v", err)
}

func TestTeardownSerialChannelTimeout(t *testing.T) {
_, f, err := os.Pipe()
assert.Nil(t, err, "%v", err)
channelCloseTimeout = 1 * time.Microsecond

c := &serialChannel{
serialConn: f,
waitCh: make(chan struct{}),
}

err = c.teardown()
assert.NotNil(t, err, "channel close should timeout")
}

func TestNewChannel(t *testing.T) {
assert := assert.New(t)

Expand Down

0 comments on commit 1d559a7

Please sign in to comment.