In Python’s multiprocessing.shared_memory, the unlink() method removes the name of a shared-memory block but does not immediately destroy the memory if other processes still have it open. Under POSIX shared-memory lifetime rules, after unlink() the segment’s name is gone, yet any existing file-descriptors in other processes keep the memory alive until the last close. Windows behaves differently: unlink() is effectively a no-op, and memory is freed when all handles close. Real-world systems often see misleading green results if this nuance is ignored: a test may show no errors even though a shared-memory leak persists. This playbook defines a strict acceptance test: clearly specify what “ownership of bytes” means post-unlink(), design a disposable fixture with synthetic processes, and build an evidence ledger to decide ACCEPT/REPAIR/RERUN/HOLD. We rely on documented contracts (Python’s shared-memory and OS docs) as oracles, then verify actual behavior in a pinned environment. Related validation guides like Refonte Learning’s executor shutdown validation guide encourage defining acceptance preconditions and capturing evidence rather than trusting a brief success signal.
Our problem statement: after a shared memory block is unlinked by one process, exactly which process(es) retain access to the data, and when is the memory finally reclaimed? We must distinguish the documented contract (from Python’s library and POSIX standards) from observed behavior in CPython. For example, the Python doc states that unlink() “should be called only once per block… in any order” with close(), and warns that accessing data after unlink may fail. POSIX clarifies that the name is removed but the contents live on until the last file descriptor is closed. However, known issues (e.g. Python’s shared-memory resource-tracker issue) have shown resource-trackers can prematurely destroy segments, betraying the contract. Our acceptance contract will encode exactly what must be true at each step (creation, attach, close, unlink) and what evidence proves it. For instance, we must prove which process still holds the bytes after unlink, and that no unintended cleanup occurs. Each hypothesis will require concrete proof (e.g. reading memory, checking for errors) rather than trusting a silent return or absence of warning.
At the end, we produce an evidence ledger mapping each case (process A unlinks, child B attached, etc.) to outcomes (data accessible, error, cleanup) and decide whether the behavior meets the documented contract or needs repair. Like other Refonte Learning validation playbooks, each result is marked EXPECTED / NOT EXECUTED unless we actually run the test harness. We use official docs and standards (Python 3.15 shared_memory doc, POSIX shm_unlink manpage) as our primary oracles, and note any discrepancies found in practice or known bug reports.
Define the acceptance contract
We start by translating the question into precise testable propositions. The public contract comes from Python and POSIX: on POSIX, shm_unlink(name) removes the name but “the removal of the memory object contents shall be postponed until all open and map references to the shared memory object have been removed”. Python’s doc echoes this: “unlink() and close() can be called in any order, but trying to access data inside a shared memory block after unlink() may result in memory access errors”. Windows, by contrast, deletes the block only after all handles close. We must prove which memory survives an unlink() and who owns it until final cleanup. The key acceptance conditions are therefore:
• Correct identification of surviving handles: When one process unlinks a block, any other process that previously opened it should still see the original data (on POSIX, since the memory content is kept alive). A new process should NOT be able to attach by that name (the name is gone). We must evidence the continued existence of the memory contents in the remaining process(es).
• Single-point unlink: Exactly one unlink should be needed per block. Calling unlink() again (even from another handle) should fail (with FileNotFoundError or equivalent). We verify this by attempting a second unlink and expecting an error.
• Proper resource-tracker behavior: If track=True (default), Python’s resource-tracker may auto-unlink remaining segments when a process exits. We expect that behavior to follow documented limits: with a single multiprocessing tree, a common tracker cleans up only after all processes are done. With spawn/subprocess creating independent trackers, the first to exit might prematurely unlink the segment, which is a known bug. Our contract must account for both: if a resource tracker is in play, then the latest exiting process should trigger a single unlink and not kill memory that others still need.
• Robust cleanup: After the final relevant process closes its handle, the shared memory should be gone (no dangling /dev/shm file on POSIX, no remaining handles on Windows). The evidence protocol should confirm the segment’s removal at that point.
Invalid substitutes (false positives) include simply seeing unlink() return without exception, or not observing an immediate crash. We must test explicitly that data persists (or errors) as expected, not just assume from unlink() returning normally. For each scenario, we will define a proof: e.g., process B reads known bytes after A unlinks; a fresh attach attempt fails; a second unlink errors; resource-tracker warning appears (or not) on exit.
Following the executor shutdown validation guide, we set up a table of propositions and required proofs:
Proposition | Required proof | Invalid substitute |
Process B still accesses original bytes after A unlinks. | B reads the same byte pattern that A wrote before unlink. | B only sees initial zeros or garbage. |
No process except B has the memory. | Any other process (C) trying to attach by name fails (name not found). | Absence of error message (we need a FileNotFoundError). |
Exactly one process executed the unlink. | The second attempt to unlink raises FileNotFoundError or OSError. | Relying on silent success return from first unlink. |
Resource tracker does not unlink prematurely. | If track=True, the process that created the block runs to completion, then no UserWarning: leaked shared_memory appears (or appears only after all processes close). | No warning while other processes still active (could be hidden). |
All of these are labelled EXPECTED / NOT EXECUTED until we run them. The fixture will drive the above checks systematically. At the end, we only accept the behavior if all contract points are proven. Any silent “OK” or absence of an exception will be scrutinized against the oracle specifications (Python docs, POSIX man).
Capture the baseline and pin versions
A rigorous acceptance test requires knowing exactly which Python/runtime we’re using. The test harness must record its environment: interpreter build, OS, and code versions. We target, say, CPython 3.15.x on Linux (modern POSIX), where SharedMemory has track support (added in 3.13). We lock this in by a pre-test script that prints and verifies the environment. For example:
python3 - <<'PY'
import hashlib, pathlib, platform, sys, multiprocessing
# Check we are on CPython 3.15.*
assert sys.implementation.name == "cpython", sys.implementation.name
assert sys.version_info[:2] == (3, 15), sys.version_info
# Record versions and fixture checksum
fixture = pathlib.Path("shm_fixture.py")
print("implementation =", sys.implementation.name)
print("version =", sys.version.strip())
print("executable =", sys.executable)
print("platform =", platform.platform())
print("fixture_sha256 =", hashlib.sha256(fixture.read_bytes()).hexdigest())
PY
An illustrative baseline output would look like this (EXPECTED / NOT EXECUTED):
implementation = cpython
version = 3.15.2 (tags/v3.15.2:0a1b2c3, Mar 5 2024, ...) [GCC 12.1.0]
executable = /usr/bin/python3.15
platform = Linux-6.1.0-...-x86_64-with-glibc2.31
fixture_sha256 = abcdef1234567890...
All entries are EXPECTED / NOT EXECUTED. We assert that sys.implementation.name == "cpython" and version matches (to ensure a patch upgrade hasn’t crept in). A mismatch (say running 3.15.1 instead of 3.15.2) does not imply incorrect behavior, but invalidates the evidence packet. We would record “RERUN” to re-pin to the desired build. We cite the exact SharedMemory documentation version that matches this Python release, so the oracle is aligned with what the interpreter should do.
Pin exact environment versions
Before any test runs, the script checks:
python3 - <<'PY'
import platform, sys
print(sys.implementation.name, sys.version_info[:3], sys.version)
print(platform.system(), platform.release(), platform.machine())
PY
For a valid run (CPython 3.15.x on Linux), output would look like (EXPECTED):
Field | EXPECTED |
Implementation | cpython |
Version | 3.15.x (e.g. 3.15.2) [GCC ...] |
OS | Linux, e.g. Ubuntu 22.04 (kernel 6.x) |
Architecture | x86_64 |
If any of these assertions fails, we must RERUN on the correct interpreter. The output is saved (evidence/baseline.txt) for audit. We cite Python’s multiprocessing docs to confirm the SharedMemory behavior is stable in this version.
Build the disposable controlled fixture
We write a self-contained test script (shm_fixture.py) that creates a shared memory block, spawns a child process to attach, and orchestrates the close/unlink sequence with synchronization. The fixture must run on a fresh temporary directory to avoid external interference, and it should not depend on any external file or service. It should explicitly manage all SharedMemory objects and processes to avoid leaks. A sketch in Python would be:
import multiprocessing, os, sys, time
from multiprocessing import shared_memory, Event, Process
def child_process(name, ready_event, done_event):
# Attach to existing block
shm = shared_memory.SharedMemory(name=name)
ready_event.set() # signal attachment done
done_event.wait() # wait for parent signal to proceed
data = bytes(shm.buf[:4]) # read some bytes
print("child read:", data)
shm.close()
# Optionally call unlink here if testing double-unlink error
done_event.wait()
if name == "__main__":
# Create a new shared memory segment
shm = shared_memory.SharedMemory(create=True, size=1024)
name = shm.name
# Write a known pattern
shm.buf[:4] = b'ABCD'
# Prepare synchronization events
ready = multiprocessing.Event()
done = multiprocessing.Event()
# Spawn child in a separate process (uses new resource tracker if fork/spawn)
p = Process(target=child_process, args=(name, ready, done))
p.start()
ready.wait() # wait until child has attached
# At this point, both parent and child have shm open
shm.unlink() # parent unlinks the shared memory name
print("parent unlinked")
done.set() # tell child to proceed and read data
done.clear() # reset for later signals
time.sleep(0.1) # give child time to read
done.set() # allow child to exit
p.join()
shm.close()
In prose, the fixture:
• Process A (parent) creates SharedMemory(create=True) and writes bytes "ABCD".
• It then starts Process B (child) (with multiprocessing.Process so that track=True creates a new resource-tracker for B, since spawn/fork+exec).
• Parent waits for child to attach (via Event), then calls shm.unlink(). According to docs, the segment name is removed here.
• Parent signals child to read the first 4 bytes and then to exit.
• We print ABCD from the child to show data.
• Finally, parent closes its handle and waits for child to finish.
This fixture is disposable: it cleans up (closes) and optionally calls unlink() only once. It uses multiprocessing.Event for deterministic ordering (no sleeps except the 0.1s grace). We use Python’s standard library only. The exact code is pinned by its checksum so any change is intentional.
The code above is a blueprint and is NOT EXECUTED. When run, we expect these behaviors (EXPECTED until run): parent writes "ABCD", child reads "ABCD", second shm.unlink() attempt (if made) raises an error. Any outputs (like prints or warnings from the resource tracker) will be captured in an evidence log. The fixture is pinned to the selected Python version, so every run uses the same semantics.
Author the independent expected-case manifest
Next, we list all test cases with their conditions and expected outcomes, independent of executing them. Each case is a row in our manifest (or a Markdown table) with: Case ID, description, and the expected result (PASS/FAIL or data/error). For example:
Case | Condition | Expected Outcome |
1 | track=True, parent unlinks after attach | Child reads b'ABCD', no errors |
2 | track=False, parent unlinks after attach | Same as Case 1 (explicit track=False decouples trackers) |
3 | track=True, parent unlinks before child attach | Child fails to attach (FileNotFoundError) |
4 | Double unlink(): parent calls unlink twice | Second unlink raises FileNotFoundError |
5 | Child exits then parent exits (both close) | No leaked segment (cleanup done) |
6 | Parent exits early (killed) | Resource-tracker warning or leak detection (if any) |
7 | Windows semantics (if run on Windows) | Unlink no-op; memory freed after last close (no error) |
Each entry is EXPECTED / NOT EXECUTED until we run them. The “Condition” column describes exactly when unlink and close happen, and whether track is True/False. “Expected Outcome” is our independent oracle-based prediction. For example, Case 3 expects an attach failure because the name is gone, per the docs. Case 4 expects that trying unlink() a second time should fail (tracked by Python’s implementation). Case 6 is a boundary: if the parent is killed, on POSIX the resource-tracker of the surviving child might or might not unlink; we predict a warning about a leaked segment (this is a known bug that we will verify).
Each expectation comes from the documentation or known behavior. None of these expectations rely on having run the code. We ensure the manifest is independent: the expected outcome is written before running the fixture. This is crucial so we do not bias the test by hindsight. After execution, we will fill in Observed results to compare against this manifest (see Evidence Ledger below).
Specify a falsifiable negative control
A robust test needs a negative control: a scenario designed to fail and thus prove our ability to detect a problem. Here, a suitable negative control is trying to attach to a name that has been unlinked. For instance, after Case 1, attempt a fresh SharedMemory(name) in a new process or after the parent unlink. We assert this should raise FileNotFoundError. A brief code example:
try:
shm_new = shared_memory.SharedMemory(name)
except FileNotFoundError:
print("EXPECTED: attach failed after unlink")
else:
print("UNEXPECTED: attach succeeded after unlink")
This is a falsifiable test: if a post-unlink attach ever succeeds, we violate the documented contract. We mark the expected behavior (“attach failed with FileNotFoundError”) and will flag any deviation. A negative control also includes verifying that calling close() on an already closed block has no effect and causes no hidden deletion.
Reproduce the misleading green result
We must be careful not to accept a “green” result that misses the real issue. An example of a misleading test is simply checking that no exception is raised by the code above. If our harness only looked at “did the script exit with code 0?”, we’d wrongly assume success even if the child never actually read data or if the memory was silently freed.
For example, suppose we ran the fixture and only verified that p.join() returned (and printed nothing visible). That by itself is not proof that b'ABCD' was read. It could be “green” even if the child got garbage. To catch this, we deliberately include printouts and read-back checks as shown. If the child prints an incorrect value, our script should detect that. Another misleading green: trusting a zero return code while the child’s assert data == b'ABCD' actually failed internally. We avoid that by capturing output.
We demonstrate the pitfall with a modified pseudo-case: remove the child’s read and see only “parent unlinked”. A naive test might say “no errors, PASS”, missing the fact that we never confirmed data. We must check the child’s output. All real checks (like comparing bytes, catching exceptions) are explicitly coded. If we ran only
python3 shm_fixture.py
and it printed just “parent unlinked” and exited 0, that would be misleading green. Instead, we require the child’s line child read: b'ABCD' to appear, confirming data integrity. We label any such incomplete check as NOT SUFFICIENT.
By explicitly recording prints and return values, we avoid false positives. For instance, if unlink() were mistakenly freeing memory early, the child would not print b'ABCD', so the test would fail. A truly “green” outcome requires our own assertions to all pass, not just an absence of errors.
Run a valid comparison case
To further ensure we see the right behavior, we run a control scenario that changes one variable. For instance, repeat the main case but with track=False on both processes. According to docs, on POSIX this should behave identically to track=True for this simple case, but it ensures no independent resource-trackers exist. In that scenario, the parent’s resource-tracker would not register the block, so the first exiting process should not cause any auto-unlink.
If track=True was inadvertently causing early deletion, the track=False run will show the memory surviving correctly. Conversely, if track=True case fails, track=False success would confirm the fix is to disable tracking in such mixed scenarios (as Python docs advise). We treat this as a valid comparison: it is the same test but without potential interference. Any difference in behavior (e.g. child reads bytes in one case but not the other) is critical evidence.
We also compare with the “no-op” case: have parent and child in the same process (using fork or threads), where only one resource-tracker is used. In that case the documented lifetime guarantee is that nothing bad happens until both detach. This yields a baseline: if the problem only occurs when processes have separate trackers, we know it’s a tracker issue, not a core OS problem.
All observations from this comparison case are added to our ledger. For example, if track=True triggered a premature unlink, but track=False did not, that is evidence to “REPAIR” by forcing track=False. If both behave the same, it confirms consistency.
Preserve failure artifacts
When tests fail (as expected in negative or buggy cases), we must save the output, logs, and any orphaned resources. For instance, if a shared-memory object is leaked, we capture the resource-tracker warning (“1 leaked shared_memory object”) in the console log. If a crash occurs, we dump the stack trace. We do not discard these artifacts when retrying. For example, if an earlier run left /dev/shm/psm_* files, we document their names and deletion status. Our harness should never auto-clean them before recording; we want to know exactly what the broken run produced. This way, the evidence ledger will clearly show “Case 6: Parent killed, leaked segment remained (warning printed): FAIL”. We include code snippets or shell commands used to detect leftovers (e.g. ls /dev/shm | grep {name}), and mark their results. This disciplined capture ensures we don’t hide intermittent bugs behind cleanup.
Exercise negative and boundary cases
We enumerate additional edge conditions to test robustness:
• Attach after unlink: As in our negative control, the new process should get FileNotFoundError. We mark this must-fail (and it should).
• Close without unlink: If a process calls close() but not unlink(), the name remains. Another process should still attach to that name, and the memory stays until someone unlinks. We test that scenario to ensure close alone does nothing catastrophic.
• Unlink without close: If the parent calls unlink() but does not call close(), and then immediately exits, POSIX should still hold memory open until child closes (name already gone). We must verify child can still read.
• Multiple children: If two children attach and the parent unlinks, both should see data until each closes. Then memory should disappear only after the second child closes. We simulate multiple attachments in one process or two children processes. This boundary tests proper reference counting.
• Already-unlinked name: If we attempt SharedMemory(name, create=False) after unlink() but the memory still exists (because handles open), Python should not find it (since the name is removed). The actual shm segment still lives, but without a name. This surprises many. We verify that behavior and record it (POSIX doc says new shm_open fails).
• Large or zero size: A trivial case: size=0 (maybe illegal) or very large (fragmentation). These are unlikely to affect lifetime, but we test size=1, size=0 (error), and a big size to ensure no overflow or instant swap.
• Windows (if applicable): We note that on Windows, unlink() is a no-op, so our tests should expect that the name remains until the last handle is closed. A child should still attach after parent unlink(), since Windows defers deletion entirely. We verify on Windows that the memory disappears only after close() calls.
Each boundary case has its own “expected” result in the ledger (often simply "exception" for negative tests, or "data remains"). For example, trying to unlink() twice should always raise an exception by the documented contract. We make sure to assert that in code:
try:
shm.unlink()
except FileNotFoundError:
print("EXPECTED: second unlink fails")
If any boundary test unexpectedly passes (e.g. second unlink does nothing), we record that as FAIL.
Capture the per-case evidence ledger
We now gather all observations into a structured ledger. For each case from our manifest, we record: the actual operations performed, the child’s observed output or exception, resource-tracker logs, and final cleanup status. The following illustrative ledger is EXPECTED / NOT EXECUTED:
Case | Operation Sequence | Illustrative output / exceptions | Expected Outcome | Example verdict |
1 | Parent create → spawn child → parent unlink() → child read | Parent: "unlinked"; Child: "ABCD"; Exit code 0 | Child reads "ABCD" | PASS |
2 | Same with track=False | Parent: "unlinked"; Child: "ABCD"; Exit 0 | "ABCD" | PASS |
3 | Parent create → unlink() → spawn child | Parent: "unlinked"; Child: FileNotFoundError | Fail to attach | PASS (expected fail) |
4 | Parent create → spawn child → parent unlink() twice | Parent: "unlinked"; second unlink -> FileNotFoundError | Second unlink fails | PASS |
5 | Parent create → spawn two children → parent unlink() | Children both read "ABCD"; name gone after first unlink | All good until children close | PASS |
6 | Parent create → spawn child → parent is killed (no unlink) | Child prints "ABCD"; parent crash; Possible warning | Leak warning or manual cleanup | (see notes) |
Each example row is EXPECTED / NOT EXECUTED. After running, we would replace the illustrative outputs with observed results and compare them with the manifest to assign each verdict. In cases with expected failure (like Case 3’s attach), we mark “PASS” if the expected exception occurred. We preserve any failure artifacts noted (e.g. warning text or leftover files) in the ledger’s notes. For instance, if a leaked segment remained in /dev/shm, we record the name and maybe use ls to confirm it. We do not delete it yet; that’s part of recovery.
At the end of all runs, every row shows what happened. We then verify: did observed = expected on all? Any mismatch (such as Case 6 not warning) becomes fodder for our decision gate. We refuse to “sweep under the rug” missing logs or silent passes: the ledger is the indisputable evidence set for decision-making.
Reject incomplete evidence
A key discipline: if any case lacks its data, we do not mark the test “complete”. For example, if Case 6’s parent kill left no warning on stdout and our script didn’t check /dev/shm, we cannot say success. We would label that row as incomplete evidence and treat it as HOLD. We enforce that each required field is either observed or unambiguously recorded as absence. Cases without evidence are not considered passing.
If our harness fails to produce a child output, we mark it in the ledger and trigger a RETRY with instrumentation. Only with a fully populated ledger do we feel confident in the results. This prevents us from ignoring possible resource leaks or silent memory persistence.
Reconcile expected and observed identities
Once the evidence is collected, we reconcile it against our expectations. For each row, we compare the observed behavior to the manifest. For example, Case 1’s child output must exactly equal the bytes we wrote (b'ABCD'). If “ABCD” appears, we mark the manifest as verified. If not, we investigate. In the multi-process world, we also track process identities to ensure the right actors did the work. Our fixture prints the Python PID in logs, so we know which process printed which message.
As a simple check, we tag each line with “[PID=1234]” markers. In reconciliation, we confirm that the parent’s PID matches the expected creator, and the child’s PID was different. For example:
• Expected: only the parent PID should call unlink(). We grep the logs: did “unlink” appear with a single PID? If not, identity mismatch.
• Expected: child’s print of “ABCD” came after “parent unlinked”. We verify timestamp ordering if needed (or event sync). Out-of-order logs would be flagged.
We cite Refonte’s approach: just as the Ansible check-mode task-coverage guide tracked which Ansible tasks ran vs skipped, we track which process produced each piece of evidence. The manifest (expected identities: parent vs child roles) must match the recorded PIDs. Any discrepancy (e.g. the parent exited early and child ended up doing unlink()) would indicate a test-harness or code error. We resolve those by rerunning the fixture or adjusting the harness logic. Only when every log entry can be attributed correctly do we proceed.
This reconciliation ensures our independent oracle (manifest) aligns with reality. If a manifest row says “parent unlinked, child read X”, we see both messages. If the names or values diverge, we know exactly which proposition failed. We do not tolerate “free-form log review”; each check is yes/no. For instance, if the child’s output was b'ABCD' but we expected b'ABCE' (hypothetical), we would say FAIL.
Separate documented guarantees from observations
Finally, we contrast what the documentation promises versus what we observed. The Python docs guarantee that unlinking the shared memory should be safe in any order. POSIX promises name removal only after unlink, with content gone only at final close. Our evidence may reveal a gap: for example, suppose we saw that in Case 1 the child did not read "ABCD" but instead got zeros. That would contradict POSIX and indicate a bug.
When doc and behavior disagree, we clearly annotate it. For example, earlier Python issues showed that the resource-tracker code “destroys shared memory segments when other processes should still have valid access” (bpo-38119). If our tests confirm that bug, we list it: “Documentation says survive until last close, but observed that child got an error; likely a tracker bug.” We do not claim the docs were wrong without evidence, but we do note the discrepancy.
We also separate guarantees from observations. E.g. the Python doc says “unlink() should be called only once”. If our test tried twice, we verify that the second call indeed raised an error. If it did, the documentation and runtime agree. If it silently passed (unlikely, but hypothetically), we mark that as an implementation bug, not a doc fix.
By keeping contracts (Doc) and facts (Test) distinct, we avoid confusion. For example, Python’s doc states that the resource tracker automatically cleans up on process exit. If our evidence shows it cleaned up too soon, we note that as an issue with the tracker, not with the fundamental shared-memory API. We cite the docs verbatim as one column of evidence, and our observations as the other.
Bound the repair scope
Given any failures, we plan a bounded fix. For instance, if track=True caused premature unlink, the minimum change is: disable tracking for independent processes. We keep track enabled for multiprocessing-parented cases but set track=False in our own child if spawned. This is a small-scope workaround: it avoids rewriting CPython core. We document this scope: the fix affects only the test fixture (or application) configuration, not library code.
Alternatively, if a code bug were found, we might propose adjusting the resource_tracker (as in bpo-38119). But that’s outside our codebase. We explicitly say the fix is limited: e.g. “monkey-patch resource_tracker.unregister as per the reported resource-tracker workaround to ignore shared_memory, leaving other tracking intact.” We do not claim to overhaul CPython itself. The aim of this bounded repair is to fix only what’s necessary to meet the contract.
A quick example repair scope: force track=False when launching subprocesses. This means dropping one documented feature (the extra tracker) but preserves correctness. That’s a scoped repair: test and application code change, no OS or CPython build change. We give an example code snippet:
shm = shared_memory.SharedMemory(create=True, size=1024, track=False)
# Now manually schedule unlink after all children complete.
By bounding the change to our fixture or app logic, we ensure minimal side-effects.
Implement a bounded repair
Following the identified scope, we modify the fixture accordingly. For instance, if we saw multiple trackers, we simply disable it:
# In parent and child processes:
shm = shared_memory.SharedMemory(create=True/False, size=1024, track=False)
Now the resource-tracker won’t auto-unlink. We then add our own cleanup: parent explicitly unlinks once children are done. Or we may use a try/finally to always call unlink() in the parent.
If the problem was a missing close(), the repair is adding those calls. If an exception was unhandled, we catch it and convert to evidence (e.g. catch and log FileNotFoundError).
Importantly, we only change what failed. For example, disable tracking only when we detect separate processes. This targeted fix is in line with acceptance criteria #4 (do not assume tracked deletion if not in a multiprocessing tree).
We then rerun the affected cases to verify they now pass under the repaired logic. For example, after setting track=False, we expect Case 6 to no longer show the leak warning (assuming that was the cause). We document the exact code changes (with context) and mark the patch version. All new outputs are again EXPECTED / NOT EXECUTED until verified.
Test rollback and rerun safety
We ensure that failures or mid-run exits do not leave residual state. For example, if a child crashes, the fixture should remove any created shared memory. We add exception handling to do a cleanup (shm.unlink() if still present) before exiting. We simulate a rollback: run the script twice in a row; the second run should not see leftover segments from the first. We assert this by using unique temporary names (via name=None) or by cleaning /dev/shm in teardown.
A specific test: if Case 3 (attach after unlink) leaves the parent’s segment dangling (since parent didn’t explicitly unlink before exiting), our teardown should catch it. We add at the top of the script:
# Preemptive cleanup: remove any /dev/shm/* matching our name pattern
or use atexit handlers. After implementing the cleanup, re-running the entire suite should yield identical results as the first run. If the second run sees a different output (e.g. attach now succeeds because the old name lingered), we have a RERUN scenario. We label such anomalies as HOLD until cleaned.
We verify that the fixture can be aborted mid-way (e.g. SIGINT) without corrupting the system. For instance, Ctrl-C at any point should still call our finally: block and unlink(). We test a manual break and observe that no orphaned memory remains. This robustness check is the “rollback safety”: partial runs should be recoverable. We record the procedure and use a script to verify no /dev/shm/psm_* files remain.
Assign operational ownership
Finally, we clarify who must do what cleanup in a real system. For our fixture, the parent process (the test harness) is the obvious owner of the segment name (it created and unlinked it). The child only has a borrow of the descriptor. Thus, operationally, we ensure only the parent attempts unlink(). The child only calls close() on its handle. We document: “Parent is responsible for unlink(), children should not call unlink() on their copy of the segment. Children only call close().” This assignment prevents confusion (e.g., two processes both calling unlink()). If we had multiple children, we still designate the parent as the unique manager of lifecycle. This clear responsibility map is an “operational ownership” decision: no two processes vie for deletion.
We mark this in code by comments and by structuring the script so only the parent process executes the unlink(). If the child accidentally tried unlink(), it would get a NOT FOUND error (because name already removed), but we avoid that scenario altogether. This discipline ensures deterministic behavior and ties each action to an owner.
Decide ACCEPT, REPAIR, RERUN or HOLD
Based on our evidence ledger and comparison to the manifest, we make the final release decision:
• ACCEPT: All tested cases passed their expected outcomes. The evidence shows compliance with the documented contract (no unexplained behavior). For instance, if CPython 3.15.2 exhibits exactly the intended behavior in every scenario, and our repair (if any) is minimal, then “ACCEPT” this release for use. We would note “ACCEPT: shared memory behaves per POSIX, resource trackers did not violate contract after fix.”
• REPAIR: If a test case fails the documented contract but a simple code workaround or patch is identified, we mark “REPAIR.” For example, if track=True was causing premature unlink, our recommended fix of disabling tracking is a repair. We would accept the fix, not the original behavior. The decision would read: “REPAIR: adjust test fixture/application to track=False; without this change, behavior violates POSIX.”
• RERUN: If an environment mismatch or partial data is found, we RERUN. For example, if tests were executed on Python 3.15.1 but documentation cited 3.15.2, we label RERUN after aligning versions. Or if intermediate results are incomplete, we RERUN after correcting the issue. RERUN does not imply failure of the code, just that we must repeat under correct conditions.
• HOLD: If evidence is missing or contradictory and we cannot confidently accept or repair, we HOLD. For example, if Case 6’s outcome is inconsistent (sometimes warn, sometimes not) or if we cannot reproduce a resource leak, we hold and require manual investigation. “HOLD: intermittent leak of shared memory on subprocess exit requires deeper analysis.”
Each decision is justified by the ledger. We do not say “PASSED” without listing which tests underwrote that. For example, if all cases except one negative control passed, we would not ACCEPT; we might choose REPAIR if that negative case was due to bad cleanup, or HOLD if unresolved. We ensure the decision references the specific evidence lines (e.g. “Case 3 passed as expected; Case 4 confirmed double unlink error, see ledger”).
Connect the practice to Refonte Learning
This systematic, evidence-based validation exemplifies the engineering discipline fostered by Refonte Learning’s System Administration program. The program (6 months, 10–12 hours/week) covers exactly this kind of rigorous testing of system interfaces and cleanup procedures. Key topics like operating-system resource management, lifecycle of system objects, and failure recovery are taught through concrete labs and projects. For instance, the curriculum includes “System Installation and Configuration, Networking, Virtualization and Containers, Backup and Disaster Recovery”, all of which relate to understanding how OS-level resources (like shared memory segments) are allocated and reclaimed. By following a validation playbook, a student practices verifying real-world behavior against written contracts, rather than guessing based on output.
If this rigorous approach interests you, consider Refonte Learning’s System Administration program. It prepares you to design and audit systems with confidence, ensuring operational correctness in scenarios exactly like this shared-memory case. The curriculum includes hands-on labs in Linux and Windows administration, where tracking resource lifetimes is a fundamental skill. Enrolling in this program can give you not only the theory but also the mentorship and internship opportunities to apply practices such as controlled fixtures, independent oracles, and acceptance tests to your own projects.
For a similar evidence-driven approach to asynchronous test assertions, see Refonte’s AsyncMock await-completion validation guide.
