| 123 | * Program created from a subprocess. |
| 124 | */ |
| 125 | export class TerminalProcess implements IProgram { |
| 126 | /** |
| 127 | * How often to check and see if the process exited. |
| 128 | */ |
| 129 | private static readonly terminationPollInterval = 1000; |
| 130 | |
| 131 | /** |
| 132 | * How often to check and see if the process exited after we send a close signal. |
| 133 | */ |
| 134 | private static readonly killConfirmInterval = 200; |
| 135 | |
| 136 | private didStop = false; |
| 137 | private onStopped!: (killed: boolean) => void; |
| 138 | public readonly stopped = new Promise<IStopMetadata>( |
| 139 | resolve => (this.onStopped = killed => { |
| 140 | this.didStop = true; |
| 141 | resolve({ code: 0, killed }); |
| 142 | }), |
| 143 | ); |
| 144 | private liveness?: PidLiveness; |
| 145 | |
| 146 | constructor( |
| 147 | private readonly terminalResult: Dap.RunInTerminalResult, |
| 148 | private readonly logger: ILogger, |
| 149 | private readonly killBehavior: KillBehavior, |
| 150 | ) { |
| 151 | if (terminalResult.processId) { |
| 152 | this.liveness = new PidLiveness( |
| 153 | terminalResult.processId, |
| 154 | this.onStopped, |
| 155 | TerminalProcess.terminationPollInterval, |
| 156 | ); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | public gotTelemetery({ processId }: IProcessTelemetry) { |
| 161 | if (this.didStop) { |
| 162 | killTree(processId, this.logger, this.killBehavior); |
| 163 | return; // to avoid any races |
| 164 | } |
| 165 | |
| 166 | this.liveness ??= new PidLiveness(processId, this.onStopped, 1000); |
| 167 | } |
| 168 | |
| 169 | public stop(): Promise<IStopMetadata> { |
| 170 | if (this.didStop) { |
| 171 | return this.stopped; |
| 172 | } |
| 173 | this.didStop = true; |
| 174 | |
| 175 | // If we're already polling some process ID, kill it and accelerate polling |
| 176 | // so we can confirm it's dead quickly. |
| 177 | if (this.liveness) { |
| 178 | killTree(this.liveness.pid, this.logger, this.killBehavior); |
| 179 | this.liveness.updateInterval(TerminalProcess.killConfirmInterval); |
| 180 | } else if (this.terminalResult.shellProcessId) { |
| 181 | // If we had a shell process ID, well, that's good enough. |
| 182 | killTree(this.terminalResult.shellProcessId, this.logger, this.killBehavior); |