Skip to content
Closed
Changes from 1 commit
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
58 changes: 55 additions & 3 deletions apps/desktop/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2652,6 +2652,55 @@ function venvHermesShimPath(updateRoot) {
: path.join(updateRoot, 'venv', 'bin', 'hermes')
}

// Every entry-point exe under venv\Scripts (generic — not a hardcoded list).
function venvScriptsExePaths(updateRoot) {
const scriptsDir = path.join(updateRoot, 'venv', 'Scripts')
try {
return fs
.readdirSync(scriptsDir)
.filter((f) => f.toLowerCase().endsWith('.exe'))
.map((f) => path.join(scriptsDir, f))
} catch {
return []
}
}

// Generic lock probe across EVERY entry-point exe under venv\Scripts, not a
// hardcoded shim list: any mapped exe (hindsight daemon's pythonw trampoline,
// a stray CLI, a future helper) blocks the update the same way.
function isVenvLocked(updateRoot) {
if (!IS_WINDOWS) {
return false
}
for (const exe of venvScriptsExePaths(updateRoot)) {
if (isShimLocked(exe)) {
return true
}
}
return false
}

// Kill every process whose exe lives under venv\Scripts (hindsight daemon,
// leftover CLIs, anything else) so the updater never races a mapped shim.
function killVenvShimHolders(updateRoot) {
if (!IS_WINDOWS) {
return
}
const scriptsDir = path.join(updateRoot, 'venv', 'Scripts').replace(/'/g, "''")
try {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please do not scope this with -like: it treats legal [/] path characters as wildcard syntax, and this literal emits two backslashes before *. Use an ordinal, case-insensitive StartsWith check against the normalized Scripts prefix, as required by tests/test_install_unmerged_index.py:171-180.

const ps =

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This force-kills every venv-resident process and its tree, including user-managed terminals, gateways, or services. Current handoff only kills Desktop-owned backend/pool PIDs and reports external holders; please retain that ownership boundary or track a specific Desktop-owned daemon.

`$p = Get-CimInstance Win32_Process | Where-Object { $_.ExecutablePath -like '${scriptsDir}\\*' }; ` +
`foreach ($x in $p) { taskkill /PID $($x.ProcessId) /T /F 2>$null | Out-Null }`
execFileSync(
'powershell',
['-NoProfile', '-Command', ps],
hiddenWindowsChildOptions({ stdio: 'ignore' })
)
} catch {
// best-effort: the lock probe below is the real gate
}
}

// Best-effort lock probe mirroring the Rust updater's is_locked(): a running
// .exe on Windows refuses an O_RDWR open with a sharing violation. On POSIX
// this practically always succeeds (no mandatory locking), so it returns false
Expand Down Expand Up @@ -2771,12 +2820,15 @@ async function releaseBackendLock(updateRoot, tag) {
forceKillProcessTree(pid)
}

const shim = venvHermesShimPath(updateRoot)
// Kill every venv\Scripts holder (hindsight daemon, stray CLIs, anything
// else) so the updater never races a mapped pythonw/python/hermes shim.
killVenvShimHolders(updateRoot)

const deadlineMs = Date.now() + 15000

while (Date.now() < deadlineMs) {
if (!isShimLocked(shim)) {
rememberLog(`[${tag}] venv shim unlocked; safe to proceed`)
if (!isVenvLocked(updateRoot)) {
rememberLog(`[${tag}] venv shims unlocked; safe to proceed`)

return { unlocked: true }
}
Expand Down