Skip to content

fix(embed): harden daemon startup lifecycle - #4700

Merged
nicoloboschi merged 6 commits into
vectorize-io:mainfrom
diatche:fix/embed-cleanup-startup-timeout
Sep 25, 2026
Merged

nicoloboschi merged 6 commits into
vectorize-io:mainfrom
diatche:fix/embed-cleanup-startup-timeout

Conversation

@diatche

@diatche diatche commented Sep 24, 2026 •

Copy link
Copy Markdown

Summary

  • keep polling when the second readiness probe fails transiently instead of declaring a live daemon crashed
  • retain the spawned Popen handle and terminate that exact child when startup reaches its deadline
  • escalate from terminate to kill after a bounded grace period
  • add regression tests for transient readiness loss, normal timeout cleanup, and escalation when the child ignores termination

Why

The API can become temporarily unresponsive immediately after binding its port while startup backlog work occupies its event loop. The embed manager currently treats one missed stability probe as a crash. Separately, when startup reaches its 180-second deadline, daemon start returns without stopping the detached child. A supervisor retry can then create overlapping API processes.

Together these changes keep a live daemon inside the existing bounded readiness loop and guarantee cleanup if it never becomes stably healthy.

Verification

  • uv run pytest tests/test_embed_manager.py -q — 33 passed
  • uv run ruff check hindsight_embed/daemon_embed_manager.py
  • uv run ruff check --ignore E721 tests/test_embed_manager.py (the file has an unrelated pre-existing E721)
  • uv run ruff format --check hindsight_embed/daemon_embed_manager.py tests/test_embed_manager.py
  • sabotage checks: each new regression test fails when its corresponding production behavior is removed

Related: #4580 (separate detached-process lifecycle issue; not closed by this PR).

@diatche diatche changed the title fix(embed): terminate daemon after startup timeout fix(embed): harden daemon startup lifecycle Sep 24, 2026
@nicoloboschi
nicoloboschi force-pushed the fix/embed-cleanup-startup-timeout branch from 7bee3de to 466afa2 Compare September 25, 2026 12:57
@nicoloboschi

Copy link
Copy Markdown
Collaborator

Thanks! I rebased onto main and pushed a few review fixes: the timeout cleanup now signals the child's whole process group on POSIX (under the uvx fallback the child is the uv launcher, so killing only its pid could orphan the real API), startup now fails fast if the child exits (since a missed stability probe no longer ends the loop), and the --daemon comment now reflects that _HINDSIGHT_DAEMON_CHILD already puts the API in daemon mode. Tests were updated to match.

diatche and others added 5 commits September 25, 2026 15:15
Review fixes on top of the startup-lifecycle hardening:
- signal the child's whole process group on POSIX: under the uvx fallback the
  spawned child is the uv launcher, so terminating only its pid could leave the
  real hindsight-api running as an orphan (Windows keeps Popen.kill)
- detect a child that exited during startup via poll() and fail immediately;
  with the stability probe now non-fatal, a real crash would otherwise wait out
  the full startup deadline
- correct the --daemon comment: _HINDSIGHT_DAEMON_CHILD already puts the API
  in daemon mode, so the flag was redundant (older APIs forked on it)
- tests: shared startup fixtures, a constant clock instead of a fixed
  time.time() sequence, coverage for fail-fast exit, group kill and Windows;
  give test_daemon_client's Popen stub a live process
- patch os.killpg with raising=False: it does not exist on Windows runners
- hindsight-all's Popen stub now reports a live process, so the new
  fail-fast exit check does not read it as a crashed daemon
@nicoloboschi
nicoloboschi force-pushed the fix/embed-cleanup-startup-timeout branch from 466afa2 to 8b6672e Compare September 25, 2026 13:15
signal.SIGKILL and os.killpg do not exist there, so faking platform.system()
is not enough to exercise that path on a Windows runner.
@nicoloboschi
nicoloboschi merged commit 25b539a into vectorize-io:main Sep 25, 2026
209 of 211 checks passed
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.

2 participants