Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 58 additions & 15 deletions lib/internal/debugger/inspect_repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -893,10 +893,32 @@ function createRepl(inspector) {
});
}

function createInitialBreakRenderPromise(pauseRender) {
function finishInitialBreakRenderWait(wait, error) {
if (initialBreakRender !== wait) return;

wait.cleanup();
initialBreakRender = null;
waitForInitialBreakRender = false;
if (error) {
wait.reject(error);
} else {
wait.resolve();
}
}

function createInitialBreakRenderWait(child) {
const { promise, resolve, reject } = PromiseWithResolvers();
initialBreakRender = promise;
PromisePrototypeThen(pauseRender, resolve, reject);
const wait = { promise, resolve, reject, cleanup: null };
const onEnd = () => finishInitialBreakRenderWait(wait);
wait.cleanup = () => {
inspector.client.removeListener('close', onEnd);
child.removeListener('exit', onEnd);
};

initialBreakRender = wait;
waitForInitialBreakRender = true;
inspector.client.once('close', onEnd);
child.once('exit', onEnd);
return promise;
}

Expand Down Expand Up @@ -930,8 +952,14 @@ function createRepl(inspector) {
print(`${header}\n${breakContext}`);
}));

if (waitForInitialBreakRender && !initialBreakRender) {
createInitialBreakRenderPromise(pauseRender);
if (waitForInitialBreakRender && initialBreakRender) {
const wait = initialBreakRender;
waitForInitialBreakRender = false;
PromisePrototypeThen(
pauseRender,
() => finishInitialBreakRenderWait(wait),
(error) => finishInitialBreakRenderWait(wait, error),
);
}
});

Expand All @@ -944,6 +972,14 @@ function createRepl(inspector) {

Debugger.on('breakpointResolved', handleBreakpointResolved);

// Startup can fail before the initial pause is reported, so release the
// startup wait instead of blocking the REPL prompt indefinitely.
Runtime.on('exceptionThrown', () => {
if (waitForInitialBreakRender && initialBreakRender) {
finishInitialBreakRenderWait(initialBreakRender);
}
});
Comment on lines +975 to +981

@inoway46 inoway46 Jun 28, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This keeps sequential/test-debug-prompt from hanging when node inspect foo fails before the initial Debugger.paused render and the REPL would otherwise never reach debug>.

failed log: https://github.com/nodejs/node/actions/runs/28320440600/job/83901478505?pr=64189#step:8:6114


Debugger.on('scriptParsed', (script) => {
const { scriptId, url } = script;
if (url) {
Expand Down Expand Up @@ -1199,10 +1235,15 @@ function createRepl(inspector) {
aliasProperties(context, SHORTCUTS);
}

async function initAfterStart() {
waitForInitialBreakRender =
async function initAfterStart(waitForInitialBreak = false) {
const child = inspector.child;
const initialBreakRenderPromise =
waitForInitialBreak &&
!!inspector.options?.script &&
process.env.NODE_INSPECT_RESUME_ON_START !== '1';
process.env.NODE_INSPECT_RESUME_ON_START !== '1' &&
child?.exitCode === null &&
child?.signalCode === null ?
createInitialBreakRenderWait(child) : null;
await Runtime.enable();
await Profiler.enable();
await Profiler.setSamplingInterval({ interval: 100 });
Expand All @@ -1211,12 +1252,14 @@ function createRepl(inspector) {
await Debugger.setBlackboxPatterns({ patterns: [] });
await Debugger.setPauseOnExceptions({ state: pauseOnExceptionState });
await restoreBreakpoints();
await Runtime.runIfWaitingForDebugger();
await PromiseResolve();
waitForInitialBreakRender = false;
const initialBreakRenderPromise = initialBreakRender;
initialBreakRender = null;
await initialBreakRenderPromise;
try {
await Runtime.runIfWaitingForDebugger();
await initialBreakRenderPromise;
} finally {
if (initialBreakRender) {
finishInitialBreakRenderWait(initialBreakRender);
}
}
}

return async function startRepl() {
Expand All @@ -1225,7 +1268,7 @@ function createRepl(inspector) {
});

// Init once for the initial connection
await initAfterStart();
await initAfterStart(true);

const replOptions = {
prompt: 'debug> ',
Expand Down
91 changes: 90 additions & 1 deletion test/parallel/test-debugger-run-restart-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ function createAgent(domain, calls, gates) {
agent.setBlackboxPatterns = method('setBlackboxPatterns');
agent.setPauseOnExceptions = method('setPauseOnExceptions');
agent.runIfWaitingForDebugger = method('runIfWaitingForDebugger');
agent.getScriptSource = async () => {
calls.push(`${domain}.getScriptSource`);
return { scriptSource: 'let x = 1;\nx = x + 1;\n' };
};
return agent;
}

Expand All @@ -56,6 +60,29 @@ function evalCommand(repl, command) {
});
}

function createChild() {
const child = new EventEmitter();
child.exitCode = null;
child.signalCode = null;
return child;
}

function emitInitialPause(inspector) {
inspector.Debugger.emit('paused', {
reason: 'Break on start',
callFrames: [{
callFrameId: 'call-frame-id',
functionName: '',
location: {
scriptId: '1',
lineNumber: 0,
columnNumber: 0,
},
scopeChain: [],
}],
});
}

async function assertCommandWaitsForInit(repl, command, gate, calls) {
let settled = false;
const promise = evalCommand(repl, command).then(() => {
Expand All @@ -74,18 +101,74 @@ async function assertCommandWaitsForInit(repl, command, gate, calls) {
assert.strictEqual(settled, true);
}

(async () => {
async function assertStartupWaitsForInitialPauseRender() {
const calls = [];
const runtimeGate = createGate();
const pauseRenderGate = createGate();
const gates = [runtimeGate];
const inspector = {
child: createChild(),
client: new EventEmitter(),
domainNames: ['Debugger', 'HeapProfiler', 'Profiler', 'Runtime'],
options: { script: 'script.js' },
stdin: new PassThrough(),
stdout: new PassThrough(),
print(text, addNewline = true) {
inspector.stdout.write(addNewline ? `${text}\n` : text);
},
suspendReplWhile(fn) {
return Promise.resolve(fn()).then(() => pauseRenderGate.promise);
},
};

for (const domain of inspector.domainNames) {
inspector[domain] = createAgent(domain, calls, gates);
}

let repl;
let settled = false;
const promise = createRepl(inspector)().then((createdRepl) => {
repl = createdRepl;
settled = true;
});

runtimeGate.resolve();
await new Promise(setImmediate);
assert.strictEqual(
settled,
false,
`startup resolved before initial pause was observed: ${calls}`,
);

emitInitialPause(inspector);
await new Promise(setImmediate);
assert.strictEqual(
settled,
false,
`startup resolved before initial pause render completed: ${calls}`,
);

pauseRenderGate.resolve();
await promise;
assert.strictEqual(settled, true);
repl.close();
}

async function assertRunAndRestartWaitForInit() {
const calls = [];
const runGate = createGate();
const restartGate = createGate();
const gates = [null, runGate, restartGate];
const inspector = {
child: null,
client: new EventEmitter(),
domainNames: ['Debugger', 'HeapProfiler', 'Profiler', 'Runtime'],
options: {},
stdin: new PassThrough(),
stdout: new PassThrough(),
run: common.mustCall(async () => {
calls.push('inspector.run');
inspector.child = createChild();
}, 2),
suspendReplWhile(fn) {
return fn();
Expand All @@ -97,6 +180,7 @@ async function assertCommandWaitsForInit(repl, command, gate, calls) {
}

const repl = await createRepl(inspector)();
inspector.options = { script: 'script.js' };

await assertCommandWaitsForInit(repl, 'run', runGate, calls);
await assertCommandWaitsForInit(repl, 'restart', restartGate, calls);
Expand All @@ -116,4 +200,9 @@ async function assertCommandWaitsForInit(repl, command, gate, calls) {
);

repl.close();
}

(async () => {
await assertRunAndRestartWaitForInit();
await assertStartupWaitsForInitialPauseRender();
})().then(common.mustCall());
Loading