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

Commit 1d559a7

Browse files
committed
channel: add serial yamux channel close timeout
So that if serial yamux session fails to close somehow, we can catch it. Fixes: #359 Signed-off-by: Peng Tao <bergwolf@gmail.com>
1 parent 7c95a50 commit 1d559a7

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

channel.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
var (
2626
channelExistMaxTries = 200
2727
channelExistWaitTime = 50 * time.Millisecond
28+
channelCloseTimeout = 5 * time.Second
2829
isAFVSockSupportedFunc = isAFVSockSupported
2930
)
3031

@@ -209,7 +210,13 @@ func (c *serialChannel) listen() (net.Listener, error) {
209210
func (c *serialChannel) teardown() error {
210211
// wait for the session to be fully shutdown first
211212
if c.waitCh != nil {
212-
<-c.waitCh
213+
t := time.NewTimer(channelCloseTimeout)
214+
select {
215+
case <-c.waitCh:
216+
t.Stop()
217+
case <-t.C:
218+
return fmt.Errorf("timeout waiting for yamux channel to close")
219+
}
213220
}
214221
return c.serialConn.Close()
215222
}

channel_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os"
1313
"path/filepath"
1414
"testing"
15+
"time"
1516

1617
"github.com/stretchr/testify/assert"
1718
)
@@ -75,6 +76,20 @@ func TestTeardownSerialChannel(t *testing.T) {
7576
assert.Nil(t, err, "%v", err)
7677
}
7778

79+
func TestTeardownSerialChannelTimeout(t *testing.T) {
80+
_, f, err := os.Pipe()
81+
assert.Nil(t, err, "%v", err)
82+
channelCloseTimeout = 1 * time.Microsecond
83+
84+
c := &serialChannel{
85+
serialConn: f,
86+
waitCh: make(chan struct{}),
87+
}
88+
89+
err = c.teardown()
90+
assert.NotNil(t, err, "channel close should timeout")
91+
}
92+
7893
func TestNewChannel(t *testing.T) {
7994
assert := assert.New(t)
8095

0 commit comments

Comments
 (0)