Repository navigation
fix(plugin): defer rescue-spawn while the managed bridge unit is active - #2589
syzsunshine219 wants to merge 2 commits into
Conversation
…ve (MemTensor#2467) During a systemd restart window (~8-10s) :18800 probes "free" while the supervised daemon is mid-start. ensure_viewer_daemon treated that as spawn clearance, so a session host rescue-spawned its own --daemon, won first bind, and the memos-bridge.service unit crash-looped forever on "port busy" while the rogue answered /api/v1/health green (413 restarts/24h observed in MemTensor#2467). Killing the rogue did not help: the host keepalive/reconnect path re-entered ensure_viewer_daemon within ~3s (shared_bridge_runtime.reconnect runs before_spawn on every respawn). Gate the rescue-spawn path on `systemctl is-active` for MEMOS_BRIDGE_SYSTEMD_UNIT (default memos-bridge.service): while the unit is active/activating, wait up to 20s for the managed daemon to take the port (report the viewer up when it does) instead of racing it; spawn again only once the unit leaves active/activating. Any failure of the check (no systemctl, no systemd bus, timeout) degrades to today's unconditional rescue behavior, so macOS/Windows/containers and manually managed daemons are unaffected. Unit-side yield (bind-retry backoff for a daemon losing first-bind to a mid-start unit) is left as follow-up work.
Acceptance requires the repo-enforced `ruff format --check` (.github/workflows/python-tests.yml:95-96) to pass. No semantic change: one gate log string joined onto a single line, patch.object call wrapping normalized in the new test file. Verified: uvx ruff@0.11.8 format --check . -> 636 files already formatted; ruff check --no-fix . -> All checks passed; python test suite still 144/144 OK.
🤖 Open Code ReviewTarget: PR #2589 🔍 OpenCodeReview found 3 issue(s) in this PR. 1.
|
|
Restores the change proposed in #2483 for review against
mainafter its former base branch,dev-v2.0.36, was deleted. The source commits and authors are preserved.Source head:
6f042035938e667c4529416eb8e07b84d6033ed8This PR is open for review. Review and CI against the current base are pending; validation claims in the original description below refer to the earlier snapshot. The original discussion and evidence remain in #2483.
Original change description
During a systemd restart window (~8-10s) nothing is bound on :18800 yet, so
_probe_viewer()can only classify the port as free/running_memos/occupied/unknown and the free verdict is read as spawn clearance. The session host then rescue-spawns its own--daemon(_bridge_command(daemon=True)+Popen(start_new_session=True)), wins first bind, andmemos-bridge.servicecrash-loops forever on "port busy" while the rogue answers/api/v1/healthgreen. Killing the rogue does not help: both host keepalive paths (legacy_start_bridge_keepaliveandshared_bridge_runtime.reconnect:219, whosebefore_spawnruns on every respawn) re-enterensure_viewer_daemonwithin ~3s, andkillExistingBridgeinbridge.cts(viadaemon/bridge.pid) makes the war bidirectional.Description
Add a managed-unit gate to the rescue-spawn path of
ensure_viewer_daemon()(apps/memos-local-plugin/adapters/hermes/memos_provider/daemon_manager.py:538, acquired after theviewer-start.lockand before thePopen):_managed_daemon_gate()(daemon_manager.py:403) runssystemctl is-activeonMEMOS_BRIDGE_SYSTEMD_UNIT(defaultmemos-bridge.service, env-overridable, empty disables the gate). While the unit reportsactive/activating, it waits up to 20s (1s poll,daemon_manager.py:68-69) for the managed daemon to take :18800.runningandensure_viewer_daemon()reports the viewer as up and returnsTruewithout spawning.active/activating(failed/inactive) does the rescue-spawn proceed as before.systemctl, no bus, timeout) degrades to the current behavior — macOS, Windows, containers and hand-managed daemons are unaffected.Root cause and full trace are in #2467.
Related Issue (Required): Fixes #2467
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Red (pre-fix baseline, acceptance run): from
apps/memos-local-plugin:→
Ran 12 tests, FAILED (failures=3, errors=6)— the 3 failures are exactly the brokenpopen.assert_not_called()assertions:Popenwas invoked with the rogue command line from the issue (node --import tsx .../bridge.mts --agent=hermes --daemon); the 6 errors reference the new helpers that did not exist yet (expected red).Green (post-fix, re-run on this branch at commit
6f042035):Lint (
pyproject.toml:186pins ruff^0.11.8; run viauvxat the locked version because no local ruff binary):(CI enforces both:
.github/workflows/python-tests.yml:95-96.)Checklist
Reviewer Checklist
Transparency note: this fix was produced by an AI agent (Claude Code, GLM) working via the
akushonkamenaccount, as already disclosed in the issue claim comment. All commands and numbers above were executed against this branch.