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

Commit

Permalink
Merge pull request #360 from bergwolf/timeout
Browse files Browse the repository at this point in the history
channel: add serial yamux channel close timeout
  • Loading branch information
bergwolf authored Sep 6, 2018
2 parents 1818587 + 1d559a7 commit 705c005
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 705c005

Please sign in to comment.