Skip to content

Commit 4005663

Browse files
committed
In case of cli, observe the process with the PID from the Init packet
1 parent 759bd68 commit 4005663

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

src/cloud.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Transport, DbgpConnection, ENCODING } from './dbgp'
44
import * as tls from 'tls'
55
import * as iconv from 'iconv-lite'
66
import * as xdebug from './xdebugConnection'
7-
import { EventEmitter } from 'stream'
7+
import { EventEmitter } from 'events'
88

99
export declare interface XdebugCloudConnection {
1010
on(event: 'error', listener: (error: Error) => void): this

src/phpDebug.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as childProcess from 'child_process'
88
import * as path from 'path'
99
import * as util from 'util'
1010
import * as fs from 'fs'
11-
import { Terminal, IProgram, ProgramPidWrapper } from './terminal'
11+
import { Terminal, IProgram, ProgramPidWrapper, isProcessAlive } from './terminal'
1212
import { convertClientPathToDebugger, convertDebuggerPathToClient, isPositiveMatchInGlobs } from './paths'
1313
import minimatch from 'minimatch'
1414
import { BreakpointManager, BreakpointAdapter } from './breakpoints'
@@ -514,6 +514,21 @@ class PhpDebugSession extends vscode.DebugSession {
514514
private async initializeConnection(connection: xdebug.Connection): Promise<void> {
515515
const initPacket = await connection.waitForInitPacket()
516516

517+
// track the process, if we asked the IDE to spawn it
518+
if (
519+
!this._phpProcess &&
520+
(this._args.program || this._args.runtimeArgs) &&
521+
initPacket.appid &&
522+
isProcessAlive(parseInt(initPacket.appid))
523+
) {
524+
this._phpProcess = new ProgramPidWrapper(parseInt(initPacket.appid))
525+
// we only do this for CLI mode. In normal listen mode, only a thread exited event is send.
526+
this._phpProcess.on('exit', (code: number | null) => {
527+
this.sendEvent(new vscode.ExitedEvent(code ?? 0))
528+
this.sendEvent(new vscode.TerminatedEvent())
529+
})
530+
}
531+
517532
// check if this connection should be skipped
518533
if (
519534
this._args.skipEntryPaths &&
@@ -1448,10 +1463,8 @@ class PhpDebugSession extends vscode.DebugSession {
14481463
}
14491464
}
14501465
// If launched as CLI, kill process
1451-
if (this._phpProcess?.pid) {
1452-
Terminal.killTree(this._phpProcess.pid).catch(err =>
1453-
this.sendEvent(new vscode.OutputEvent(`killTree: ${err as string}\n`))
1454-
)
1466+
if (this._phpProcess) {
1467+
this._phpProcess.kill()
14551468
}
14561469
} catch (error) {
14571470
this.sendErrorResponse(response, error as Error)

src/terminal.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import * as Path from 'path'
77
import * as FS from 'fs'
88
import * as CP from 'child_process'
9-
import { EventEmitter } from 'stream'
9+
import { EventEmitter } from 'events'
1010

1111
export class Terminal {
1212
private static _terminalService: ITerminalService
@@ -61,11 +61,11 @@ export class ProgramPidWrapper extends EventEmitter implements IProgram {
6161
/**
6262
* How often to check and see if the process exited after we send a close signal.
6363
*/
64-
//private static readonly killConfirmInterval = 200;
64+
private static readonly killConfirmInterval = 200
6565

6666
private loop?: { timer: NodeJS.Timeout; processId: number }
6767

68-
constructor(readonly pid?: number) {
68+
constructor(readonly pid: number) {
6969
super()
7070

7171
if (pid) {
@@ -74,7 +74,11 @@ export class ProgramPidWrapper extends EventEmitter implements IProgram {
7474
}
7575

7676
kill(signal?: number | NodeJS.Signals | undefined): boolean {
77-
return false
77+
this.startPollLoop(this.pid, ProgramPidWrapper.killConfirmInterval)
78+
Terminal.killTree(this.pid).catch(err => {
79+
// ignore
80+
})
81+
return true
7882
}
7983

8084
private startPollLoop(processId: number, interval = ProgramPidWrapper.terminationPollInterval) {
@@ -95,7 +99,7 @@ export class ProgramPidWrapper extends EventEmitter implements IProgram {
9599
this.loop = loop
96100
}
97101
}
98-
function isProcessAlive(processId: number) {
102+
export function isProcessAlive(processId: number) {
99103
try {
100104
// kill with signal=0 just test for whether the proc is alive. It throws if not.
101105
process.kill(processId, 0)

src/xdebugConnection.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export class InitPacket {
1818
engineVersion: string
1919
/** the name of the engine */
2020
engineName: string
21+
/** the internal PID */
22+
appid: string
2123
/**
2224
* @param {XMLDocument} document - An XML document to read from
2325
* @param {Connection} connection
@@ -30,6 +32,7 @@ export class InitPacket {
3032
this.ideKey = documentElement.getAttribute('idekey')!
3133
this.engineVersion = documentElement.getElementsByTagName('engine').item(0)?.getAttribute('version') ?? ''
3234
this.engineName = documentElement.getElementsByTagName('engine').item(0)?.textContent ?? ''
35+
this.appid = documentElement.getAttribute('appid') ?? ''
3336
this.connection = connection
3437
}
3538
}

0 commit comments

Comments
 (0)