Skip to content

install.sh leaves a stale daemon/socket after upgrade — consider stale-socket auto-reap on launch #291

Closed

Description

@capt-marbles

Summary

scripts/install.sh swaps the on-disk binary but never retires a running server or reaps the runtime socket/lock. As a result, an "upgrade" can leave the old code in charge (a surviving daemon), and a leftover dead socket can wedge the client into a connect-retry loop. This is the same class of problem that let #277 persist past the on-disk fix.

This is not a regression in the #277 reload-loop fix — that fix is sound (v0.17.0+, 5fa0360d), and the client-side circuit breaker shipped in v0.17.1 makes the catastrophic flicker much less likely. This is about upgrade hygiene and one remaining sharp edge.

What I hit

On Omarchy (Arch), XDG_RUNTIME_DIR=/run/user/1000:

  • jcode --version → v0.17.0 (unknown)
  • jcode debug list →
    Running jcode servers:
      /run/user/1000/jcode.sock (✗ not responding (stale socket?))
    
  • Symptom: flicker on launch — the client repeatedly tries to hand off to the dead socket and never completes (same surface symptom as jcode badly broken on Omarchy #277, different cause).

jcode correctly detects the socket as stale ("✗ not responding (stale socket?)"), but a normal launch doesn't appear to reap it — so the bad state persists across restarts until the socket is removed by hand.

What fixed it

A manual clean — kill any process, remove the stale socket + lock, clear old build dirs, reinstall:

pkill -9 -f jcode
rm -f /run/user/1000/jcode.sock /run/user/1000/jcode-debug.sock /run/user/1000/jcode-daemon.lock
rm -rf ~/.jcode/builds/{current,stable,versions}
curl -fsSL .../install.sh | bash      # → v0.17.1
JCODE_NO_UPDATE=1 jcode               # clean, no flicker

The decisive step in my case was removing the stale socket — none of this is something install.sh does today.

Two gaps

  1. install.sh doesn't retire a running daemon on upgrade. It only copies binaries into ~/.jcode/builds/{stable,current,versions}, so a previously-running server keeps executing old code until the user manually kills it.
  2. A stale socket isn't auto-reaped on launch. The runtime already knows the socket is dead (it prints "stale socket?"), but doesn't clean it up and rebind, so the bad state survives restarts.

Possible fixes (your call on direction)

Option A — binary-side (probably the more robust fix): on startup, when the socket at storage::runtime_dir()/jcode.sock exists but has no live listener (you already detect this in server/socket.rs), remove it + the sibling debug socket + jcode-daemon.lock and rebind. Optionally add a jcode server stop subcommand that install.sh can call for a graceful, cross-platform daemon shutdown — cleaner than the installer killing processes.

Option B — installer-side (quick, self-contained): have install.sh retire a surviving daemon and clear the stale socket/lock on upgrade. Candidate patch below. Caveat: it matches the server by its builds/ path via pgrep -f (intentionally narrow, so it doesn't kill an unrelated foreground jcode), which is a bit blunt — Option A is cleaner if you'd rather the binary own this.

Candidate install.sh patch (Option B)
@@ after the info()/err() helpers @@
+# --- runtime dir resolution: mirror storage::runtime_dir() in the Rust source ---
+jcode_runtime_dir() {
+  if [ -n "${JCODE_RUNTIME_DIR:-}" ]; then printf '%s\n' "$JCODE_RUNTIME_DIR"; return; fi
+  if [ -n "${XDG_RUNTIME_DIR:-}" ]; then printf '%s\n' "$XDG_RUNTIME_DIR"; return; fi
+  if [ "$(uname -s)" = "Darwin" ] && [ -n "${TMPDIR:-}" ]; then printf '%s\n' "$TMPDIR"; return; fi
+  # Fallback mirrors storage::fallback_runtime_dir(): temp_dir()/jcode-<euid>
+  printf '%s\n' "${TMPDIR:-/tmp}/jcode-$(id -u)"
+}
+
+# Retire any jcode server left running from a previous install so the freshly
+# installed binary serves the next launch, and clear the runtime socket/lock.
+stop_running_daemon() {
+  [ "$IS_WINDOWS" = true ] && return 0
+  local rt sock dbg lock pids
+  rt="$(jcode_runtime_dir)"
+  sock="$rt/jcode.sock"; dbg="$rt/jcode-debug.sock"; lock="$rt/jcode-daemon.lock"
+  # Match the server by its builds path, not every running 'jcode'.
+  pids="$(pgrep -f "$builds_dir/" 2>/dev/null || true)"
+  if [ -n "$pids" ]; then
+    info "Retiring jcode server(s) from the previous install (pids: $pids)"
+    # shellcheck disable=SC2086
+    kill -TERM $pids 2>/dev/null || true
+    for _ in 1 2 3 4 5 6 7 8 9 10; do
+      [ -z "$(pgrep -f "$builds_dir/" 2>/dev/null || true)" ] && break
+      sleep 0.3
+    done
+    pids="$(pgrep -f "$builds_dir/" 2>/dev/null || true)"
+    # shellcheck disable=SC2086
+    [ -n "$pids" ] && kill -KILL $pids 2>/dev/null || true
+  fi
+  rm -f "$sock" "$dbg" "$lock" 2>/dev/null || true
+}
@@ after the binary is installed + symlinks updated, before the Darwin xattr block @@
+# Retire a pre-upgrade daemon now that the new binary is in place.
+if [ -n "$EXISTING" ]; then
+  stop_running_daemon
+fi

jcode_runtime_dir() mirrors storage::runtime_dir() exactly, including the temp_dir()/jcode-<euid> fallback and the macOS $TMPDIR branch.

Note on (unknown) version (not a bug — for anyone else who trips on it)

The Linux x86_64 release reports v0.X.Y (unknown) because it's built in the manylinux2014 container where git rev-parse isn't available, so the cosmetic hash falls back to "unknown" (jcode-build-meta/build.rs). macOS builds natively and shows a real hash. The absence of a -dev suffix confirms it's a genuine JCODE_RELEASE_BUILD=1 release; the label has no bearing on the compiled code. Might be worth passing JCODE_BUILD_GIT_HASH=${{ github.sha }} in release.yml so Linux artifacts show the hash too — minor, but it'd avoid the "is my binary legit?" confusion.

Activity

  1. added
    bug
    on May 31, 2026
  2. added
    priority: high
    triage: fixed
    on May 31, 2026
  3. 1jehuang commented on May 31, 2026

    @1jehuang
    Owner

    Both gaps you identified have since been addressed in current code, and the issue text itself notes the underlying #277 fix (5fa0360) is sound.

    1. install.sh now retires a running daemon on upgrade. scripts/install.sh has an explicit block (commented issue #291) that, after installing the new binary, runs "$reload_bin" server reload to hand a running server onto the freshly installed version. It uses the graceful reload path (live headless/swarm sessions are handed to a newly-exec'd server rather than dropped) and only reloads when the running server is genuinely older. It is best-effort and skipped when no server is running / when JCODE_SKIP_SERVER_RELOAD=1.

    2. Stale sockets are auto-reaped on launch. reap_stale_socket_if_dead() lives in crates/jcode-app-core/src/server/socket.rs and is called from the launch/handoff paths (src/cli/dispatch.rs, src/cli/commands.rs). It only unlinks the socket pair + lock when the socket is provably dead (no live listener), then rebinds. There's also a jcode server stop subcommand (src/cli/args.rs) for graceful, cross-platform daemon shutdown - the Option A direction you suggested.

    This closely matches the "Option A (binary-side)" + installer retirement you proposed. The (unknown) version cosmetic note is a separate minor item and not blocking.

    Thanks for the thorough writeup and candidate patch. If you still hit a wedged connect-retry loop on a current build after an upgrade, please reopen with jcode --version and jcode debug list output.

  4. added 3 commits that reference this issue on May 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpriority: hightriage: fixed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions