Skip to content

feat(discovery): remove stale process descriptors and detect reused PIDs - #114

Merged
akijakya merged 3 commits into
mainfrom
feat/113-stale-descriptors
Oct 20, 2026
Merged

akijakya merged 3 commits into
mainfrom
feat/113-stale-descriptors

Conversation

@akijakya

Copy link
Copy Markdown
Member

The process adapter skipped a descriptor whose PID was gone but left the file in place. Descriptors piled up whenever a process couldn't remove its own (SIGKILL, a crash, a sandbox killing its children), and once the PID was reused, after a reboot or when PIDs wrap, a stale descriptor was listed again as a live workload.

Changes

  • Stale descriptors are removed: when a scan finds a discoverable descriptor (discovery label matches) whose process is gone, the file is deleted and logged; the workload is withdrawn as before. Descriptors without the discovery label are never touched. A deletion that fails is logged once, like an invalid file.
  • keep_stale (DISCOVERY_RUNTIME_PROCESS_KEEP_STALE, default false) leaves stale descriptors in place, for a directory discovery may only read.
  • started_at (optional, RFC 3339): when the descriptor was written. A process holding the PID that started later than that (with a second of slack) reused the PID, so the descriptor is stale. The process start time comes from /proc/<pid>/stat and /proc/stat on Linux and sysctl kern.proc.pid on macOS; elsewhere, or when it can't be read, only the PID is checked.
  • Docs: the descriptor table, the clean-up behaviour and the new setting.

This reverses the earlier rule that discovery never modifies descriptors (TestWatchEventsProcessExit now asserts removal).

Testing

  • go test ./config/ ./runtime/process/ and golangci-lint run ./... in discovery pass; GOOS=linux go vet and GOOS=windows go build too. New tests: a stale discoverable descriptor is removed and a non-discoverable one kept, keep_stale keeps it, a started_at older than the PID's process is stale while a current one is listed, and isRunning without started_at.

Fixes #113. Part of #105.

🤖 Generated with Claude Code

@akijakya
akijakya requested a review from a team as a code owner October 15, 2026 12:31
@codecov-commenter

codecov-commenter commented Oct 15, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.00000% with 9 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
discovery/runtime/process/start_linux.go 71.4% 8 Missing ⚠️
discovery/runtime/process/adapter.go 96.8% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Cleanup can delete a concurrently replaced live descriptor, making an active workload undiscoverable.

3 open findings
What changed in this PR

Updates process discovery to clean up stale descriptors and detect reused PIDs, supporting accurate running-workload registration.

Changes:

  • Removes stale discoverable descriptors unless keep_stale is enabled.
  • Adds optional started_at checks on Linux and macOS.
  • Documents the behavior and adds cleanup and PID-reuse tests.
File Description
docs/​dir-component-runtime-discovery.md Documents timestamps, cleanup, and configuration.
discovery/​runtime/​process/​start_other.go Provides unsupported-platform fallback.
discovery/​runtime/​process/​start_linux.go Reads process start times from /proc.
discovery/​runtime/​process/​start_darwin.go Reads process start times through sysctl.
discovery/​runtime/​process/​descriptor.go Adds optional started_at.
discovery/​runtime/​process/​config.go Defines KeepStale.
discovery/​runtime/​process/​adapter.go Checks process identity and removes stale descriptors.
discovery/​runtime/​process/​adapter_test.go Tests cleanup, retention, and PID reuse.
discovery/​go.mod Makes golang.org/x/sys a direct dependency.
discovery/​config/​config.go Defaults keep_stale to false.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread discovery/runtime/process/adapter.go Resolved
Comment thread discovery/runtime/process/adapter.go Resolved
akijakya added a commit that referenced this pull request Oct 15, 2026
Address review on #114: removal now checks that the descriptor on disk
is still the file the scan read, so an atomic replacement written since
is left alone; a failed removal stays in the invalid-file cache and is
logged once per file version; and the Linux start time divides before
multiplying, so it doesn't overflow for processes started ~1,068 days
after boot.

Signed-off-by: András Jáky <[email protected]>
akijakya added a commit to agntcy/dir that referenced this pull request Oct 15, 2026
The process adapter skipped a descriptor whose PID was gone but left
the file, so descriptors of killed processes piled up, and once the PID
was reused (after a reboot, or when PIDs wrap) the stale descriptor was
listed as a live workload again. A discoverable descriptor whose
process is gone is now removed (keep_stale leaves it in place), and an
optional started_at lets the adapter tell a reused PID from the
original process on Linux and macOS.

Fixes #113

Signed-off-by: András Jáky <[email protected]>
Address review on #114: removal now checks that the descriptor on disk
is still the file the scan read, so an atomic replacement written since
is left alone; a failed removal stays in the invalid-file cache and is
logged once per file version; and the Linux start time divides before
multiplying, so it doesn't overflow for processes started ~1,068 days
after boot.

Signed-off-by: András Jáky <[email protected]>
@akijakya
akijakya force-pushed the feat/113-stale-descriptors branch from ffdeec2 to 2528b92 Compare October 19, 2026 12:49
@akijakya
akijakya merged commit de65cb2 into main Oct 20, 2026
14 checks passed
@akijakya
akijakya deleted the feat/113-stale-descriptors branch October 20, 2026 10:44
akijakya added a commit to agntcy/dir that referenced this pull request Oct 20, 2026
dir-runtime v1.5.0 includes agntcy/dir-runtime#114. It requires dir
api, client and utils v1.8.0; those are replaced by the local modules.

Signed-off-by: András Jáky <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

process adapter: remove stale descriptors and detect reused PIDs

6 participants