Commit f6ff3f3
committed
client: close client.Std{err,out} if they are not os.Std{err,out}
cpu clients are implemented in the client package. The cpu
command uses this package. As in os/exec, code can call
client.Command(); followed by Client.Start() and client.Wait(),
or use client.Run(), which combines Start and Wait.
The client struct has io.WriteCloser for its Stdout and Stderr.
The question of how these gets closed is important.
Normally, when a cpu process runs, kernel process exit handling performs
a close on stdout and stderr. User code need not call close.
When the client package is used as part of a program that does not exit, as
in a test, code might set the client struct
Stderr and Stdout to a pipe, to avoid littering test output with cpu command
output. In this case, the pipe reader will hang until the Stderr and Stdout
are closed, as no process exits.
It is not safe to just blindly close these files; users might unintentionally
close Stdout, leading to confusing results.
The actual IO is handled by two io.Copy goroutines started in cpu.Start, one for
Stdout, one for Stderr. These two goroutines
exit when the remote process closes the ssh Session Std{err,out}.
At that point, code could decide whether to close the Std{err,out}.
An ideal time to do this check is when the io.Copy exits.
This change tests the type of client.Std{err,out}; IF they are an *os.File,
AND if their Fd() matches the Fd() of os.Std{err,out}, respectively, then they are
left alone; otherwise, they are closed.
This seems a safe test. It has been tested both in a test and interactively
and the behavior is sensible.
Signed-off-by: Ronald G Minnich <rminnich@gmail.com>1 parent 5c713f2 commit f6ff3f3
1 file changed
+22
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
64 | | - | |
65 | | - | |
| 64 | + | |
| 65 | + | |
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
| |||
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
105 | 112 | | |
106 | 113 | | |
107 | 114 | | |
| |||
512 | 519 | | |
513 | 520 | | |
514 | 521 | | |
| 522 | + | |
515 | 523 | | |
516 | 524 | | |
517 | 525 | | |
| |||
522 | 530 | | |
523 | 531 | | |
524 | 532 | | |
| 533 | + | |
525 | 534 | | |
526 | 535 | | |
527 | 536 | | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
528 | 544 | | |
529 | 545 | | |
| 546 | + | |
530 | 547 | | |
531 | 548 | | |
532 | 549 | | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
533 | 553 | | |
534 | 554 | | |
535 | 555 | | |
| |||
0 commit comments