shutdownInput cancels input before closing the reader's resources. Keep the bounded shutdown for slow readers, but retain ownership of a reader that is still in use until its input loop has actually finished.
()
| 118 | // bounded shutdown for slow readers, but retain ownership of a reader that is |
| 119 | // still in use until its input loop has actually finished. |
| 120 | func (p *Program) shutdownInput() { |
| 121 | reader := p.cancelReader |
| 122 | if reader == nil { |
| 123 | return |
| 124 | } |
| 125 | // The reader is closed below, or later once its input loop finishes. |
| 126 | // Forget it so that a later run doesn't close it again: closing it closes |
| 127 | // raw file descriptors, and closing one twice can close an unrelated file |
| 128 | // that has since reused the number. |
| 129 | p.cancelReader = nil |
| 130 | |
| 131 | if reader.Cancel() && p.readLoopDone != nil { |
| 132 | done := p.readLoopDone |
| 133 | timer := time.NewTimer(inputReadLoopTimeout) |
| 134 | defer timer.Stop() |
| 135 | |
| 136 | select { |
| 137 | case <-done: |
| 138 | case <-timer.C: |
| 139 | go func() { |
| 140 | <-done |
| 141 | _ = reader.Close() |
| 142 | }() |
| 143 | return |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | _ = reader.Close() |
| 148 | } |
| 149 | |
| 150 | // waitForReadLoop waits for the cancelReader to finish its read loop. |
| 151 | func (p *Program) waitForReadLoop() { |