Skip to content

Commit 7d9eaea

Browse files
committed
Close the pty instead of killing the process for runAndDetectCredentialRequest
As with the previous commit, this is not strictly necessary for anything, just cleaner.
1 parent 83046a0 commit 7d9eaea

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

pkg/commands/oscommands/cmd_obj_runner.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ func (self *cmdObjRunner) runAndDetectCredentialRequest(
336336
tr := io.TeeReader(handler.stdoutPipe, cmdWriter)
337337

338338
go utils.Safe(func() {
339-
self.processOutput(tr, handler.stdinPipe, promptUserForCredential, cmdObj)
339+
self.processOutput(tr, handler.stdinPipe, promptUserForCredential, handler.close, cmdObj)
340340
})
341341
})
342342
}
@@ -345,6 +345,7 @@ func (self *cmdObjRunner) processOutput(
345345
reader io.Reader,
346346
writer io.Writer,
347347
promptUserForCredential func(CredentialType) <-chan string,
348+
closeFunc func() error,
348349
cmdObj *CmdObj,
349350
) {
350351
checkForCredentialRequest := self.getCheckForCredentialRequestFunc()
@@ -358,25 +359,26 @@ func (self *cmdObjRunner) processOutput(
358359
if ok {
359360
responseChan := promptUserForCredential(askFor)
360361
if responseChan == nil {
361-
// Returning a nil channel means we should kill the process.
362-
// Note that we don't break the loop after this, because we
363-
// still need to drain the output, otherwise the Wait() call
364-
// later might block.
365-
if err := Kill(cmdObj.GetCmd()); err != nil {
362+
// Returning a nil channel means we should terminate the process.
363+
// We achieve this by closing the pty that it's running in. Note that this won't
364+
// work for the case where we're not running in a pty (i.e. on Windows), but
365+
// in that case we'll never be prompted for credentials, so it's not a concern.
366+
if err := closeFunc(); err != nil {
366367
self.log.Error(err)
367368
}
368-
} else {
369-
if task != nil {
370-
task.Pause()
371-
}
372-
toInput := <-responseChan
373-
if task != nil {
374-
task.Continue()
375-
}
376-
// If the return data is empty we don't write anything to stdin
377-
if toInput != "" {
378-
_, _ = writer.Write([]byte(toInput))
379-
}
369+
break
370+
}
371+
372+
if task != nil {
373+
task.Pause()
374+
}
375+
toInput := <-responseChan
376+
if task != nil {
377+
task.Continue()
378+
}
379+
// If the return data is empty we don't write anything to stdin
380+
if toInput != "" {
381+
_, _ = writer.Write([]byte(toInput))
380382
}
381383
}
382384
}

pkg/commands/oscommands/cmd_obj_runner_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func TestProcessOutput(t *testing.T) {
127127
writer := &strings.Builder{}
128128

129129
cmdObj := &CmdObj{task: gocui.NewFakeTask()}
130-
runner.processOutput(reader, writer, toChanFn(scenario.promptUserForCredential), cmdObj)
130+
runner.processOutput(reader, writer, toChanFn(scenario.promptUserForCredential), func() error { return nil }, cmdObj)
131131

132132
if writer.String() != scenario.expectedToWrite {
133133
t.Errorf("expected to write '%s' but got '%s'", scenario.expectedToWrite, writer.String())

0 commit comments

Comments
 (0)