Skip to content

fix: reliable self-update restart (v7.5.2) - #233

Merged
vakovalskii merged 1 commit into
mainfrom
fix/self-update-restart
Jul 19, 2026
Merged

vakovalskii merged 1 commit into
mainfrom
fix/self-update-restart

Conversation

@vakovalskii

Copy link
Copy Markdown
Owner

Problem

Clicking Update Now installed the new version via npm i -g codbash-app@latest, but the server then died and never came back up — the port was freed yet nothing relistened. The UI kept showing the stale version (v7.4.0 → v7.5.1) and the auto location.reload() hit a dead server.

Root cause

/api/update spawned the restarter inside a process.on('exit') handler:

process.on('exit', () => {
  require('child_process').spawn(process.argv[0], process.argv.slice(1), {
    detached: true, stdio: 'inherit'
  }).unref();
});
process.exit(0);

At exit time the event loop is already draining — spawning there is unreliable and the detached child frequently never survives. Net result: server gone, no relisten.

Fix

  • Spawn a fully-detached restarter (detached: true, stdio: 'ignore', own process group, .unref()) before exiting, then process.exit(0) after a 250ms tick so the port is released for the restarter.
  • Restarter reuses the same --port/--host and passes --no-browser (the existing tab reloads itself — no duplicate window).
  • Restarter loads freshly-installed code from the same argv[1] path npm just overwrote.
  • Bumped npm-install timeout 60s → 120s for slower connections.

Version

Patch bump 7.5.1 → 7.5.2 + changelog entry.

Note: users currently on the broken updater will still hit the old path once; every self-update after they land on 7.5.2 is reliable.

Testing

  • node -c syntax check passes on server.js and changelog.js.
  • Manual restart verified: server comes up on 3847 and reports 7.5.2.

🤖 Generated with Claude Code

The "Update Now" flow installed the new version but then died without
coming back up — the port was freed but nothing relistened, so the UI
kept showing the stale version and the page failed to reload.

Root cause: the restart spawned a child inside a `process.on('exit')`
handler. The event loop is already draining at exit time, so the
detached child frequently never survived.

Fix: spawn a fully-detached, unref'd restarter (own process group,
stdio ignored) BEFORE exiting, then exit after a short tick to release
the port. Restarter reuses the same port/host and skips reopening the
browser. Also raise the npm-install timeout 60s -> 120s.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@vakovalskii
vakovalskii merged commit d28d1a7 into main Jul 19, 2026
6 checks passed
@vakovalskii
vakovalskii deleted the fix/self-update-restart branch July 19, 2026 03:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant