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 #338 from bergwolf/yamux
Browse files Browse the repository at this point in the history
wait on yamux session close
  • Loading branch information
Julio Montes authored Aug 29, 2018
2 parents 7e9f7d0 + ba0c7fc commit cdc68ec
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 42 deletions.
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

[[constraint]]
name = "github.com/hashicorp/yamux"
revision = "f5742cb6b85602e7fa834e9d5d91a7d7fa850824"
revision = "cc6d2ea263b2471faabce371255777a365bf8306"

[[constraint]]
name = "github.com/mdlayher/vsock"
Expand Down
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
23 changes: 22 additions & 1 deletion protocols/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package client

import (
"context"
"fmt"
"net"
"net/url"
"strconv"
Expand All @@ -31,6 +32,7 @@ const (
)

var defaultDialTimeout = 15 * time.Second
var defaultCloseTimeout = 5 * time.Second

// AgentClient is an agent gRPC client connection wrapper for agentgrpc.AgentServiceClient
type AgentClient struct {
Expand All @@ -45,7 +47,26 @@ type yamuxSessionStream struct {
}

func (y *yamuxSessionStream) Close() error {
return y.session.Close()
waitCh := y.session.CloseChan()
timeout := time.NewTimer(defaultCloseTimeout)

if err := y.Conn.Close(); err != nil {
return err
}

if err := y.session.Close(); err != nil {
return err
}

// block until session is really closed
select {
case <-waitCh:
timeout.Stop()
case <-timeout.C:
return fmt.Errorf("timeout waiting for session close")
}

return nil
}

type dialer func(string, time.Duration) (net.Conn, error)
Expand Down
65 changes: 45 additions & 20 deletions vendor/github.com/hashicorp/yamux/session.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 29 additions & 16 deletions vendor/github.com/hashicorp/yamux/stream.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions vendor/github.com/hashicorp/yamux/util.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cdc68ec

Please sign in to comment.