| 85 | * Program created from a subprocess. |
| 86 | */ |
| 87 | export class TerminalProcess implements IProgram { |
| 88 | /** |
| 89 | * How often to check and see if the process exited. |
| 90 | */ |
| 91 | private static readonly terminationPollInterval = 1000; |
| 92 | |
| 93 | /** |
| 94 | * How often to check and see if the process exited after we send a close signal. |
| 95 | */ |
| 96 | private static readonly killConfirmInterval = 200; |
| 97 | |
| 98 | private didStop = false; |
| 99 | private onStopped!: (killed: boolean) => void; |
| 100 | public readonly stopped = new Promise<IStopMetadata>( |
| 101 | resolve => |
| 102 | (this.onStopped = killed => { |
| 103 | this.didStop = true; |
| 104 | resolve({ code: 0, killed }); |
| 105 | }), |
| 106 | ); |
| 107 | private loop?: { timer: NodeJS.Timer; processId: number }; |
| 108 | |
| 109 | constructor( |
| 110 | private readonly terminalResult: Dap.RunInTerminalResult, |
| 111 | private readonly logger: ILogger, |
| 112 | ) { |
| 113 | if (terminalResult.processId) { |
| 114 | this.startPollLoop(terminalResult.processId); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public gotTelemetery({ processId }: IProcessTelemetry) { |
| 119 | if (this.didStop) { |
| 120 | killTree(processId, this.logger); |
| 121 | return; // to avoid any races |
| 122 | } |
| 123 | |
| 124 | if (!this.loop) { |
| 125 | this.startPollLoop(processId); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | public stop(): Promise<IStopMetadata> { |
| 130 | if (this.didStop) { |
| 131 | return this.stopped; |
| 132 | } |
| 133 | this.didStop = true; |
| 134 | |
| 135 | // If we're already polling some process ID, kill it and accelerate polling |
| 136 | // so we can confirm it's dead quickly. |
| 137 | if (this.loop) { |
| 138 | killTree(this.loop.processId, this.logger); |
| 139 | this.startPollLoop(this.loop.processId, TerminalProcess.killConfirmInterval); |
| 140 | } else if (this.terminalResult.shellProcessId) { |
| 141 | // If we had a shell process ID, well, that's good enough. |
| 142 | killTree(this.terminalResult.shellProcessId, this.logger); |
| 143 | this.startPollLoop(this.terminalResult.shellProcessId, TerminalProcess.killConfirmInterval); |
| 144 | } else { |