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

Commit

Permalink
client: wait for session to be fully closed
Browse files Browse the repository at this point in the history
So that we know for sure nothing is using the yamux channel before
returning success in close.

Fixes: #337

Signed-off-by: Peng Tao <[email protected]>
  • Loading branch information
bergwolf committed Aug 29, 2018
1 parent 0865c98 commit ba0c7fc
Showing 1 changed file with 22 additions and 1 deletion.
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

0 comments on commit ba0c7fc

Please sign in to comment.