Skip to content

Fix clock-dependent RAM accounting identities - #164

Merged
den-sq merged 2 commits into
mainfrom
fix/162-stable-memory-process-identity
Oct 21, 2026
Merged

den-sq merged 2 commits into
mainfrom
fix/162-stable-memory-process-identity

Conversation

@den-sq

@den-sq den-sq commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

Clock adjustments could make the RAM collector count the same live worker repeatedly or discard a valid manager manifest. This replaces wall-clock identity keys throughout the collector with psutil's stable Process identity, so adjustments between discoveries or during a sweep neither multiply contributions nor create false process churn.

The root handshake and manager manifests now share one validated IPC adapter. Missing, malformed, unsupported or unreadable identities invalidate coverage instead of falling back to PID/public creation time or silently dropping a worker. A per-sweep invariant rejects multiple live identities for one PID. Psutil is pinned to 7.1.3 in package metadata, the lockfile and focused CI; the adapter also rejects unverified runtime versions.

Closes #162.

Shared-basis audit

  • psutil Process owns process identity and equality/hash semantics. Previously _identity, root_created, manager pid/create_time fields, and manifest matching reconstructed a competing wall-clock identity. Those writers, carriers and comparisons are removed. known, excluded, and per-sweep owner maps now use Process objects. Root startup consumes its token once and retains only the validated Process.
  • memory_ipc owns the boundary adapter and existing file retry policy. It exports psutil's private _ident with its verified version/platform only when publishing control/manager JSON; receipts validate against Process identity. No kernel start counter or clock-derived alternative is calculated. Manifest bytes retained during a sweep are only allocation-change evidence, not identity keys. The private API is the issue's chosen tradeoff: exact dependency pin plus native identity/IPC/clock upgrade tests. Generated wheel metadata requires psutil==7.1.3, covering production pip installs as well as Poetry.
  • Liveness still derives from psutil equality. A fresh Process identity is validated before comparing it with the retained object, using the same native identity query as is_running(). This avoids psutil treating (pid, None) as exit/PID reuse and silently losing a reparented worker. Genuine exit/reuse remains detectable; transient identity access failure remains recoverable.
  • The shared-memory manager remains the allocation owner. The existing owner registry, publication generation, mincore/native readers, session failure flag, sampling windows, operation lifecycle and cleanup are reused. No parallel allocation registry, new memory metric, or system-RAM counter is added.

Validation

  • New short regressions use real psutil objects, subprocesses, a production observer and a real shared-memory manager. They inject clock-conversion changes without changing the host clock and assert exact process membership, one native read per PID, stable observer exclusion and one contribution per touched segment.
  • Covers root IPC across adjustment; manager publication before first discovery and updates while observed; forward/backward changes between discoveries and during a sweep; missing/private-API/version/unknown-identity errors; stale owners, PID reuse, genuine process/allocation churn and cleanup.
  • Local Linux identity suite: 26 passed. Full Python suite: 407 passed, 2 native-platform skips in 29.69 seconds; required helper coverage: 99%.
  • All six native Linux/Windows/macOS jobs passed on Python 3.11/3.12, including the new identity tests: native CI run. Full Python coverage, desktop test coverage and documentation CI also passed.
  • poetry check --lock passes. Its existing metadata-deprecation warnings match the base commit. Package metadata confirms the exact psutil constraint; git diff --check passes.

Limits and follow-ups

Discovery continues to use psutil's children(): its own public-timestamp filtering can omit a previously unseen child if the clock moves backward inside one enumeration. Previously observed live workers remain independently checked. This does not claim an atomic or complete process census.

Collector overhead and real-run coverage validation remain in #163; replacement RAM measurements/publication tables remain in https://github.com/den-sq/ouroboros_pub/issues/121. This repair does not validate archived peaks or certify a 1% overhead bound. Existing #139 native-macOS workflow and macOS-host Docker deferrals remain unchanged.

@tavateva tavateva 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.

APPROVE at d1f4cb8fee0e99175e074b86e507ade7a79d73f6.

This fixes #162 at the identity owner rather than patching each symptom:

  • known, excluded and the per-sweep maps now hold psutil.Process objects, so psutil's own equality and hash are the identity.
  • _identity, root_created and the manifest's pid/create_time pair are gone; no create_time() caller remains under python/ouroboros.
  • The two cross-process carriers (the root handshake and the manager manifest) go through a single adapter in memory_ipc. It exports _ident tagged with the psutil version and platform, and receivers only use it to match a live Process.

This is the choice my comment on #162 asked to have named: private value, exact pin and native tests. It is declared in the PR body, pyproject.toml and run-statistics.md. It also covers both macOS (unadjusted native start) and Windows (native creation value) without the PR calculating its own platform identity.

Failure envelope, checked against the code:

  • A psutil other than 7.1.3 makes export_process_identity raise during MemoryCollector.start. RunStatistics.start_memory turns that into an unavailable RAM metric, and the operation still runs. I confirmed this with psutil 7.2.2: RuntimeError: Memory identity requires psutil 7.1.3; got 7.2.2.
  • An old-format or unverifiable manifest owner raises ValueError instead of quietly counting as owning no segments, so that sweep is incomplete rather than under-counted.
  • process_is_running validates the fresh identity before comparing, so a (pid, None) wrapper can't be cached as "PID reused" and drop a reparented worker.

Verification at the head (Linux, psutil 7.1.3 overlay):

  • The four memory suites, including the new test_memory_identity.py, ran with -W error::DeprecationWarning: 105 passed. The full Python suite: 407 passed, 2 skipped, which matches the body.
  • #162's own reproduction, adapted to the new constructor: 50 sleeping children, boot time moved +0…+30 s and then back to −10 s. The sampler reported 51 / 51 / 51 / 51 processes, with 51 unique PIDs throughout. Before the fix the same script printed 51 / 251 / 1551 at 4502a3c.
  • Boot time moved between the two discoveries inside a sweep: 5 of 5 sweeps were complete, where before they would have failed with "Process tree changed".
  • All CI checks are green, including all six native Linux/Windows/macOS collector jobs.

I did not run Docker/WSL2. That and the overhead check are correctly moved to #163.

Non-blocking notes

  1. Wrong-repo link in the docs. run-statistics.md L410 links "#121" to ChengLabResearch/ouroboros/issues/121, which is a closed Electron dependency-bump PR. The intended target is den-sq/ouroboros_pub#121, as in the PR body. Fixing the URL is enough.
  2. "Transient identity access failure remains recoverable" holds for sweeps, not the idle poll. Inside sample(), a ValueError from process_is_running becomes one incomplete sample. In memory_observer.main's waiting and idle loops, process_is_running(sampler.root) is outside the per-sample try, so the same error ends observation and the run reports unavailable. Exposure is close to nil, because the root is the backend itself and readable by the same user on every platform, and failing to "unavailable" is the conservative direction. Narrowing the body claim, or catching ValueError there, is either sufficient.

@den-sq
den-sq merged commit e81c4d4 into main Oct 21, 2026
9 checks passed
@den-sq
den-sq deleted the fix/162-stable-memory-process-identity branch October 21, 2026 21:00
@tavateva tavateva mentioned this pull request Nov 9, 2026
9 of 13 tasks
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.

Fix clock-dependent process identity corrupting RAM statistics

2 participants