Skip to content

Commit

Permalink
Avoid closing the Command cancel channel twice
Browse files Browse the repository at this point in the history
  • Loading branch information
kke committed Jul 29, 2021
1 parent 3b76017 commit 4b3ea15
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Command struct {

done chan struct{}
cancel chan struct{}
once sync.Once
}

func newCommand(shell *Shell, ids string) *Command {
Expand Down Expand Up @@ -104,20 +105,25 @@ func (c *Command) check() error {

// Close will terminate the running command
func (c *Command) Close() error {
if err := c.check(); err != nil {
var err error

if err = c.check(); err != nil {
return err
}

select { // close cancel channel if it's still open
case <-c.cancel:
default:
close(c.cancel)
if _, ok := <-c.cancel; !ok {
return nil // channel already closed, do not send a signal
}

request := NewSignalRequest(c.client.url, c.shell.id, c.id, &c.client.Parameters)
defer request.Free()
c.once.Do(func() {
close(c.cancel)

request := NewSignalRequest(c.client.url, c.shell.id, c.id, &c.client.Parameters)
defer request.Free()

_, err = c.client.sendRequest(request)
})

_, err := c.client.sendRequest(request)
return err
}

Expand Down

0 comments on commit 4b3ea15

Please sign in to comment.