Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(installer): free our engine, not every llama-server on the machine

Davidecc48e7a57ca
diff --git a/src-tauri/windows/installer-hooks.nsh b/src-tauri/windows/installer-hooks.nsh
index 9b04d269..ffdbfe70 100644
--- a/src-tauri/windows/installer-hooks.nsh
+++ b/src-tauri/windows/installer-hooks.nsh
@@ -87,10 +87,32 @@
;
; The test is the write itself, not a process list: if the file opens for
; writing, nothing holds it and we touch nothing.
+;
+; And when something does hold it, only OUR copy may die. `taskkill /F /T /IM`
+; matches by image name for the whole session, so the first version of this
+; hook also hard-killed a llama.cpp server the user had started themselves,
+; mid-generation, during an update they never watched. llama.cpp is one of the
+; backends this app detects, so having one running is normal, and everywhere
+; else the app leaves a stranger alone: an engine start that meets one on its
+; port refuses and asks the user to quit it (commands/engine.rs), and every
+; kill in Rust goes by PID. The installer was the single place that swung at a
+; name. It now ends exactly the processes whose image IS the file about to be
+; overwritten. The path comes from WMI rather than Get-Process because the
+; installer is 32 bit and .NET cannot read MainModule of a 64 bit process from
+; there, which is what our own engine is.
+;
+; Last resort, when the lock outlives the four rounds because someone else
+; holds it (a scanner, a backup agent, a process this user may not touch):
+; Windows refuses to WRITE a locked image but still allows it to be RENAMED, so
+; the old engine moves aside and the update lands. That is the whole point of
+; the hook, and it beats the stock dialog where Retry loops on the same lock
+; and Ignore leaves the new app running last release's engine. The leftover is
+; swept at the start of the next install.
!macro LU_FREE_SIDECAR
Push $R3
Push $R4
Push $R9
+ Delete "$INSTDIR\llama-server.exe.old"
StrCpy $R4 0
${Do}
${IfNot} ${FileExists} "$INSTDIR\llama-server.exe"
@@ -103,12 +125,13 @@
${ExitDo}
${EndIf}
${If} $R4 >= 4
- ; Still locked after four rounds. Fall through and let the stock error
- ; dialog appear rather than spin here forever.
+ ; Still locked after four rounds, so it is not our engine holding it.
+ ; Move it out of the way instead of spinning here or failing the copy.
+ Rename "$INSTDIR\llama-server.exe" "$INSTDIR\llama-server.exe.old"
${ExitDo}
${EndIf}
IntOp $R4 $R4 + 1
- nsExec::Exec '"$SYSDIR\taskkill.exe" /F /T /IM "llama-server.exe"'
+ nsExec::Exec `"$SYSDIR\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -NonInteractive -Command "Get-CimInstance Win32_Process | Where-Object { $$_.ExecutablePath -eq '$INSTDIR\llama-server.exe' } | ForEach-Object { Stop-Process -Id $$_.ProcessId -Force }"`
Pop $R9
Sleep 800
${Loop}
diff --git a/src/lib/__tests__/installer-frees-sidecar.test.ts b/src/lib/__tests__/installer-frees-sidecar.test.ts
index bf031504..d0e709b0 100644
--- a/src/lib/__tests__/installer-frees-sidecar.test.ts
+++ b/src/lib/__tests__/installer-frees-sidecar.test.ts
@@ -1,5 +1,6 @@
/**
- * The Windows installer has to free our own engine before it copies over it.
+ * The Windows installer has to free our own engine before it copies over it,
+ * and it may free NOTHING ELSE.
*
* aldrich_ironhart, 2026-08-10: the update died on "Error opening file for
* writing: D:\Locally Uncensored\llama-server.exe". Windows locks a running
@@ -11,6 +12,15 @@
* in the installer, and a rename in one of them makes the hook silently do
* nothing again. So all three are compared here, not just the hook.
*
+ * Review 2026-08-14: the first version of the unlock reached far too wide.
+ * `taskkill /F /T /IM "llama-server.exe"` matches by image name for the whole
+ * session, so a llama.cpp server the user started themselves (one of the
+ * backends this app detects, so a normal thing to have running) was hard
+ * killed mid-generation by an update they never watched. Everywhere else the
+ * app leaves a stranger alone, so the kill is now scoped to the one file the
+ * installer is about to overwrite, and a lock it cannot clear moves the file
+ * aside instead of handing the user Abort, Retry, Ignore.
+ *
* Run: npx vitest run src/lib/__tests__/installer-frees-sidecar.test.ts
*/
import { describe, it, expect } from 'vitest'
@@ -28,6 +38,13 @@ const conf = JSON.parse(read('src-tauri/tauri.conf.json')) as {
bundle?: { externalBin?: string[] }
}
+// Only the unlock macro. The dormant product-rename macro below it kills the
+// app itself and is a different question.
+const unlock = hooks.slice(
+ hooks.indexOf('!macro LU_FREE_SIDECAR'),
+ hooks.indexOf('!macro NSIS_HOOK_PREINSTALL'),
+)
+
describe('the installer frees the bundled engine', () => {
it('runs the unlock on every build, not only on a product rename', () => {
const preinstall = hooks.slice(hooks.indexOf('!macro NSIS_HOOK_PREINSTALL'))
@@ -39,17 +56,63 @@ describe('the installer frees the bundled engine', () => {
})
it('asks the only question that matters, can the file be written', () => {
- expect(hooks).toMatch(/FileOpen \$R3 "\$INSTDIR\\llama-server\.exe" a/)
- expect(hooks).toMatch(/taskkill\.exe" \/F \/T \/IM "llama-server\.exe"/)
+ expect(unlock).toMatch(/FileOpen \$R3 "\$INSTDIR\\llama-server\.exe" a/)
// Bounded: a locked file must not spin the installer forever.
- expect(hooks).toMatch(/\$R4 >= 4/)
+ expect(unlock).toMatch(/\$R4 >= 4/)
})
- it('kills the name the bundle actually ships and Rust actually starts', () => {
+ it('frees the file the bundle actually ships and Rust actually starts', () => {
const external = conf.bundle?.externalBin ?? []
expect(external).toContain('bin/llama-server')
expect(engineRs).toMatch(/"llama-server\.exe"/)
- const killed = hooks.match(/\/IM "([^"]+)"/g) ?? []
- expect(killed).toContain('/IM "llama-server.exe"')
+ })
+})
+
+describe('it kills our engine and nobody elses', () => {
+ it('never swings at an image name', () => {
+ expect(unlock).not.toMatch(/\/IM/)
+ })
+
+ it('kills only the process whose image is the file being overwritten', () => {
+ expect(unlock).toContain("-eq '$INSTDIR\\llama-server.exe'")
+ expect(unlock).toMatch(/Stop-Process -Id \$\$_\.ProcessId -Force/)
+ })
+
+ it('reads the path over WMI, a 32 bit installer cannot read a 64 bit MainModule', () => {
+ expect(unlock).toContain('Get-CimInstance Win32_Process')
+ expect(unlock).not.toContain('Get-Process')
+ })
+
+ it('escapes the PowerShell dollar so NSIS does not eat the pipeline variable', () => {
+ // A bare $_ is an unknown NSIS variable and expands to nothing, which would
+ // compare the empty string against the path and match no process at all.
+ expect(unlock).not.toMatch(/[^$]\$_\./)
+ })
+
+ it('touches nothing while the file still opens for writing', () => {
+ // The kill sits behind the write probe, not in front of it.
+ expect(unlock.indexOf('FileOpen')).toBeLessThan(unlock.indexOf('nsExec::Exec'))
+ })
+
+ it('follows the rule the rest of the app already keeps', () => {
+ expect(engineRs).toMatch(
+ /already serving another llama-server that this app does not manage/,
+ )
+ })
+})
+
+describe('a lock we cannot clear must not end the update', () => {
+ it('moves the old engine aside instead of failing the copy', () => {
+ expect(unlock).toMatch(
+ /Rename "\$INSTDIR\\llama-server\.exe" "\$INSTDIR\\llama-server\.exe\.old"/,
+ )
+ // The rename is the last resort, it belongs after the retry budget.
+ expect(unlock.indexOf('$R4 >= 4')).toBeLessThan(unlock.indexOf('Rename'))
+ })
+
+ it('sweeps the leftover before the next install writes a new one', () => {
+ const sweep = unlock.indexOf('Delete "$INSTDIR\\llama-server.exe.old"')
+ expect(sweep).toBeGreaterThan(-1)
+ expect(sweep).toBeLessThan(unlock.indexOf('${Do}'))
})
})