From bd77b557a8b028e68e18ee4ef8b326476a9a2c20 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:00:07 -0800 Subject: [PATCH] fix(terminal): show the real Windows error when conpty.node fails to load (#43337) * fix(terminal): show the real Windows error when node-pty cannot load conpty.node * fix(terminal): keep the first load error across the retry and cap the retry wait per module --- config/patches/node-pty@1.1.0.patch | 175 ++++++++++++++++- pnpm-lock.yaml | 6 +- .../node-pty-native-load-error.test.ts | 184 ++++++++++++++++++ 3 files changed, 358 insertions(+), 7 deletions(-) create mode 100644 src/main/orcad/native-runtime/node-pty-native-load-error.test.ts diff --git a/config/patches/node-pty@1.1.0.patch b/config/patches/node-pty@1.1.0.patch index d6ee94ff78d..4523b2d6820 100644 --- a/config/patches/node-pty@1.1.0.patch +++ b/config/patches/node-pty@1.1.0.patch @@ -269,10 +269,10 @@ index 1ec12f796a822c78fba9ad7f6448c3987e325c23..1779098c54ff4be8c0d4dc9a93e96e71 else { // Stop processing immediately on unexpected error and log diff --git a/lib/utils.js b/lib/utils.js -index af7918b6902a105a7c49813bc0bb4d687d87a7d4..1fca29d71f11ece98b8224b81ab78a6bc5b41a6d 100644 +index af7918b6902a105a7c49813bc0bb4d687d87a7d4..3870bba49c32a3695d3ddbbf1bca9ecb56c7d700 100644 --- a/lib/utils.js +++ b/lib/utils.js -@@ -15,8 +15,8 @@ function assign(target) { +@@ -15,25 +15,82 @@ function assign(target) { } exports.assign = assign; function loadNativeModule(name) { @@ -283,6 +283,87 @@ index af7918b6902a105a7c49813bc0bb4d687d87a7d4..1fca29d71f11ece98b8224b81ab78a6b // Check relative to the parent dir for unbundled and then the current dir for bundled var relative = ['..', '.']; var lastError; ++ // Orca: keep real load failures; the final miss (usually prebuilds) would otherwise hide them. ++ var failures = []; + for (var _i = 0, dirs_1 = dirs; _i < dirs_1.length; _i++) { + var d = dirs_1[_i]; + for (var _a = 0, relative_1 = relative; _a < relative_1.length; _a++) { + var r = relative_1[_a]; + var dir = r + "/" + d + "/"; +- try { +- return { dir: dir, module: require(dir + "/" + name + ".node") }; +- } +- catch (e) { +- lastError = e; ++ var path = dir + name + ".node"; ++ for (var attempt = 0;; attempt++) { ++ try { ++ return { dir: dir, module: require(path) }; ++ } ++ catch (e) { ++ lastError = e; ++ if (!isModuleNotFoundError(e)) { ++ failures.push({ path: path, error: e, retry: attempt > 0 }); ++ } ++ if (attempt === 0 && !retryWaitSpent.has(name) && isTransientNativeModuleLoadError(e)) { ++ // Orca: a freshly copied addon can be briefly held by an antivirus scan. ++ retryWaitSpent.add(name); ++ sleepSync(NATIVE_MODULE_RETRY_DELAY_MS); ++ continue; ++ } ++ break; ++ } + } + } + } +- throw new Error("Failed to load native module: " + name + ".node, checked: " + dirs.join(', ') + ": " + lastError); ++ throw new Error(formatNativeModuleLoadError(name, dirs, failures, lastError)); + } + exports.loadNativeModule = loadNativeModule; ++var NATIVE_MODULE_RETRY_DELAY_MS = 200; ++// Orca: one retry wait per module per process, so a permanent denial cannot stall every spawn. ++var retryWaitSpent = new Set(); ++function errorCode(error) { ++ var code = error && typeof error === 'object' ? error.code : undefined; ++ return typeof code === 'string' ? code : undefined; ++} ++function errorMessage(error) { ++ return error instanceof Error ? error.message : String(error); ++} ++function isModuleNotFoundError(error) { ++ return errorCode(error) === 'MODULE_NOT_FOUND'; ++} ++function isTransientNativeModuleLoadError(error) { ++ if (process.platform !== 'win32') { ++ return false; ++ } ++ var code = errorCode(error); ++ if (code === 'EBUSY' || code === 'EPERM') { ++ return true; ++ } ++ // Windows reports sharing, lock, and scanner-held denials only in the dlopen message. ++ return /being used by another process|locked a portion of the file|access is denied/i.test(errorMessage(error)); ++} ++function formatNativeModuleLoadError(name, dirs, failures, lastError) { ++ var prefix = "Failed to load native module: " + name + ".node, checked: " + dirs.join(', ') + ": "; ++ if (failures.length === 0) { ++ return prefix + lastError; ++ } ++ return prefix + failures.map(function (failure) { ++ var code = errorCode(failure.error); ++ return failure.path + (failure.retry ? ' on retry' : '') + (code ? " (" + code + ")" : '') + ": " + errorMessage(failure.error).trim(); ++ }).join('; '); ++} ++function sleepSync(ms) { ++ try { ++ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms); ++ } ++ catch (_a) { ++ // Atomics.wait is unavailable on this thread; retry without waiting. ++ } ++} + //# sourceMappingURL=utils.js.map +\ No newline at end of file diff --git a/src/conpty_console_list_agent.ts b/src/conpty_console_list_agent.ts index 181ccabbbe9c4948a9725fb1db907a68e9de01fc..67f31facf85562b67adbfbd04ce28ddd8eeb4a79 100644 --- a/src/conpty_console_list_agent.ts @@ -1165,10 +1246,10 @@ index 98733dc0cd752b554bd94e45904ca341ad141bba..3dd5ad9b3124dbd5ba7f76679b26650d // Stop processing immediately on unexpected error and log this._writeQueue.length = 0; diff --git a/src/utils.ts b/src/utils.ts -index 81a70c77a18e25c50b26f0d80aa0e8d8c2672805..4bc8d760fe00e208f211570930cd163b77901810 100644 +index 81a70c77a18e25c50b26f0d80aa0e8d8c2672805..f23925ec12a9c36ed78bc82b263f3d588fdc2a3e 100644 --- a/src/utils.ts +++ b/src/utils.ts -@@ -10,8 +10,8 @@ export function assign(target: any, ...sources: any[]): any { +@@ -10,20 +10,89 @@ export function assign(target: any, ...sources: any[]): any { export function loadNativeModule(name: string): {dir: string, module: any} { @@ -1179,6 +1260,92 @@ index 81a70c77a18e25c50b26f0d80aa0e8d8c2672805..4bc8d760fe00e208f211570930cd163b // Check relative to the parent dir for unbundled and then the current dir for bundled const relative = ['..', '.']; let lastError: unknown; ++ // Orca: keep real load failures; the final miss (usually prebuilds) would otherwise hide them. ++ const failures: NativeModuleLoadFailure[] = []; + for (const d of dirs) { + for (const r of relative) { + const dir = `${r}/${d}/`; +- try { +- return { dir, module: require(`${dir}/${name}.node`) }; +- } catch (e) { +- lastError = e; ++ const path = `${dir}${name}.node`; ++ for (let attempt = 0; ; attempt++) { ++ try { ++ return { dir, module: require(path) }; ++ } catch (e) { ++ lastError = e; ++ if (!isModuleNotFoundError(e)) { ++ failures.push({ path, error: e, retry: attempt > 0 }); ++ } ++ if (attempt === 0 && !retryWaitSpent.has(name) && isTransientNativeModuleLoadError(e)) { ++ // Orca: a freshly copied addon can be briefly held by an antivirus scan. ++ retryWaitSpent.add(name); ++ sleepSync(NATIVE_MODULE_RETRY_DELAY_MS); ++ continue; ++ } ++ break; ++ } + } + } + } +- throw new Error(`Failed to load native module: ${name}.node, checked: ${dirs.join(', ')}: ${lastError}`); ++ throw new Error(formatNativeModuleLoadError(name, dirs, failures, lastError)); ++} ++ ++interface NativeModuleLoadFailure { ++ path: string; ++ error: unknown; ++ retry: boolean; ++} ++ ++const NATIVE_MODULE_RETRY_DELAY_MS = 200; ++// Orca: one retry wait per module per process, so a permanent denial cannot stall every spawn. ++const retryWaitSpent = new Set(); ++ ++function errorCode(error: unknown): string | undefined { ++ const code = error && typeof error === 'object' ? (error as { code?: unknown }).code : undefined; ++ return typeof code === 'string' ? code : undefined; ++} ++ ++function errorMessage(error: unknown): string { ++ return error instanceof Error ? error.message : String(error); ++} ++ ++function isModuleNotFoundError(error: unknown): boolean { ++ return errorCode(error) === 'MODULE_NOT_FOUND'; ++} ++ ++function isTransientNativeModuleLoadError(error: unknown): boolean { ++ if (process.platform !== 'win32') { ++ return false; ++ } ++ const code = errorCode(error); ++ if (code === 'EBUSY' || code === 'EPERM') { ++ return true; ++ } ++ // Windows reports sharing, lock, and scanner-held denials only in the dlopen message. ++ return /being used by another process|locked a portion of the file|access is denied/i.test(errorMessage(error)); ++} ++ ++function formatNativeModuleLoadError(name: string, dirs: string[], failures: NativeModuleLoadFailure[], lastError: unknown): string { ++ const prefix = `Failed to load native module: ${name}.node, checked: ${dirs.join(', ')}: `; ++ if (failures.length === 0) { ++ return prefix + lastError; ++ } ++ return prefix + failures.map(failure => { ++ const code = errorCode(failure.error); ++ return `${failure.path}${failure.retry ? ' on retry' : ''}${code ? ` (${code})` : ''}: ${errorMessage(failure.error).trim()}`; ++ }).join('; '); ++} ++ ++function sleepSync(ms: number): void { ++ try { ++ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms); ++ } catch { ++ // Atomics.wait is unavailable on this thread; retry without waiting. ++ } + } diff --git a/src/win/conpty.cc b/src/win/conpty.cc index 7b286d3d64..5239ba4e40 100644 --- a/src/win/conpty.cc diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2dc8ae03e6d..7796651dc59 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -214,7 +214,7 @@ patchedDependencies: minimatch@10.2.6: 4324868e550a69eee9addc4801001bee415e98339c9b2b88d6ab4fc6028741c6 minimatch@9.0.9: c356feccb9147b4d6a1cc72b10270defcb95d14cb42ee74ddfe7219341417161 monaco-editor@0.55.1: 53b71f7d8a7c00b76ffdb73ce83d18e31692f193f5fafa6a390e48f5b3fdd636 - node-pty@1.1.0: 37dc98d9a4bcfc67153fa2fbf30689563ff49dcf8e6f0578484cbacdf1e38af5 + node-pty@1.1.0: 323de54927594a11e3e110e31d5350327ef0a9d527c25342ab9b770b89948c9b oxlint-plugin-anti-slop@0.1.2: c10c871702365a0c2c59e9f1c5fb92df97cae5755d534c0a3d83c6368f6feafe pdfjs-dist@6.3.289: 424fde648e16c965ce45c55bdda49ecb3a4aa992c856900714c841c3f13f8415 prosemirror-view@1.42.3: 3340ccbcc4e3302f3ae1c0eda3911015966add2e7e0df76abd89fbef4f3902f7 @@ -267,7 +267,7 @@ importers: version: 3.0.2 node-pty: specifier: ^1.1.0 - version: 1.1.0(patch_hash=37dc98d9a4bcfc67153fa2fbf30689563ff49dcf8e6f0578484cbacdf1e38af5) + version: 1.1.0(patch_hash=323de54927594a11e3e110e31d5350327ef0a9d527c25342ab9b770b89948c9b) posthog-node: specifier: ^5.52.5 version: 5.52.5 @@ -14163,7 +14163,7 @@ snapshots: node-int64@0.4.0: {} - node-pty@1.1.0(patch_hash=37dc98d9a4bcfc67153fa2fbf30689563ff49dcf8e6f0578484cbacdf1e38af5): + node-pty@1.1.0(patch_hash=323de54927594a11e3e110e31d5350327ef0a9d527c25342ab9b770b89948c9b): dependencies: node-addon-api: 7.1.1 diff --git a/src/main/orcad/native-runtime/node-pty-native-load-error.test.ts b/src/main/orcad/native-runtime/node-pty-native-load-error.test.ts new file mode 100644 index 00000000000..e13eed00234 --- /dev/null +++ b/src/main/orcad/native-runtime/node-pty-native-load-error.test.ts @@ -0,0 +1,184 @@ +import { copyFileSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs' +import { createRequire } from 'node:module' +import { tmpdir } from 'node:os' +import { dirname, join } from 'node:path' +import { runInNewContext } from 'node:vm' +import { afterEach, describe, expect, it } from 'vitest' +import { classifyNodePtyLoaderMessage } from './node-pty-loader-diagnosis' + +// Exercises the loader added by config/patches/node-pty@1.1.0.patch, not a copy of it. +const require = createRequire(import.meta.url) +const PATCHED_UTILS = join(dirname(require.resolve('node-pty/package.json')), 'lib', 'utils.js') +const WIN_DIRS = 'build/orca-napi-win32-x64, build/Release, build/Debug, prebuilds/win32-x64' +const SHARING_VIOLATION = + 'The process cannot access the file because it is being used by another process.' + +type LoadNativeModule = (name: string) => unknown + +const tempDirs: string[] = [] + +afterEach(() => { + for (const dir of tempDirs.splice(0)) { + rmSync(dir, { recursive: true, force: true }) + } +}) + +function loadPatchedUtilsCopy(nativeFiles: Record = {}): LoadNativeModule { + const root = mkdtempSync(join(tmpdir(), 'orca-node-pty-load-')) + tempDirs.push(root) + mkdirSync(join(root, 'lib')) + copyFileSync(PATCHED_UTILS, join(root, 'lib', 'utils.js')) + for (const [relativePath, contents] of Object.entries(nativeFiles)) { + mkdirSync(dirname(join(root, relativePath)), { recursive: true }) + writeFileSync(join(root, relativePath), contents) + } + const utils: { loadNativeModule: LoadNativeModule } = require(join(root, 'lib', 'utils.js')) + return utils.loadNativeModule +} + +/** Runs the installed loader with a scripted require and a faked platform, recording retry waits. */ +function loadPatchedUtilsWithRequire( + platform: string, + fakeRequire: (path: string) => unknown +): { loadNativeModule: LoadNativeModule; requested: string[]; waits: number[] } { + const requested: string[] = [] + const waits: number[] = [] + const moduleExports: { loadNativeModule?: LoadNativeModule } = {} + const wrapper = runInNewContext( + `(function (exports, require) {${readFileSync(PATCHED_UTILS, 'utf8')}\n})`, + { + process: { platform, arch: 'x64' }, + Atomics: { + wait: (_array: unknown, _index: number, _value: number, ms: number) => waits.push(ms) + }, + Error, + Object + } + ) + wrapper(moduleExports, (path: string) => { + requested.push(path) + return fakeRequire(path) + }) + const { loadNativeModule } = moduleExports + if (typeof loadNativeModule !== 'function') { + throw new Error('patched utils.js did not export loadNativeModule') + } + return { loadNativeModule, requested, waits } +} + +function loadError(loadNativeModule: LoadNativeModule, name: string): string { + try { + loadNativeModule(name) + } catch (error) { + return error instanceof Error ? error.message : String(error) + } + throw new Error(`expected ${name}.node to fail to load`) +} + +function dlopenError(message: string): Error { + return Object.assign(new Error(message), { code: 'ERR_DLOPEN_FAILED' }) +} + +function notFound(path: string): Error { + return Object.assign(new Error(`Cannot find module '${path}'`), { code: 'MODULE_NOT_FOUND' }) +} + +describe('patched node-pty native module loader', () => { + it('keeps the real load failure instead of the final missing prebuilds directory', () => { + const loadNativeModule = loadPatchedUtilsCopy({ + 'build/Release/broken.node': 'not a native module' + }) + + const message = loadError(loadNativeModule, 'broken') + + expect(message).toMatch(/^Failed to load native module: broken\.node, checked: /) + expect(message).toContain('../build/Release/broken.node (ERR_DLOPEN_FAILED): ') + expect(message).not.toContain('Cannot find module') + }) + + it('retries a Windows sharing violation once and returns the module when it then loads', () => { + const conpty = { name: 'conpty' } + let releaseAttempts = 0 + const { loadNativeModule, requested, waits } = loadPatchedUtilsWithRequire('win32', (path) => { + if (path !== '../build/Release/conpty.node') { + throw notFound(path) + } + releaseAttempts += 1 + if (releaseAttempts === 1) { + throw dlopenError(SHARING_VIOLATION) + } + return conpty + }) + + expect(loadNativeModule('conpty')).toEqual({ dir: '../build/Release/', module: conpty }) + expect(waits).toEqual([200]) + expect(requested.filter((path) => path === '../build/Release/conpty.node')).toHaveLength(2) + }) + + it('reports both the first failure and the retry failure, then stops', () => { + const { loadNativeModule, requested, waits } = loadPatchedUtilsWithRequire('win32', (path) => { + throw path === '../build/Release/conpty.node' + ? dlopenError(SHARING_VIOLATION) + : notFound(path) + }) + + const message = loadError(loadNativeModule, 'conpty') + + expect(message).toBe( + `Failed to load native module: conpty.node, checked: ${WIN_DIRS}: ` + + `../build/Release/conpty.node (ERR_DLOPEN_FAILED): ${SHARING_VIOLATION}; ` + + `../build/Release/conpty.node on retry (ERR_DLOPEN_FAILED): ${SHARING_VIOLATION}` + ) + expect(classifyNodePtyLoaderMessage(message)).toEqual({ + reason: 'load_failed', + detail: message + }) + expect(waits).toEqual([200]) + // Eight candidates plus the single retry. + expect(requested).toHaveLength(9) + }) + + it('keeps the first denial when the file is gone by the time of the retry', () => { + let releaseAttempts = 0 + const { loadNativeModule } = loadPatchedUtilsWithRequire('win32', (path) => { + if (path === '../build/Release/conpty.node' && ++releaseAttempts === 1) { + throw dlopenError('Access is denied.') + } + throw notFound(path) + }) + + const message = loadError(loadNativeModule, 'conpty') + + expect(message).toBe( + `Failed to load native module: conpty.node, checked: ${WIN_DIRS}: ` + + '../build/Release/conpty.node (ERR_DLOPEN_FAILED): Access is denied.' + ) + expect(classifyNodePtyLoaderMessage(message).reason).toBe('load_failed') + }) + + it('spends the retry wait once per module per process', () => { + const { loadNativeModule, waits } = loadPatchedUtilsWithRequire('win32', (path) => { + throw path === '../build/Release/conpty.node' + ? dlopenError('Access is denied.') + : notFound(path) + }) + + loadError(loadNativeModule, 'conpty') + const second = loadError(loadNativeModule, 'conpty') + + expect(waits).toEqual([200]) + expect(second).not.toContain('on retry') + }) + + it('reports a denial outside Windows without retrying it', () => { + const { loadNativeModule, requested, waits } = loadPatchedUtilsWithRequire('linux', (path) => { + throw path === '../build/Release/pty.node' ? dlopenError('Access is denied.') : notFound(path) + }) + + expect(loadError(loadNativeModule, 'pty')).toContain( + '../build/Release/pty.node (ERR_DLOPEN_FAILED): Access is denied.' + ) + expect(waits).toEqual([]) + expect(requested).toHaveLength(8) + }) +})