Skip to content

Commit cbcbca8

Browse files
committed
refactor: improve process detection robustness
- Use PowerShell Get-CimInstance on Windows (WMIC is deprecated) - Add WMIC fallback for older Windows systems - Add -ww flag to ps on Unix to prevent command line truncation - Use regex path matching for deepnote-venvs detection - Prevent false positives with proper path boundary matching
1 parent 887834e commit cbcbca8

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

src/kernels/deepnote/deepnoteServerStarter.node.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -597,26 +597,45 @@ export class DeepnoteServerStarter implements IDeepnoteServerStarter, IExtension
597597
const processService = await this.processServiceFactory.create(undefined);
598598

599599
if (process.platform === 'win32') {
600-
// Windows: use wmic to get command line
601-
const result = await processService.exec(
602-
'wmic',
603-
['process', 'where', `ProcessId=${pid}`, 'get', 'CommandLine'],
604-
{ throwOnStdErr: false }
605-
);
606-
if (result.stdout) {
607-
const cmdLine = result.stdout.toLowerCase();
608-
// Check if it's running from our deepnote-venvs directory or is deepnote_toolkit
609-
return cmdLine.includes('deepnote-venvs') || cmdLine.includes('deepnote_toolkit');
600+
// Windows: prefer PowerShell CIM, fallback to WMIC
601+
let cmdLine = '';
602+
try {
603+
const ps = await processService.exec(
604+
'powershell.exe',
605+
[
606+
'-NoProfile',
607+
'-Command',
608+
`(Get-CimInstance Win32_Process -Filter "ProcessId=${pid}").CommandLine`
609+
],
610+
{ throwOnStdErr: false }
611+
);
612+
cmdLine = (ps.stdout || '').toLowerCase();
613+
} catch {
614+
// Ignore PowerShell errors, will fallback to WMIC
615+
}
616+
if (!cmdLine) {
617+
const result = await processService.exec(
618+
'wmic',
619+
['process', 'where', `ProcessId=${pid}`, 'get', 'CommandLine'],
620+
{ throwOnStdErr: false }
621+
);
622+
cmdLine = (result.stdout || '').toLowerCase();
623+
}
624+
if (cmdLine) {
625+
// Use regex to match path separators for more robust detection
626+
const inVenv = /[\\/](deepnote-venvs)[\\/]/i.test(cmdLine);
627+
return inVenv || cmdLine.includes('deepnote_toolkit');
610628
}
611629
} else {
612-
// Unix-like: use ps to get command line
613-
const result = await processService.exec('ps', ['-p', pid.toString(), '-o', 'command='], {
630+
// Unix-like: use ps with -ww to avoid truncation of long command lines
631+
const result = await processService.exec('ps', ['-ww', '-p', pid.toString(), '-o', 'command='], {
614632
throwOnStdErr: false
615633
});
616634
if (result.stdout) {
617635
const cmdLine = result.stdout.toLowerCase();
618-
// Check if it's running from our deepnote-venvs directory or is deepnote_toolkit
619-
return cmdLine.includes('deepnote-venvs') || cmdLine.includes('deepnote_toolkit');
636+
// Use regex to match path separators for more robust detection
637+
const inVenv = /[\\/](deepnote-venvs)[\\/]/i.test(cmdLine);
638+
return inVenv || cmdLine.includes('deepnote_toolkit');
620639
}
621640
}
622641
} catch (ex) {

0 commit comments

Comments
 (0)