From 62c321b3fc0583328832e358e1dd2843457c13d2 Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Thu, 29 Jul 2021 15:05:34 +0300 Subject: [PATCH 1/2] Avoid closing the Command cancel channel twice Signed-off-by: Kimmo Lehto --- command.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/command.go b/command.go index 80afd95..892025a 100644 --- a/command.go +++ b/command.go @@ -37,6 +37,7 @@ type Command struct { done chan struct{} cancel chan struct{} + once sync.Once } func newCommand(ctx context.Context, shell *Shell, ids string) *Command { @@ -113,20 +114,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 } From a0872011bfba9194dca6efd9e99b4d8c2aaa18f4 Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Fri, 22 Dec 2023 09:59:40 +0200 Subject: [PATCH 2/2] Clear c.id on Close to avoid closing twice Signed-off-by: Kimmo Lehto --- command.go | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/command.go b/command.go index 892025a..6a50413 100644 --- a/command.go +++ b/command.go @@ -37,7 +37,6 @@ type Command struct { done chan struct{} cancel chan struct{} - once sync.Once } func newCommand(ctx context.Context, shell *Shell, ids string) *Command { @@ -114,24 +113,19 @@ func (c *Command) check() error { // Close will terminate the running command func (c *Command) Close() error { - var err error - - if err = c.check(); err != nil { + if err := c.check(); err != nil { return err } - if _, ok := <-c.cancel; !ok { - return nil // channel already closed, do not send a signal - } + close(c.cancel) - c.once.Do(func() { - close(c.cancel) + id := c.id + c.id = "" - request := NewSignalRequest(c.client.url, c.shell.id, c.id, &c.client.Parameters) - defer request.Free() + request := NewSignalRequest(c.client.url, c.shell.id, id, &c.client.Parameters) + defer request.Free() - _, err = c.client.sendRequest(request) - }) + _, err := c.client.sendRequest(request) return err }