Skip to content

Add kqueue parent-death watcher for Python sidecars#301

Merged
EtanHey merged 1 commit into
mainfrom
fix/kqueue-pdeath
May 18, 2026
Merged

Add kqueue parent-death watcher for Python sidecars#301
EtanHey merged 1 commit into
mainfrom
fix/kqueue-pdeath

Conversation

@EtanHey
Copy link
Copy Markdown
Owner

@EtanHey EtanHey commented May 18, 2026

Summary

  • add a macOS/BSD kqueue NOTE_EXIT parent-death watcher for BrainLayer Python sidecars
  • install it in BrainBar/MCP/daemon/watch/enrich/drain sidecar entry points
  • add a real subprocess test that proves a child exits within 1s after its parent dies

Verification

  • PYTHONPATH=src pytest tests/test_parent_death.py tests/test_brainbar_hybrid_helper.py tests/test_cli_enrich.py tests/test_jsonl_watcher.py tests/test_daemon_kg.py tests/test_mcp_input_schema_limits.py tests/test_lifecycle.py -q
  • ruff check src/ tests/ brain-bar/Scripts/brainbar_stdio_adapter.py
  • ruff format --check src/ tests/ brain-bar/Scripts/brainbar_stdio_adapter.py
  • swift test --filter SocketIntegrationTests (from brain-bar/)

Notes

  • The kqueue behavior is active only where Python exposes select.kqueue; other platforms no-op.
  • BRAINLAYER_DISABLE_PARENT_DEATH_WATCH=1 disables the watcher for emergency/debug runs.
  • Full local pytest collection is blocked by local environment deps: missing deepchecks and NumPy 2.4 incompatible with numba/ranx.

Note

Medium Risk
Medium risk because it changes lifecycle/termination behavior for multiple persistent processes and uses a background kqueue thread with os._exit, which could surface platform-specific edge cases.

Overview
Adds a new brainlayer.parent_death.install_parent_death_watcher() implementation using BSD/macOS kqueue NOTE_EXIT to terminate Python sidecar processes when their parent exits (with an env var escape hatch and no-op behavior on unsupported platforms).

Installs the watcher at startup across several long-running entry points (daemon, MCP stdio server, drain loop, enrichment pipeline runner, hybrid helper, CLI enrich/watch, and BrainBar’s brainbar_stdio_adapter).

Introduces tests/test_parent_death.py, including a subprocess-based integration test that verifies the child process exits shortly after its parent terminates, plus a unit test for the no-kqueue fallback and a check that entrypoints include the watcher.

Reviewed by Cursor Bugbot for commit edf76d1. Bugbot is set up for automated code reviews on this repo. Configure here.

Note

Add kqueue parent-death watcher to Python sidecar processes

  • Adds parent_death.py with install_parent_death_watcher(), which uses kqueue/EVFILT_PROC to monitor the parent PID and calls os._exit(0) in a daemon thread when the parent exits.
  • Installs the watcher at startup in all long-running entrypoints: daemon, drain, MCP stdio server, CLI enrich and watch commands, hybrid helper, enrichment pipeline, and the stdio adapter.
  • Can be disabled by setting BRAINLAYER_DISABLE_PARENT_DEATH_WATCH; no-ops silently on platforms without select.kqueue (i.e. non-BSD/macOS).
  • Adds tests covering the no-kqueue no-op path, a live parent-death integration test, and a coverage check that all entrypoints call install_parent_death_watcher.

Macroscope summarized edf76d1.

Copy link
Copy Markdown

@greptile-apps greptile-apps Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 18, 2026

Warning

Rate limit exceeded

@EtanHey has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 4 minutes and 21 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 8e8a2b3a-ad94-4b5e-bd2a-c1d743b844b1

📥 Commits

Reviewing files that changed from the base of the PR and between 8bf7b75 and edf76d1.

📒 Files selected for processing (9)
  • brain-bar/Scripts/brainbar_stdio_adapter.py
  • src/brainlayer/brainbar_hybrid_helper.py
  • src/brainlayer/cli/__init__.py
  • src/brainlayer/daemon.py
  • src/brainlayer/drain.py
  • src/brainlayer/mcp/__init__.py
  • src/brainlayer/parent_death.py
  • src/brainlayer/pipeline/enrichment.py
  • tests/test_parent_death.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/kqueue-pdeath

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@EtanHey
Copy link
Copy Markdown
Owner Author

EtanHey commented May 18, 2026

@codex review
@cursor review
@BugBot review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@EtanHey EtanHey merged commit 30ecb3b into main May 18, 2026
7 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.

1 participant