Open kke opened 3 years ago
@masterzen I made a PR that attempts to fix this via sync.Once
in #122 - PTAL.
@kke , do you by any chance have client code or a unit test that could reproduce the issue so that I understand how the double close was triggered?
It goes something like this:
shell, err := c.client.CreateShell()
if err != nil {
return err
}
defer shell.Close()
command, err := shell.Execute(cmd)
if err != nil {
return err
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
defer command.Stdin.Close()
command.Stdin.Write([]byte(stdin))
}()
wg.Add(1)
go func() {
defer wg.Done()
defer command.Stdout.Close()
outputScanner := bufio.NewScanner(command.Stdout)
for outputScanner.Scan() {
fmt.Print(outputScanner.Text()+"\n", "")
}
}()
command.Wait()
wg.Wait()
command.Close()
It seems to occur in situations where a command is periodically retried.
Anyway, to me it seems the select statement does not always do what it is supposed to. If it manages to read from the cancel channel, the channel remains open. Also I'm not sure if it's guaranteed to always hit that branch first.
Also I don't know if it should even proceed to send the signal to endpoint if the channel is already closed.
https://github.com/masterzen/winrm/blob/master/command.go#L111-L115