From 4b091bf8faa0e246ba1b966fea24040f240838f2 Mon Sep 17 00:00:00 2001 From: eitsupi Date: Mon, 16 Nov 2026 23:53:25 +0000 Subject: [PATCH 01/11] test(interactive): report display state when Stop Session times out The Stop Session test intermittently times out on macOS while waiting for every display to report that R stopped. Report the remaining displays, the view's status, the output queue, and when the stop notice and each display update reached the notebook, so the failure shows whether the update was late, missing, or never scheduled. --- src/test/suite/interactiveEditor.test.ts | 90 ++++++++++++++++++++---- 1 file changed, 78 insertions(+), 12 deletions(-) diff --git a/src/test/suite/interactiveEditor.test.ts b/src/test/suite/interactiveEditor.test.ts index 81d5ef715..62e6ed88f 100644 --- a/src/test/suite/interactiveEditor.test.ts +++ b/src/test/suite/interactiveEditor.test.ts @@ -3841,26 +3841,91 @@ par(mfrow=c(1,1))`, manifest: manifests[1], } as vscode.QuickPickItem); const info = sinon.stub(vscode.window, 'showInformationMessage').resolves(undefined); + const displaysOf = (outputs: readonly vscode.NotebookCellOutput[]) => + outputs + .flatMap((output) => output.items) + .filter((item) => item.mime === DISPLAY_MIME) + .map( + (item) => + JSON.parse(Buffer.from(item.data).toString()) as Record, + ); + // Record when the stop notice and display updates reach the notebook, so + // a timeout reports which displays stayed running and what was pending. + const stopped = Date.now(); + const timeline: string[] = []; + const changes = vscode.workspace.onDidChangeNotebookDocument((event) => { + if (event.notebook !== notebook) { + return; + } + const at = `${Date.now() - stopped}ms`; + for (const change of event.contentChanges) { + for (const cell of change.addedCells) { + if (cell.metadata.rNoticeKind) { + timeline.push(`${at} notice ${String(cell.metadata.rNoticeKind)}`); + } + } + } + for (const change of event.cellChanges) { + const running = + change.outputs && displaysOf(change.outputs).map((data) => data.running); + if (running?.length) { + timeline.push(`${at} cell ${change.cell.index} running=${running.join(',')}`); + } + } + }); + const displayDiagnostics = (): string => { + const manager = interactiveManager() as unknown as { + views: Map< + string, + { + notebook: vscode.NotebookDocument; + client: AgentClient; + executions: Map; + outputQueue: { + pending: Set; + running?: Promise; + timer?: NodeJS.Timeout; + paused: number; + disposed: boolean; + }; + } + >; + }; + const view = [...manager.views.values()].find((item) => item.notebook === notebook); + const queue = view?.outputQueue; + return [ + `Timed out waiting for every display to stop running after ${Date.now() - stopped}ms`, + `displays: ${JSON.stringify( + notebook.getCells().flatMap((cell) => + displaysOf(cell.outputs).map((data) => ({ + cell: cell.index, + executionId: String(cell.metadata.rExecutionId), + kind: data.kind, + generation: data.generation, + running: data.running, + connected: data.connected, + })), + ), + )}`, + view + ? `view: status=${view.client.manifest.status} connected=${view.client.connected} executions=${view.executions.size}` + : 'view: none', + queue + ? `queue: pending=${JSON.stringify([...queue.pending])} running=${queue.running !== undefined} timer=${queue.timer !== undefined} paused=${queue.paused} disposed=${queue.disposed}` + : 'queue: none', + `timeline: ${timeline.join('; ') || 'none'}`, + ].join('\n'); + }; try { await vscode.commands.executeCommand('r.interactive.stop', notebook.uri); await until(() => notebook.getCells().some((cell) => cell.metadata.rNoticeKind === 'stopped'), ); const displays = (): Record[] => - notebook - .getCells() - .flatMap((cell) => cell.outputs) - .flatMap((output) => output.items) - .filter((item) => item.mime === DISPLAY_MIME) - .map( - (item) => - JSON.parse(Buffer.from(item.data).toString()) as Record< - string, - unknown - >, - ); + notebook.getCells().flatMap((cell) => displaysOf(cell.outputs)); await until( () => displays().length > 0 && displays().every((data) => data.running === false), + displayDiagnostics, ); const ended = manifests[0].ended; assert.ok(typeof ended === 'number' && ended >= manifests[0].created); @@ -3918,6 +3983,7 @@ par(mfrow=c(1,1))`, client.close(); } } finally { + changes.dispose(); confirmation.restore(); picker.restore(); info.restore(); From 28e48371714ffa151331dd1ec36f57dc99b2f180 Mon Sep 17 00:00:00 2001 From: eitsupi Date: Tue, 17 Nov 2026 00:14:10 +0000 Subject: [PATCH 02/11] test(help): report pending fake timers when Help panel tests fail Help panel tests intermittently find one pending fake timer after the navigation under test has completed. Fake timers replace setTimeout for the whole extension host, so report each pending timer's type, delay and callback source to tell a Help timer apart from one scheduled by unrelated code. --- src/test/suite/helpPanel.test.ts | 39 +++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/test/suite/helpPanel.test.ts b/src/test/suite/helpPanel.test.ts index 6227cff57..8878670d6 100644 --- a/src/test/suite/helpPanel.test.ts +++ b/src/test/suite/helpPanel.test.ts @@ -23,6 +23,33 @@ suite('Help panel navigation', () => { return { promise, resolve }; } + // Fake timers replace setTimeout for the whole extension host. On failure, + // report what is pending so a Help timer can be told apart from one that + // unrelated code scheduled during the test. + function assertNoPendingTimers(clock: sinon.SinonFakeTimers): void { + const count = clock.countTimers(); + if (count === 0) { + return; + } + const internals = clock as unknown as { + timerHeap?: { + timers: { type?: string; delay?: number; callAt?: number; func: unknown }[]; + }; + jobs?: { func: unknown }[]; + }; + const source = (func: unknown) => String(func).replace(/\s+/g, ' ').slice(0, 300); + assert.fail( + [ + `Expected no pending fake timers at ${clock.now}, found ${count}:`, + ...(internals.timerHeap?.timers ?? []).map( + (timer) => + `${timer.type} delay=${timer.delay} callAt=${timer.callAt}: ${source(timer.func)}`, + ), + ...(internals.jobs ?? []).map((job) => `job: ${source(job.func)}`), + ].join('\n'), + ); + } + async function settleNavigation(navigation: Promise): Promise { await navigation; await new Promise((resolve) => setImmediate(resolve)); @@ -81,7 +108,7 @@ suite('Help panel navigation', () => { await clock.tickAsync(1000); await pending; assert.match(panel.webview.html, /First/); - assert.strictEqual(clock.countTimers(), 0); + assertNoPendingTimers(clock); }); test('closing the panel settles pending navigation without waiting for a timeout', async () => { @@ -90,7 +117,7 @@ suite('Help panel navigation', () => { panel.dispose(); await pending; assert.strictEqual(help.panel, undefined); - assert.strictEqual(clock.countTimers(), 0); + assertNoPendingTimers(clock); }); test('failed delivery falls back immediately instead of waiting for the scroll timeout', async () => { @@ -99,7 +126,7 @@ suite('Help panel navigation', () => { await help.goBack(); await clock.tickAsync(0); assert.match(panel.webview.html, /First/); - assert.strictEqual(clock.countTimers(), 0); + assertNoPendingTimers(clock); }); test('closing Help during a refresh fetch does not recreate its panel', async () => { @@ -169,7 +196,7 @@ suite('Help panel navigation', () => { await newer; assert.strictEqual(htmlAfterOlder, original); assert.match(panel.webview.html, /Latest preview/); - assert.strictEqual(clock.countTimers(), 0); + assertNoPendingTimers(clock); }); test('closing Help during a refresh scroll wait leaves it closed and cancels its timer', async () => { @@ -182,7 +209,7 @@ suite('Help panel navigation', () => { panel.dispose(); await refresh; assert.strictEqual(help.panel, undefined); - assert.strictEqual(clock.countTimers(), 0); + assertNoPendingTimers(clock); sinon.assert.notCalled(create); }); @@ -207,7 +234,7 @@ suite('Help panel navigation', () => { await clock.tickAsync(1000); await refresh; assert.match(panel.webview.html, /Third/); - assert.strictEqual(clock.countTimers(), 0); + assertNoPendingTimers(clock); }); test('closing during async page preparation prevents a late write to the disposed webview', async () => { From 5d9633bc062ebc968a877dc4123214577d241ffe Mon Sep 17 00:00:00 2001 From: eitsupi Date: Tue, 17 Nov 2026 00:49:41 +0000 Subject: [PATCH 03/11] test(activation): wait for the restored R terminal to accept input The persistent terminal reload case sent its state check once, as soon as the restored terminal reported a PID. VS Code can drop input while it replays a revived terminal's buffer, so a single early send may never reach R. Resend an idempotent probe until R answers, then send the state check, and report R errors from that check instead of timing out. On failure, keep the pty host trace logs, the phase and probe files, a timeline of sent input, and the terminal's process state so the next CI failure shows whether input reached R. --- .github/workflows/main.yml | 2 +- scripts/test-activation.cjs | 105 +++++++++++++++++++++++++++++++----- 2 files changed, 93 insertions(+), 14 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 12c46bceb..68f2e643e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -134,7 +134,7 @@ jobs: with: name: extension-test-logs-${{ runner.os }} path: | - .vscode-test/activation/*.log + .vscode-test/activation/** .vscode-test/user-data/**/logs/**/exthost/exthost.log .vscode-test/user-data/**/logs/**/exthost/output_logging_*/*.log retention-days: 7 diff --git a/scripts/test-activation.cjs b/scripts/test-activation.cjs index 56453bfe1..67d177175 100644 --- a/scripts/test-activation.cjs +++ b/scripts/test-activation.cjs @@ -42,6 +42,33 @@ async function within(promise, message) { } finally { clearTimeout(timer); } } +// Record the terminal's process tree so a failed CI run shows whether R was +// alive and, on macOS, where its main thread was waiting. +function recordTerminalProcesses(root, terminalPid) { + if (process.platform === 'win32') { return; } + const { spawnSync } = require('node:child_process'); + const output = path.join(root, 'terminal-processes.log'); + const run = (command, args) => { + const result = spawnSync(command, args, { encoding: 'utf8', timeout: 15000 }); + fs.appendFileSync(output, `$ ${command} ${args.join(' ')}\n${result.stdout || ''}${result.stderr || ''}` + + `${result.error ? `${result.error}\n` : ''}\n`); + return result.stdout || ''; + }; + const table = run('ps', ['-A', '-o', 'pid=,ppid=,stat=,command=']); + const parents = new Map(table.split('\n').map((line) => line.trim().split(/\s+/)) + .filter((fields) => fields.length > 1).map(([pid, ppid]) => [pid, ppid])); + const descendants = [...parents.keys()].filter((pid) => { + for (let current = pid; current; current = parents.get(current)) { + if (current === String(terminalPid)) { return pid !== String(terminalPid); } + if (current === '1' || current === '0') { return false; } + } + return false; + }); + if (process.platform === 'darwin') { + descendants.forEach((pid) => run('sample', [pid, '2'])); + } +} + exports.run = async (api) => { const vscode = api?.extensions ? api : require('vscode'); const testCase = process.env.VSCR_ACTIVATION_CASE; @@ -116,14 +143,52 @@ exports.run = async (api) => { await until(() => vscode.window.terminals.length > 0, 'Persistent R Terminal was not restored'); const terminal = vscode.window.terminals[0]; const terminalPid = await within(terminal.processId, 'Restored terminal PID was not available'); - const stateFile = path.join(root, reloading ? 'terminal-after' : 'terminal-before'); - const rFile = JSON.stringify(stateFile.replace(/\\/g, '/')); - const rTemporary = JSON.stringify(`${stateFile}.pending`.replace(/\\/g, '/')); - terminal.sendText(`${reloading ? '' : '.vscode_r_activation_marker <- 42; '}` + + const previous = reloading ? JSON.parse(fs.readFileSync(phaseFile, 'utf8')) : undefined; + if (reloading) { + assert.equal(terminalPid, previous.terminalPid, 'Window reload must retain the terminal process'); + } + const phase = reloading ? 'after' : 'before'; + const events = path.join(root, 'terminal-events.log'); + const record = (event) => fs.appendFileSync(events, `${new Date().toISOString()} ${phase} ${event}\n`); + const rPath = (file) => JSON.stringify(file.replace(/\\/g, '/')); + // A known PID only means the pty exists. While VS Code replays a + // revived terminal's buffer it can drop input, so wait until R + // answers an idempotent probe before sending the state check. + const probeFile = path.join(root, `terminal-probe-${phase}`); + const probe = `writeLines(as.character(Sys.getpid()), ${rPath(`${probeFile}.pending`)}); ` + + `invisible(file.rename(${rPath(`${probeFile}.pending`)}, ${rPath(probeFile)}))`; + const probeDeadline = Date.now() + 30000; + let probes = 0; + while (!fs.existsSync(probeFile)) { + if (Date.now() >= probeDeadline) { + recordTerminalProcesses(root, terminalPid); + assert.fail(`R terminal did not answer ${probes} input probes`); + } + terminal.sendText(probe); + record(`probe ${++probes} sent`); + const resend = Date.now() + 2000; + while (!fs.existsSync(probeFile) && Date.now() < resend) { await pause(50); } + } + record(`probe answered by R ${fs.readFileSync(probeFile, 'utf8').trim()}`); + const stateFile = path.join(root, `terminal-${phase}`); + const errorFile = `${stateFile}.error`; + terminal.sendText('tryCatch({ ' + + `${reloading ? '' : '.vscode_r_activation_marker <- 42; '}` + `writeLines(c(as.character(Sys.getpid()), get('.session_id', asNamespace('sess'))(), ` + - `as.character(.vscode_r_activation_marker)), ${rTemporary}); ` + - `stopifnot(file.rename(${rTemporary}, ${rFile}))`); - await until(() => fs.existsSync(stateFile), 'R terminal did not report its retained state'); + `as.character(.vscode_r_activation_marker)), ${rPath(`${stateFile}.pending`)}); ` + + `stopifnot(file.rename(${rPath(`${stateFile}.pending`)}, ${rPath(stateFile)})) ` + + `}, error = function(e) writeLines(conditionMessage(e), ${rPath(errorFile)}))`); + record('state check sent'); + try { + await until(() => fs.existsSync(stateFile) || fs.existsSync(errorFile), + 'R terminal did not report its retained state'); + } catch (error) { + recordTerminalProcesses(root, terminalPid); + throw error; + } + assert.ok(!fs.existsSync(errorFile), fs.existsSync(errorFile) && + `R terminal state check failed: ${fs.readFileSync(errorFile, 'utf8')}`); + record('state reported'); const [pid, sessionId, marker] = fs.readFileSync(stateFile, 'utf8').trim().split(/\r?\n/); assert.equal(marker, '42', 'Window reload must retain the R workspace'); if (!reloading) { @@ -133,8 +198,6 @@ exports.run = async (api) => { connectionId: extension.exports.session.getSessionReference(sessionId).connectionId })); return 'reload'; } - const previous = JSON.parse(fs.readFileSync(phaseFile, 'utf8')); - assert.equal(terminalPid, previous.terminalPid, 'Window reload must retain the terminal process'); assert.equal(pid, previous.pid, 'Window reload must retain the same R process'); assert.equal(sessionId, previous.sessionId, 'The retained R process must keep its sess identity'); assert.equal(extension.isActive, false, 'Restored terminal waits for explicit R activation'); @@ -332,6 +395,8 @@ exports.activate = async () => { '--user-data-dir', profile, '--extensions-dir', extensions, '--disable-renderer-backgrounding', '--disable-background-timer-throttling', '--disable-backgrounding-occluded-windows', + // Pty host trace logs show whether reload-time input reached R. + ...(persistentTerminal ? ['--log', 'trace'] : []), ]; const log = fs.createWriteStream(path.join(caseRoot, `${testCase}.log`)); try { @@ -370,10 +435,24 @@ exports.activate = async () => { fs.mkdirSync(artifacts, { recursive: true }); for (const directory of fs.readdirSync(root, { withFileTypes: true })) { if (!directory.isDirectory()) { continue; } - for (const file of fs.readdirSync(path.join(root, directory.name))) { - if (file.endsWith('.log')) { - fs.copyFileSync(path.join(root, directory.name, file), - path.join(artifacts, `${directory.name}-${file}`)); + const caseRoot = path.join(root, directory.name); + if (directory.name === 'terminal-persistent-reload') { + fs.mkdirSync(path.join(artifacts, directory.name), { recursive: true }); + } + for (const file of fs.readdirSync(caseRoot, { withFileTypes: true })) { + if (file.name.endsWith('.log')) { + fs.copyFileSync(path.join(caseRoot, file.name), + path.join(artifacts, `${directory.name}-${file.name}`)); + } else if (directory.name === 'terminal-persistent-reload' && file.isFile()) { + // Phase and probe files show how far each window got. + fs.copyFileSync(path.join(caseRoot, file.name), + path.join(artifacts, directory.name, file.name)); + } + } + if (directory.name === 'terminal-persistent-reload') { + const logs = path.join(caseRoot, 'profile', 'logs'); + if (fs.existsSync(logs)) { + fs.cpSync(logs, path.join(artifacts, directory.name, 'logs'), { recursive: true }); } } } From a11386ae99af7e6790753fa11b238e47c9f9180a Mon Sep 17 00:00:00 2001 From: eitsupi Date: Tue, 17 Nov 2026 01:26:33 +0000 Subject: [PATCH 04/11] test(activation): wait for the terminal layout save before reloading VS Code saves the terminal layout through a debounced call that a window reload does not flush. On macOS CI, resizes kept postponing that save until the test reloaded, so the R terminal was missing from the saved layout, was not revived, and the test read a new default shell as terminals[0]. Before reloading, wait until the pty host trace log shows an accepted layout that includes the R terminal's persistent process. After reloading, select the terminal by the PID recorded before the reload, and report the open terminals if none matches. Also sample the terminal PID itself on macOS, since the R Terminal profile runs R directly rather than as a child process. --- scripts/test-activation.cjs | 77 ++++++++++++++++++++++++++++++++----- 1 file changed, 68 insertions(+), 9 deletions(-) diff --git a/scripts/test-activation.cjs b/scripts/test-activation.cjs index 67d177175..83cb9438f 100644 --- a/scripts/test-activation.cjs +++ b/scripts/test-activation.cjs @@ -43,7 +43,8 @@ async function within(promise, message) { } // Record the terminal's process tree so a failed CI run shows whether R was -// alive and, on macOS, where its main thread was waiting. +// alive and, on macOS, where its main thread was waiting. The R Terminal +// profile runs R directly, so the terminal PID is usually R itself. function recordTerminalProcesses(root, terminalPid) { if (process.platform === 'win32') { return; } const { spawnSync } = require('node:child_process'); @@ -57,18 +58,49 @@ function recordTerminalProcesses(root, terminalPid) { const table = run('ps', ['-A', '-o', 'pid=,ppid=,stat=,command=']); const parents = new Map(table.split('\n').map((line) => line.trim().split(/\s+/)) .filter((fields) => fields.length > 1).map(([pid, ppid]) => [pid, ppid])); - const descendants = [...parents.keys()].filter((pid) => { + const tree = [...parents.keys()].filter((pid) => { for (let current = pid; current; current = parents.get(current)) { - if (current === String(terminalPid)) { return pid !== String(terminalPid); } + if (current === String(terminalPid)) { return true; } if (current === '1' || current === '0') { return false; } } return false; }); if (process.platform === 'darwin') { - descendants.forEach((pid) => run('sample', [pid, '2'])); + tree.forEach((pid) => run('sample', [pid, '2'])); } } +// VS Code saves the terminal layout through a debounced call that a window +// reload does not flush, and no extension API reports the save. A terminal +// missing from the saved layout is not revived, so read the pty host trace log +// (this case runs with --log trace) to find whether the last accepted layout +// includes the persistent process whose OS PID is `pid`. +function terminalLayoutState(logs, pid) { + const sessions = fs.existsSync(logs) ? fs.readdirSync(logs).sort() : []; + const file = sessions.length ? path.join(logs, sessions[sessions.length - 1], 'ptyhost.log') : undefined; + const state = { file, id: undefined, saved: false, pending: undefined, layout: undefined }; + if (!file || !fs.existsSync(file)) { return state; } + const json = (text) => { try { return JSON.parse(text); } catch { return undefined; } }; + for (const line of fs.readFileSync(file, 'utf8').split('\n')) { + const ready = /PtyService#_onProcessReady\.fire\((\{.*\})\)\s*$/.exec(line); + if (ready) { + const event = json(ready[1]); + if (event?.event?.pid === pid) { state.id = event.id; } + continue; + } + const request = /\[RPC Request\] PtyService#setTerminalLayoutInfo\((\{.*\})\)\s*$/.exec(line); + if (request) { + state.layout = request[1]; + state.pending = state.id !== undefined && (json(request[1])?.tabs ?? []).some((tab) => + (tab.terminals ?? []).some((entry) => entry.terminal === state.id)); + } else if (/\[trace\] \[RPC Response\] PtyService#setTerminalLayoutInfo undefined\s*$/.test(line) && state.pending !== undefined) { + state.saved = state.pending; + state.pending = undefined; + } + } + return state; +} + exports.run = async (api) => { const vscode = api?.extensions ? api : require('vscode'); const testCase = process.env.VSCR_ACTIVATION_CASE; @@ -140,12 +172,30 @@ exports.run = async (api) => { { config: { extensionIdentifier: 'REditorSupport.r', id: 'r.terminal-profile', title: 'R Terminal' } }), 'Persistent R Terminal creation timed out'); } - await until(() => vscode.window.terminals.length > 0, 'Persistent R Terminal was not restored'); - const terminal = vscode.window.terminals[0]; - const terminalPid = await within(terminal.processId, 'Restored terminal PID was not available'); const previous = reloading ? JSON.parse(fs.readFileSync(phaseFile, 'utf8')) : undefined; - if (reloading) { - assert.equal(terminalPid, previous.terminalPid, 'Window reload must retain the terminal process'); + let terminal; + let terminalPid; + if (!reloading) { + await until(() => vscode.window.terminals.length > 0, 'Persistent R Terminal was not created'); + terminal = vscode.window.terminals[0]; + terminalPid = await within(terminal.processId, 'Persistent R Terminal PID was not available'); + } else { + // The terminal order says nothing about which process was + // revived; VS Code may also open a new default shell. + const deadline = Date.now() + 15000; + while (!terminal) { + const candidates = await Promise.all(vscode.window.terminals.map(async (candidate) => + ({ candidate, pid: await Promise.race([candidate.processId, pause(200)]) }))); + terminal = candidates.find(({ pid }) => pid === previous.terminalPid)?.candidate; + if (!terminal) { + if (Date.now() >= deadline) { recordTerminalProcesses(root, previous.terminalPid); } + assert.ok(Date.now() < deadline, `Window reload did not revive the R terminal ` + + `(PID ${previous.terminalPid}); terminals: ${JSON.stringify(candidates.map( + ({ candidate, pid }) => ({ name: candidate.name, pid })))}`); + await pause(100); + } + } + terminalPid = previous.terminalPid; } const phase = reloading ? 'after' : 'before'; const events = path.join(root, 'terminal-events.log'); @@ -194,6 +244,15 @@ exports.run = async (api) => { if (!reloading) { await until(() => extension.exports.session.getSessionReference(sessionId), 'Initial R terminal sess attachment did not complete'); + const logs = path.join(root, 'profile', 'logs'); + const layoutDeadline = Date.now() + 15000; + let layout; + while (!(layout = terminalLayoutState(logs, terminalPid)).saved || layout.pending === false) { + assert.ok(Date.now() < layoutDeadline, 'VS Code did not save a terminal layout with the ' + + `R terminal before reload: ${JSON.stringify(layout)}`); + await pause(100); + } + record(`layout with persistent terminal ${layout.id} saved`); fs.writeFileSync(phaseFile, JSON.stringify({ terminalPid, pid, sessionId, connectionId: extension.exports.session.getSessionReference(sessionId).connectionId })); return 'reload'; From 55590d2ceeb8faaa51837bf55e52c2cddca474b3 Mon Sep 17 00:00:00 2001 From: eitsupi Date: Tue, 17 Nov 2026 02:03:41 +0000 Subject: [PATCH 05/11] test(lsp): cover a server that exits after answering shutdown R's languageserver exits once it has answered shutdown, so the client can fail to write the exit notification and see the connection reset. On Windows this showed a "connection to server is erroring" notification and failed the stop with ECONNABORTED during the Interactive editor suite. Fix that order in a test with a real owned child process: the client must stop without a user-facing error when the server exits by itself, still fail when the server keeps running, and still report a reset while the server is running. --- src/test/suite/languageService.test.ts | 154 +++++++++++++++++++++++++ 1 file changed, 154 insertions(+) diff --git a/src/test/suite/languageService.test.ts b/src/test/suite/languageService.test.ts index a4fd7b4ac..1f52b2777 100644 --- a/src/test/suite/languageService.test.ts +++ b/src/test/suite/languageService.test.ts @@ -2,13 +2,21 @@ import * as assert from 'node:assert'; import { EventEmitter } from 'node:events'; import * as sinon from 'sinon'; import * as vscode from 'vscode'; +import { + createMessageConnection, + State, + StreamMessageReader, + StreamMessageWriter, +} from 'vscode-languageclient/node'; import type { LanguageClient } from 'vscode-languageclient/node'; import childProcess from 'node:child_process'; +import * as net from 'node:net'; import * as fs from 'node:fs/promises'; import * as path from 'node:path'; import * as os from 'node:os'; import { LanguageService } from '../../languageService'; import { connectTcpLanguageServer } from '../../languageServerTransport'; +import { CancellableLanguageClient } from '../../languageServerClient'; import * as util from '../../util'; import { mockExtensionContext } from '../common/mockvscode'; @@ -427,3 +435,149 @@ suite('Language service initialization cancellation', () => { watcherDisposals.forEach((dispose) => sinon.assert.calledOnce(dispose)); }); }); + +suite('Language client stop', () => { + let sandbox: sinon.SinonSandbox; + let errors: sinon.SinonStub; + let cleanup: Array<() => unknown>; + + setup(() => { + sandbox = sinon.createSandbox(); + errors = sandbox.stub(vscode.window, 'showErrorMessage').resolves(undefined); + cleanup = []; + }); + + teardown(async () => { + try { + for (const release of cleanup.reverse()) { + await release(); + } + } finally { + sandbox.restore(); + } + }); + + const disconnect = (code: string) => Object.assign(new Error(`socket ${code}`), { code }); + + /** + * A TCP language server owned by a real child process, like R's server. + * The child exits only when the test calls `exitChild()`. With + * `closeAtExitNotification`, the client sees the order R can produce: the + * server has answered shutdown and closed, so reading resets and writing the + * exit notification fails. + */ + async function startServer(options: { + closeAtExitNotification?: { exitChild: boolean }; + }): Promise<{ + client: CancellableLanguageClient; + child: childProcess.ChildProcess; + errorHandler: sinon.SinonSpy; + serverSocket: () => net.Socket; + }> { + const channel = vscode.window.createOutputChannel('Test language client stop', { + log: true, + }); + cleanup.push(() => channel.dispose()); + const child = childProcess.spawn( + process.execPath, + ['-e', "process.stdin.on('end', () => process.exit(0)); process.stdin.resume();"], + { + env: { ...process.env, ELECTRON_RUN_AS_NODE: '1' }, + stdio: ['pipe', 'ignore', 'ignore'], + }, + ); + const closed = new Promise((resolve) => child.once('close', () => resolve())); + cleanup.push(async () => { + child.kill('SIGKILL'); + await closed; + }); + let serverSocket: net.Socket | undefined; + const connections: Array<{ dispose(): void }> = []; + const server = net.createServer((socket) => { + serverSocket = socket; + socket.on('error', () => undefined); + const connection = createMessageConnection( + new StreamMessageReader(socket), + new StreamMessageWriter(socket), + ); + connections.push(connection); + connection.onRequest('initialize', () => ({ capabilities: {} })); + connection.onRequest('shutdown', () => null); + connection.listen(); + }); + await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); + cleanup.push(async () => { + connections.forEach((connection) => connection.dispose()); + serverSocket?.destroy(); + await new Promise((resolve) => server.close(() => resolve())); + }); + const client: CancellableLanguageClient = new CancellableLanguageClient( + 'stop-test', + 'Stop test', + async () => { + client.ownProcess( + Object.assign(child, { + dispose: () => child.kill('SIGKILL'), + }) as unknown as util.DisposableProcess, + ); + const socket = net.connect((server.address() as net.AddressInfo).port, '127.0.0.1'); + await new Promise((resolve) => socket.once('connect', resolve)); + client.ownSocket(socket); + const writer = new StreamMessageWriter(socket); + const close = options.closeAtExitNotification; + if (close) { + const write = writer.write.bind(writer); + writer.write = async (message) => { + if (!('method' in message) || message.method !== 'exit') { + return write(message); + } + if (close.exitChild) { + child.stdin!.end(); + await closed; + } + socket.emit('error', disconnect('ECONNRESET')); + throw disconnect('EPIPE'); + }; + } + return { reader: new StreamMessageReader(socket), writer }; + }, + { documentSelector: [], outputChannel: channel }, + new AbortController().signal, + ); + const errorHandler = sandbox.spy(client.clientOptions.errorHandler!, 'error'); + await client.start(); + cleanup.push(() => client.stop().catch(() => undefined)); + return { client, child, errorHandler, serverSocket: () => serverSocket! }; + } + + test('a server that exits after answering shutdown stops without a user-facing error', async () => { + const { client, child, errorHandler } = await startServer({ + closeAtExitNotification: { exitChild: true }, + }); + await client.stop(); + assert.strictEqual(client.state, State.Stopped); + assert.strictEqual(child.exitCode, 0, 'The server must exit by itself, not be killed'); + sinon.assert.calledWithMatch(errorHandler, sinon.match.has('code', 'ECONNRESET')); + // The client reports connection errors after its handler returns. + await new Promise((resolve) => setImmediate(resolve)); + sinon.assert.notCalled(errors); + }); + + test('a server that answers shutdown but keeps running still fails the stop', async () => { + const { client } = await startServer({ closeAtExitNotification: { exitChild: false } }); + await assert.rejects( + client.stop(), + (error) => (error as { code?: string }).code === 'EPIPE', + ); + }); + + test('a reset connection while running is still reported', async () => { + const { serverSocket } = await startServer({}); + serverSocket().resetAndDestroy(); + const deadline = Date.now() + 5000; + while (!errors.called) { + assert.ok(Date.now() < deadline, 'An unexpected disconnect must be reported'); + await new Promise((resolve) => setTimeout(resolve, 20)); + } + }); +}); From cdd50f44bd990bfd4868ce480e96c768a242ef29 Mon Sep 17 00:00:00 2001 From: eitsupi Date: Tue, 17 Nov 2026 02:13:48 +0000 Subject: [PATCH 06/11] fix(lsp): stop quietly when R's server exits after answering shutdown R's languageserver exits as soon as it has answered shutdown, so writing the exit notification can fail and the connection can be reset. On Windows this showed users a "connection to server is erroring" notification whenever a server was stopped, such as when an Interactive notebook was closed, and made the stop reject with ECONNABORTED. Treat that as a completed stop only when the server answered shutdown, the error is a known disconnect, and every owned server process exited before any forced termination. During a requested stop, log disconnect errors to the output channel instead of notifying. A reset while the server is running is still reported. Track stops of clients removed from the language service, log their failures, and wait for them on disposal. A rebind no longer skips starting the new server when stopping the old one fails. --- src/languageServerClient.ts | 94 +++++++++++++++++++++++++++++++++---- src/languageService.ts | 21 +++++++-- 2 files changed, 101 insertions(+), 14 deletions(-) diff --git a/src/languageServerClient.ts b/src/languageServerClient.ts index 0c81eeeb5..fab485b70 100644 --- a/src/languageServerClient.ts +++ b/src/languageServerClient.ts @@ -1,9 +1,20 @@ -import { LanguageClient, State } from 'vscode-languageclient/node'; +import { ErrorAction, LanguageClient, State } from 'vscode-languageclient/node'; import type { LanguageClientOptions, ServerOptions } from 'vscode-languageclient/node'; import type { Socket } from 'node:net'; import type { ChildProcess } from 'node:child_process'; import type { DisposableProcess } from './util'; +/** Transport errors that mean the peer closed or reset the connection. */ +const DISCONNECTS = new Set(['EPIPE', 'ECONNRESET', 'ECONNABORTED']); + +function disconnectCode(error: unknown): string | undefined { + const code = + error && typeof error === 'object' && 'code' in error + ? (error as { code: unknown }).code + : undefined; + return typeof code === 'string' && DISCONNECTS.has(code) ? code : undefined; +} + function cancelledStartup(): Error { const error = new Error('R language server startup was cancelled.'); error.name = 'AbortError'; @@ -15,7 +26,14 @@ class StartupResources { readonly cancellation = new AbortController(); private readonly releases: Array<() => void> = []; private readonly exits: Promise[] = []; + /** Owned server processes and whether each has closed. */ + private readonly processes = new Map(); private disposed = false; + /** Set when stop() begins for the connection these resources belong to. */ + stopRequested = false; + /** The exit notification is written only after shutdown was answered. */ + exitSent = false; + stopping?: Promise; constructor( private readonly parent: AbortSignal, @@ -56,18 +74,28 @@ class StartupResources { } watchProcess(child: ChildProcess): void { - if (this.disposed || child.exitCode !== null || child.signalCode !== null) { + if (this.disposed || this.processes.has(child)) { + return; + } + const exited = child.exitCode !== null || child.signalCode !== null; + this.processes.set(child, exited); + if (exited) { return; } this.exits.push( new Promise((resolve) => { - child.once('close', resolve); - this.own(() => child.removeListener('close', resolve)); + const closed = () => { + this.processes.set(child, true); + resolve(); + }; + child.once('close', closed); + this.own(() => child.removeListener('close', closed)); }), ); } - async waitForExit(): Promise { + /** Waits briefly for owned processes; true if there is one and all of them closed. */ + async waitForExit(): Promise { let timer: NodeJS.Timeout | undefined; try { // shutdown()/exit() acknowledge transport writes, not process exit. @@ -82,6 +110,7 @@ class StartupResources { } finally { clearTimeout(timer); } + return this.processes.size > 0 && [...this.processes.values()].every(Boolean); } throwIfCancelled(): void { @@ -116,6 +145,22 @@ export class CancellableLanguageClient extends LanguageClient { private readonly cancellation: AbortSignal, ) { super(id, name, serverOptions, clientOptions); + const handler = this.clientOptions.errorHandler!; + this.clientOptions.errorHandler = { + error: (error, message, count) => { + // A server can close its end once it has answered shutdown. + // That is the requested outcome, not a failure to report. + const code = disconnectCode(error); + if (code && this.resources?.stopRequested) { + this.outputChannel.appendLine( + `The R language server connection closed while stopping (${code}).`, + ); + return { action: ErrorAction.Continue, handled: true }; + } + return handler.error(error, message, count); + }, + closed: () => handler.closed(), + }; } override start(): Promise { @@ -183,6 +228,13 @@ export class CancellableLanguageClient extends LanguageClient { protected override async createMessageTransports(encoding: string) { const resources = this.resources; const transports = await super.createMessageTransports(encoding); + const write = transports.writer.write.bind(transports.writer); + transports.writer.write = (message) => { + if (resources && 'method' in message && message.method === 'exit') { + resources.exitSent = true; + } + return write(message); + }; // Executable-based stdio starts inside LanguageClient. Its public process // handle is available once transports exist, before initialize is sent. const child = this.serverProcess; @@ -198,20 +250,42 @@ export class CancellableLanguageClient extends LanguageClient { return transports; } - override async stop(timeout?: number): Promise { + override stop(timeout?: number): Promise { const resources = this.resources; if (this.state === State.Starting || this.state === State.StartFailed) { // LanguageClient's initialization failure handler also calls stop(). // Its base implementation rejects in these states; closing owned // transports instead lets the pending initialize request reject. resources?.dispose(); - return; + return Promise.resolve(); } + if (!resources) { + return super.stop(timeout); + } + return (resources.stopping ??= this.stopRunning(resources, timeout)); + } + + private async stopRunning(resources: StartupResources, timeout?: number): Promise { + resources.stopRequested = true; try { - await super.stop(timeout); - await resources?.waitForExit(); + try { + await super.stop(timeout); + } catch (error) { + // R's languageserver exits once it answers shutdown, so writing + // the exit notification can fail. That still stopped the server + // if it answered shutdown and its process closed by itself. + const code = disconnectCode(error); + if (!code || !resources.exitSent || !(await resources.waitForExit())) { + throw error; + } + this.outputChannel.appendLine( + `The R language server exited after answering shutdown, before the exit notification (${code}).`, + ); + return; + } + await resources.waitForExit(); } finally { - resources?.dispose(); + resources.dispose(); } } } diff --git a/src/languageService.ts b/src/languageService.ts index 20d78e92f..a7c9280bd 100644 --- a/src/languageService.ts +++ b/src/languageService.ts @@ -52,6 +52,7 @@ export class LanguageService implements Disposable { private readonly startup: Promise; private readonly startupCancellation = new AbortController(); private readonly startingClients = new Set(); + private readonly stoppingClients = new Set>(); private disposed = false; constructor() { @@ -72,6 +73,16 @@ export class LanguageService implements Disposable { return this.stopLanguageService(); } + /** Stops a client removed from the service; disposal still waits for it. */ + private stopClient(client: LanguageClient): Promise { + const stopping = client.stop().catch((error: unknown) => { + this.outputChannel.appendLine(`Failed to stop language server: ${String(error)}`); + }); + this.stoppingClients.add(stopping); + void stopping.finally(() => this.stoppingClients.delete(stopping)); + return stopping; + } + private spawnServer( client: LanguageClient, rPath: string, @@ -539,7 +550,7 @@ export class LanguageService implements Disposable { if (client) { this.clients.delete(key); this.initSet.delete(key); - void client.stop(); + void this.stopClient(client); } }; @@ -560,7 +571,9 @@ export class LanguageService implements Disposable { const client = this.clients.get(key); this.clients.delete(key); this.initSet.delete(key); - await client?.stop(); + if (client) { + await this.stopClient(client); + } } await didOpenTextDocument(document); if (this.clients.has(key)) { @@ -606,7 +619,7 @@ export class LanguageService implements Disposable { if (client) { this.clients.delete(key); this.initSet.delete(key); - void client.stop(); + void this.stopClient(client); } } }), @@ -652,7 +665,7 @@ export class LanguageService implements Disposable { private async stopLanguageService(): Promise { await this.startup; await Promise.all(this.clientUpdates.values()); - const promises: Thenable[] = []; + const promises: Thenable[] = [...this.stoppingClients]; for (const client of this.clients.values()) { promises.push(client.stop()); } From 444166d462dbfc70c2b772737a52357e774432b7 Mon Sep 17 00:00:00 2001 From: eitsupi Date: Tue, 17 Nov 2026 02:40:27 +0000 Subject: [PATCH 07/11] test(plot): report the jgd canvas timeline when hidden-tab restoration fails The retained jgd tab test intermittently found the canvas at 207x116 while its container had returned to 966x542 after the tab was revealed. The failure did not show whether the viewer drew at a temporary size, whether it asked R to resize, or whether a later replay corrected the canvas. Record each replay's start and end with the container and canvas sizes, the viewer's resize reports, visibility messages, and container resizes. When an assertion after the reveal fails, inspect again after one second and attach both inspections, the resize calls R received, and the timeline to the failure. --- src/test/plot/plotRestoration.test.ts | 96 ++++++++++++++++++++++----- 1 file changed, 78 insertions(+), 18 deletions(-) diff --git a/src/test/plot/plotRestoration.test.ts b/src/test/plot/plotRestoration.test.ts index 3187cdd31..830fdd98b 100644 --- a/src/test/plot/plotRestoration.test.ts +++ b/src/test/plot/plotRestoration.test.ts @@ -2600,6 +2600,7 @@ suite('Standalone plot restoration', () => { container: number[]; canvas: number[]; pixel: number[]; + log: unknown[]; } let panel: vscode.WebviewPanel | undefined; let inspection: Inspection | undefined; @@ -2623,6 +2624,41 @@ suite('Standalone plot restoration', () => { panel!.webview.html = panel!.webview.html.replace( '', ``, @@ -2689,23 +2726,46 @@ suite('Standalone plot restoration', () => { panel!.reveal(); await waitForValue(() => (panel!.visible ? true : undefined)); const shown = await inspect(); - assert.deepStrictEqual( - shown.container, - visible.container, - 'the viewport did not change', - ); - assert.deepStrictEqual( - shown.canvas, - shown.container, - 'visibility alone restores the full canvas', - ); - assert.deepStrictEqual( - shown.pixel, - [255, 0, 255, 255], - 'the canvas contains the newly selected plot', - ); - assert.strictEqual(history.count('device'), 2); - sinon.assert.notCalled(resize); + try { + assert.deepStrictEqual( + shown.container, + visible.container, + 'the viewport did not change', + ); + assert.deepStrictEqual( + shown.canvas, + shown.container, + 'visibility alone restores the full canvas', + ); + assert.deepStrictEqual( + shown.pixel, + [255, 0, 255, 255], + 'the canvas contains the newly selected plot', + ); + assert.strictEqual(history.count('device'), 2); + sinon.assert.notCalled(resize); + } catch (error) { + // Show whether a later replay corrects the canvas and whether R + // was asked to resize, along with the webview's own timeline. + await new Promise((resolve) => setTimeout(resolve, 1000)); + let settled: unknown; + let webview = shown.log; + try { + const { log, ...later } = await inspect(); + settled = later; + webview = log; + } catch (inspectionError) { + settled = `not available: ${String(inspectionError)}`; + } + (error as Error).message += `\nDiagnostics: ${JSON.stringify({ + initial: { container: visible.container, canvas: visible.canvas }, + shown: { ...shown, log: undefined }, + settledAfter1s: settled, + resizeCalls: resize.getCalls().map((call) => call.args), + webview, + })}`; + throw error; + } } finally { panel?.dispose(); if (coveringEditor) { From c29af3f697cebc8b28a89161e3bbd56066a3cdec Mon Sep 17 00:00:00 2001 From: eitsupi Date: Tue, 17 Nov 2026 02:42:19 +0000 Subject: [PATCH 08/11] test(plot): use lowercase jgd in the retained tab test name --- src/test/plot/plotRestoration.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/plot/plotRestoration.test.ts b/src/test/plot/plotRestoration.test.ts index 830fdd98b..a931e0a71 100644 --- a/src/test/plot/plotRestoration.test.ts +++ b/src/test/plot/plotRestoration.test.ts @@ -2593,7 +2593,7 @@ suite('Standalone plot restoration', () => { } }); - test('a retained JGD tab redraws plots made while hidden without resizing R', async () => { + test('a retained jgd tab redraws plots made while hidden without resizing R', async () => { interface Inspection { hidden: boolean; background: string; From 7fc4d866b22bac6032b8fde9fcfb0a70403df363 Mon Sep 17 00:00:00 2001 From: eitsupi <50911393+eitsupi@users.noreply.github.com> Date: Tue, 17 Nov 2026 06:33:23 +0000 Subject: [PATCH 09/11] test(plot): surface jgd diagnostics in Mocha failure output --- src/test/plot/plotRestoration.test.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/test/plot/plotRestoration.test.ts b/src/test/plot/plotRestoration.test.ts index a931e0a71..4f4f816d4 100644 --- a/src/test/plot/plotRestoration.test.ts +++ b/src/test/plot/plotRestoration.test.ts @@ -2757,14 +2757,19 @@ suite('Standalone plot restoration', () => { } catch (inspectionError) { settled = `not available: ${String(inspectionError)}`; } - (error as Error).message += `\nDiagnostics: ${JSON.stringify({ + const original = error as Error; + const diagnostics = JSON.stringify({ initial: { container: visible.container, canvas: visible.canvas }, shown: { ...shown, log: undefined }, settledAfter1s: settled, resizeCalls: resize.getCalls().map((call) => call.args), webview, - })}`; - throw error; + }); + // A new error is thrown because Mocha prints the original stack, + // which would not include an appended message. + throw new Error(`${original.message}\nDiagnostics: ${diagnostics}`, { + cause: original, + }); } } finally { panel?.dispose(); From c55280dba78e86a8b70e49369e2c4532d9219914 Mon Sep 17 00:00:00 2001 From: eitsupi <50911393+eitsupi@users.noreply.github.com> Date: Tue, 17 Nov 2026 07:05:47 +0000 Subject: [PATCH 10/11] fix: `ERR_STREAM_DESTROYED` may occur --- src/languageServerClient.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/languageServerClient.ts b/src/languageServerClient.ts index fab485b70..e34eb8c60 100644 --- a/src/languageServerClient.ts +++ b/src/languageServerClient.ts @@ -5,7 +5,7 @@ import type { ChildProcess } from 'node:child_process'; import type { DisposableProcess } from './util'; /** Transport errors that mean the peer closed or reset the connection. */ -const DISCONNECTS = new Set(['EPIPE', 'ECONNRESET', 'ECONNABORTED']); +const DISCONNECTS = new Set(['EPIPE', 'ECONNRESET', 'ECONNABORTED', 'ERR_STREAM_DESTROYED']); function disconnectCode(error: unknown): string | undefined { const code = From 8c592fd13b0afe86a5e06902c0d01ff23c0bc336 Mon Sep 17 00:00:00 2001 From: eitsupi <50911393+eitsupi@users.noreply.github.com> Date: Tue, 17 Nov 2026 07:09:33 +0000 Subject: [PATCH 11/11] ci: ensure install xquartz --- .github/actions/prepare-test-runtime/action.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/actions/prepare-test-runtime/action.yml b/.github/actions/prepare-test-runtime/action.yml index 420de66f5..7e6139f02 100644 --- a/.github/actions/prepare-test-runtime/action.yml +++ b/.github/actions/prepare-test-runtime/action.yml @@ -6,6 +6,11 @@ description: >- runs: using: composite steps: + - name: Install macOS dependencies + if: runner.os == 'macOS' + shell: bash + run: | + brew install --cask xquartz - name: Install R test dependencies uses: r-lib/actions/setup-r-dependencies@f9a764fea8d5c63df6ef9a5c7795bf7deb5d7e05 # v2 with: