* Child process exited but the session itself is sticking around. We tell * the client (lifecycle child-exit), then schedule a respawn after a short * debounce — unless the child has been crashing too often, in which case * we open the circuit breaker and dispose for real.
(
exited: pty.IPty,
exitCode: number,
signalRaw: number | undefined,
)
| 169 | * we open the circuit breaker and dispose for real. |
| 170 | */ |
| 171 | private onChildExit( |
| 172 | exited: pty.IPty, |
| 173 | exitCode: number, |
| 174 | signalRaw: number | undefined, |
| 175 | ): void { |
| 176 | if (this.disposed) return; |
| 177 | // Ignore exits from an already-replaced term (paranoia). |
| 178 | if (exited !== this.term) return; |
| 179 | const signal = typeof signalRaw === 'number' ? signalRaw : null; |
| 180 | this.log.info('session.child_exit', { |
| 181 | pid: exited.pid, |
| 182 | code: exitCode, |
| 183 | signal, |
| 184 | }); |
| 185 | if (this.firstExit === null) { |
| 186 | this.firstExit = { code: exitCode, signal }; |
| 187 | for (const w of this.exitWaiters) w(this.firstExit); |
| 188 | this.exitWaiters.clear(); |
| 189 | } |
| 190 | this.sendControl({ |
| 191 | type: 'lifecycle', |
| 192 | kind: 'child-exit', |
| 193 | code: exitCode, |
| 194 | signal, |
| 195 | }); |
| 196 | |
| 197 | const now = Date.now(); |
| 198 | this.respawnTimes = this.respawnTimes.filter((t) => now - t < RESPAWN_WINDOW_MS); |
| 199 | this.respawnTimes.push(now); |
| 200 | if (this.respawnTimes.length > RESPAWN_WINDOW_LIMIT) { |
| 201 | this.log.warn('session.respawn_circuit_open', { |
| 202 | recentCrashes: this.respawnTimes.length, |
| 203 | }); |
| 204 | this.sendControl({ type: 'exit', code: exitCode, signal }); |
| 205 | this.dispose('respawn circuit open'); |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | if (this.respawnTimer) clearTimeout(this.respawnTimer); |
| 210 | this.respawnTimer = setTimeout(() => this.respawnNow(), RESPAWN_DEBOUNCE_MS); |
| 211 | this.respawnTimer.unref(); |
| 212 | } |
| 213 | |
| 214 | private respawnNow(): void { |
| 215 | this.respawnTimer = null; |
no test coverage detected