Improve Windows terminal performance: retain WebGL contexts, warm first ConPTY (#7085)
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
*/
|
||||
import { startDaemon, type DaemonHandle } from './daemon-main'
|
||||
import { createPtySubprocess } from './pty-subprocess'
|
||||
import { warmWindowsConptyOnce } from './windows-conpty-warmup'
|
||||
import { warmPwshAvailabilityCache } from '../pwsh'
|
||||
|
||||
export function parseArgs(argv: string[]): { socketPath: string; tokenPath: string } {
|
||||
@@ -84,6 +85,8 @@ async function main(): Promise<void> {
|
||||
if (process.send) {
|
||||
process.send({ type: 'ready' })
|
||||
}
|
||||
|
||||
warmWindowsConptyOnce()
|
||||
}
|
||||
|
||||
// Only auto-run when executed directly (not imported for testing)
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import type * as pty from 'node-pty'
|
||||
import { warmWindowsConptyOnce } from './windows-conpty-warmup'
|
||||
|
||||
function setPlatform(platform: NodeJS.Platform): () => void {
|
||||
const original = process.platform
|
||||
Object.defineProperty(process, 'platform', { configurable: true, value: platform })
|
||||
return () => Object.defineProperty(process, 'platform', { configurable: true, value: original })
|
||||
}
|
||||
|
||||
function flushImmediates(): Promise<void> {
|
||||
return new Promise((resolve) => setImmediate(resolve))
|
||||
}
|
||||
|
||||
let restorePlatform: (() => void) | null = null
|
||||
afterEach(() => {
|
||||
restorePlatform?.()
|
||||
restorePlatform = null
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
function makeFakePty(): { proc: pty.IPty; fireExit: () => void } {
|
||||
let exitListener: (() => void) | null = null
|
||||
const proc = {
|
||||
pid: 4321,
|
||||
kill: vi.fn(),
|
||||
onExit: vi.fn((listener: () => void) => {
|
||||
exitListener = listener
|
||||
return { dispose: () => undefined }
|
||||
})
|
||||
} as unknown as pty.IPty
|
||||
return { proc, fireExit: () => exitListener?.() }
|
||||
}
|
||||
|
||||
describe('warmWindowsConptyOnce', () => {
|
||||
it('is a no-op off Windows', async () => {
|
||||
restorePlatform = setPlatform('darwin')
|
||||
const spawnPty = vi.fn() as unknown as typeof pty.spawn
|
||||
|
||||
warmWindowsConptyOnce(spawnPty)
|
||||
await flushImmediates()
|
||||
|
||||
expect(spawnPty).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('spawns a short-lived cmd.exe with the bundled ConPTY on Windows', async () => {
|
||||
restorePlatform = setPlatform('win32')
|
||||
const { proc, fireExit } = makeFakePty()
|
||||
const spawnPty = vi.fn(() => proc) as unknown as typeof pty.spawn
|
||||
|
||||
warmWindowsConptyOnce(spawnPty)
|
||||
await flushImmediates()
|
||||
|
||||
expect(spawnPty).toHaveBeenCalledTimes(1)
|
||||
const [file, args, options] = vi.mocked(spawnPty).mock.calls[0]
|
||||
expect(String(file).toLowerCase()).toContain('cmd')
|
||||
expect(args).toEqual(['/c', 'exit'])
|
||||
expect(options).toMatchObject({ useConptyDll: true, cols: 2, rows: 1 })
|
||||
|
||||
// A clean exit must not leave the kill timer to fire later.
|
||||
fireExit()
|
||||
expect(proc.kill).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('kills the warm-up shell if it never exits', async () => {
|
||||
restorePlatform = setPlatform('win32')
|
||||
vi.useFakeTimers()
|
||||
try {
|
||||
const { proc } = makeFakePty()
|
||||
const spawnPty = vi.fn(() => proc) as unknown as typeof pty.spawn
|
||||
|
||||
warmWindowsConptyOnce(spawnPty)
|
||||
await vi.runOnlyPendingTimersAsync()
|
||||
vi.advanceTimersByTime(10_000)
|
||||
|
||||
expect(proc.kill).toHaveBeenCalledTimes(1)
|
||||
} finally {
|
||||
vi.useRealTimers()
|
||||
}
|
||||
})
|
||||
|
||||
it('swallows spawn failures', async () => {
|
||||
restorePlatform = setPlatform('win32')
|
||||
const spawnPty = vi.fn(() => {
|
||||
throw new Error('conpty unavailable')
|
||||
}) as unknown as typeof pty.spawn
|
||||
|
||||
expect(() => warmWindowsConptyOnce(spawnPty)).not.toThrow()
|
||||
await flushImmediates()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,45 @@
|
||||
import os from 'node:os'
|
||||
import * as pty from 'node-pty'
|
||||
|
||||
const WARMUP_KILL_TIMEOUT_MS = 10_000
|
||||
|
||||
/**
|
||||
* Pays the one-time cost of the first ConPTY spawn (conpty native module
|
||||
* load, bundled conpty.dll + OpenConsole.exe first launch, Defender scans of
|
||||
* those binaries) at daemon boot instead of on the user's first terminal.
|
||||
* Measured ~2.7s on a Windows dev profile for the first spawn vs ~70ms after.
|
||||
*/
|
||||
export function warmWindowsConptyOnce(spawnPty: typeof pty.spawn = pty.spawn): void {
|
||||
if (process.platform !== 'win32') {
|
||||
return
|
||||
}
|
||||
// Why: setImmediate keeps the ready/handshake path ahead of the warm-up; a
|
||||
// real spawn arriving first simply does the warming itself.
|
||||
setImmediate(() => {
|
||||
try {
|
||||
const proc = spawnPty(process.env.COMSPEC || 'cmd.exe', ['/c', 'exit'], {
|
||||
name: 'xterm-256color',
|
||||
cols: 2,
|
||||
rows: 1,
|
||||
cwd: os.homedir(),
|
||||
env: process.env as Record<string, string>,
|
||||
// Match real terminal spawns so the bundled ConPTY binaries are the
|
||||
// ones warmed, not the legacy system ConPTY.
|
||||
useConptyDll: true
|
||||
})
|
||||
const killTimer = setTimeout(() => {
|
||||
try {
|
||||
proc.kill()
|
||||
} catch {
|
||||
/* best-effort cleanup of a stuck warm-up shell */
|
||||
}
|
||||
}, WARMUP_KILL_TIMEOUT_MS)
|
||||
killTimer.unref?.()
|
||||
proc.onExit(() => {
|
||||
clearTimeout(killTimer)
|
||||
})
|
||||
} catch {
|
||||
/* warm-up is best-effort; real spawns surface their own errors */
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user