Skip to content

Commit

Permalink
Merge pull request #235 from gopcua/monitorchannel-cancellation
Browse files Browse the repository at this point in the history
improved cancellation of monitorChannel() goroutine
  • Loading branch information
magiconair authored Jul 10, 2019
2 parents 3a9584a + 1f7662d commit edee101
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ type Client struct {
subscriptions map[uint32]*Subscription
subMux sync.RWMutex

//cancelMonitor cancels the monitorChannel goroutine
cancelMonitor context.CancelFunc

// once initializes session
once sync.Once
}
Expand Down Expand Up @@ -171,9 +174,12 @@ func (c *Client) Dial(ctx context.Context) error {
return err
}
c.sechan = sechan
ctx, c.cancelMonitor = context.WithCancel(ctx)
go c.monitorChannel(ctx)

if err := sechan.Open(); err != nil {
c.cancelMonitor()

_ = conn.Close()
c.sechan = nil
return err
Expand Down Expand Up @@ -209,6 +215,10 @@ func (c *Client) Close() error {
// try to close the session but ignore any error
// so that we close the underlying channel and connection.
_ = c.CloseSession()
if c.cancelMonitor != nil {
c.cancelMonitor()
}

return c.sechan.Close()
}

Expand Down

0 comments on commit edee101

Please sign in to comment.