Skip to content

fix(daemon): recover stale service PID files safely - #1469

Open
wizzomafizzo wants to merge 1 commit into
mainfrom
fix/watchdog-pid-reuse
Open

fix(daemon): recover stale service PID files safely#1469
wizzomafizzo wants to merge 1 commit into
mainfrom
fix/watchdog-pid-reuse

Conversation

@wizzomafizzo

@wizzomafizzo wizzomafizzo commented Sep 9, 2026

Copy link
Copy Markdown
Member

Summary

  • Recover a confirmed foreign/reused PID during service Start, under the existing start gate, without signaling that process or changing PID-file format.
  • Keep unknown/inaccessible process identity fail-closed. Stop and Restart still refuse unrelated PIDs.
  • Distinguish cached service executables and shell script arguments from unrelated processes merely mentioning a service file; retain deleted-executable identity across binary replacement.
  • Cover stale-PID recovery with competing starts, healthy-service idempotence, unrelated-process survival and unknown identity.

Ref ZaparooProject/Main_MiSTer#24. This is the Core-owned cleanup companion to Main's watchdog identity check; Core should ship before relying on automatic PID-reuse recovery.

Validated with task test, task lint, task deadlock, task cross-lint:all and task vulncheck. Original code failed the recovery regression. Vulnerability scan reports no called vulnerable code; it notes uncalled dependency advisories. No device service, deployment or release changes. Work isolated from the owner's active checkout.

Summary by CodeRabbit

  • Bug Fixes
    • Daemon startup now recovers from stale PID files that reference unrelated running processes.
    • Unrelated processes are no longer signaled during PID-file recovery.
    • Process identity checks are stricter, improving reliability when validating service processes.
    • Repeated or concurrent starts continue to maintain a single service process.

@coderabbitai

coderabbitai Bot commented Sep 9, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: fca89297-245d-4ca2-aa3c-75340286b7ac

📥 Commits

Reviewing files that changed from the base of the PR and between d346a2e and 3cf1dfa.

📒 Files selected for processing (2)
  • pkg/service/daemon/daemon.go
  • pkg/service/daemon/daemon_test.go

Included review availability: Your plan provides up to 2 included reviews per hour; 1 remains after this review.


📝 Walkthrough

Walkthrough

The daemon now uses typed PID mismatch errors and explicit process identity checks. Startup removes a confirmed stale PID file without signaling the unrelated process. Tests cover concurrent recovery, shell identity validation, and unavailable process identities.

Changes

Daemon PID handling

Layer / File(s) Summary
Process identity validation
pkg/service/daemon/daemon.go, pkg/service/daemon/daemon_test.go
Process identity checks now return errors, handle deleted executables, and restrict shell-backed matches to valid service interpreter arguments. Tests cover rejected arguments and unavailable identities.
Stale PID recovery during startup
pkg/service/daemon/daemon.go, pkg/service/daemon/daemon_test.go
Start removes an unchanged PID file after confirming that its PID belongs to another process. Concurrent starts publish one service PID and do not signal the unrelated process.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to 3cf1d

Daemon startup now safely replaces confirmed stale PID files without signaling unrelated processes, while preserving fail-closed handling for unknown identities. The covered recovery and idempotence behavior is ready to merge.

Sequence Diagram(s)

sequenceDiagram
  participant Start
  participant Running
  participant serviceProcessIdentity
  participant PIDFile
  Start->>Running: Check recorded PID
  Running->>serviceProcessIdentity: Identify recorded PID
  serviceProcessIdentity-->>Running: Confirm foreign process
  Running-->>Start: Return servicePIDMismatchError
  Start->>PIDFile: Re-read and remove unchanged stale PID
  Start-->>Start: Launch service
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 10 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: safely recovering stale service PID files during daemon startup.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch

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.

@codecov

codecov Bot commented Sep 9, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 78.78788% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/service/daemon/daemon.go 78.78% 7 Missing ⚠️

📢 Thoughts on this report? Let us know!

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