Skip to content

macOS: daemon stop orphans a live daemon when the daemon and the client run under different LC_TIME locales (ps -o lstart is not locale-pinned) #4656

Open

Description

@nstfn

Purpose

On macOS, agent-device identifies a process by its pid plus the ps -o lstart= start time, and compares the two start strings exactly. /bin/ps prints lstart in the language of LC_TIME. A daemon spawned under LANG=fr_FR.UTF-8 records mar. 17 nov. 19:42:27 2026. A client under LC_TIME=C reads Tue Nov 17 19:42:27 2026 for the same pid. daemon stop then answers stopped: false, mode: not-running, cleanupConfidence: known, deletes daemon.json and the lock, and leaves the daemon alive. daemon status then reports state: stopped while the process runs.

The Linux fix for the same comparison (#4437, #4438) reads /proc and does not cover macOS. On Windows the start time is already read with InvariantCulture (host-process.ts:478).

Environment

  • agent-device 0.21.39 (npm). The code path is unchanged on main at 655ede279.
  • macOS 27.2.1 (26B131), Apple Silicon, Node 24.21.0, Xcode 27.2 (27B52).
  • No device is needed.

Reproduction

sd=$HOME/.agent-device-lanes/adr5-bug
# 1. Spawn a daemon under a French locale (any command that starts one).
env -u LC_ALL -u LC_TIME LANG=fr_FR.UTF-8 agent-device devices --state-dir "$sd" --json
grep processStartTime "$sd/daemon.json"
#   "processStartTime": "mar. 17 nov. 19:42:27 2026"
# 2. Stop it from a client whose LC_TIME differs.
LC_TIME=C agent-device daemon stop --state-dir "$sd" --json
#   {"success": true, "data": {"stopped": false, "mode": "not-running", "cleanupConfidence": "known", ...}}
ps -p <pid> -o pid=,lstart=,command=
#   72247 Tue Nov 17 19:42:27 2026  .../node .../agent-device/dist/src/internal/daemon.js
ls "$sd"          # daemon.json and the lock are gone
LC_TIME=C agent-device daemon status --state-dir "$sd" --json
#   {"success": true, "data": {"state": "stopped", ...}}   (the process is still alive)

The daemon log then shows daemon_metadata_lost (state: absent). With no session, the daemon exits at its idle window and logs daemon_shutdown_record_failed / daemon_registration_finish_failed ("Process lock acquisition is no longer held"). On 0.21.35 we saw the same orphan with two daemons that held sessions: they lived until their session idle expiry (AGENT_DEVICE_SESSION_IDLE_TIMEOUT_MS), about 1 h later. Without that expiry, a daemon that holds a session does not idle out.

Control: the same steps with the French locale on both sides answer stopped: true, mode: graceful, and the process is gone 2 s later.

Cause (source at v0.21.39)

  • host-process.ts:321-339: the identity read runs /bin/ps -p <pids> -o pid=,state=,lstart= with no env. The single-field read (:395) and the async read (:199) are the same.
  • host-process.ts:243-251: processStartTimesDiffer returns left !== right for two non-Linux strings, so two renderings of one start time "differ".
  • daemon-client-startup.ts:658-659 compares info.processStartTime === identity.startTime.
  • Inferred, not measured: device claims (ownerStartTime, device-claim-inspection.ts:200) and the runner lease (runner-lease.ts:530) compare the same strings, so a claim written under one locale can read as owner-process-reused from another.

Required behavior

  • The macOS (and BSD ps) start-time read gives the same string whatever the caller's LANG, LC_TIME or LC_ALL. For example, run ps with env: { ...process.env, LC_ALL: 'C' } (ExecOptions.env, exec.ts:21-23), or parse lstart into a locale-free token, like the linux: token of fix(host-kit): keep Linux process identity stable across clock steps #4438.
  • A record written before the fix in another locale does not prove a different lifetime: compare it as "unknown", not as "differs", so an upgrade does not orphan the daemons that are already running.

Completion conditions

  • The reproduction above answers stopped: true, mode: graceful, and the process is gone.
  • A unit test pins the ps environment, or the parsed token, for lstart.

Dependencies

Blocked by: none. Sibling of #4437, whose fix #4438 covers Linux only.

Workaround

Export LC_TIME=C (and leave LC_ALL unset) in every shell that runs agent-device, so daemons and clients share one rendering.

Activity

  1. thymikee commented on Nov 18, 2026

    @thymikee
    Member

    Triage: bug; actionable without more input from you. The synchronous, batched and asynchronous ps identity reads on current main inherit the caller locale, while non-Linux start-time strings are compared verbatim. Fix that at the shared host-process owner and treat incompatible existing records as unknown evidence, never permission to reclaim a live process. #4659 changes shutdown transport; it does not remove this identity mismatch for daemon, claim and lease consumers.

  2. thymikee commented on Nov 18, 2026

    @thymikee
    Member

    I’m following up in #4682 to narrow the locale pin to start-time reads and check the shutdown path on macOS.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions