Skip to content

Commit 42eb80d

Browse files
rustyconoverclaude
andcommitted
fix(fixtures): make cache fixture nonces unique across worker processes
cache_nonce, cache_scoped_txn and cache_revalidatable minted nonces from a bare per-process itertools.count() starting at 0. The extension subprocess pool keeps several worker processes per path and can serve two real invocations (cache MISSes) from two different processes, which then both return the same value, so a MISS reads as a HIT and tests such as cache/transaction_scope.test fail falsely. Fold the pid (read at mint time, so forked workers differ too) into the high 32 bits; the low bits stay a per-process count. The column stays int64, values are unchanged on a hit and change on every real miss. Mirrors vgi-go fc71b1c. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0265597 commit 42eb80d

2 files changed

Lines changed: 61 additions & 4 deletions

File tree

‎tests/test_cache_nonce.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2026 Query Farm LLC - https://query.farm
2+
3+
"""The result-cache nonce fixtures mint values unique across worker processes.
4+
5+
The client's subprocess pool may serve two cache MISSes from two different
6+
worker processes (``cache/transaction_scope.test`` asserts their nonces differ),
7+
so a bare per-process counter is not enough: the pid lives in the high 32 bits.
8+
"""
9+
10+
from __future__ import annotations
11+
12+
import multiprocessing
13+
import os
14+
15+
from vgi._test_fixtures.table.cache import _next_nonce
16+
17+
18+
def _child_nonce(_: int) -> int:
19+
return _next_nonce()
20+
21+
22+
def test_nonce_monotonic_and_carries_pid() -> None:
23+
"""Within a process nonces advance by one and carry this process's pid."""
24+
a = _next_nonce()
25+
b = _next_nonce()
26+
assert b == a + 1
27+
for n in (a, b):
28+
assert n >> 32 == os.getpid()
29+
assert -(2**63) <= n < 2**63 # fits the declared int64 column
30+
31+
32+
def test_nonces_disjoint_across_processes() -> None:
33+
"""Fresh processes (each counter at 0) never mint the parent's or each other's values."""
34+
ctx = multiprocessing.get_context("spawn")
35+
with ctx.Pool(2) as pool:
36+
children = pool.map(_child_nonce, range(2), chunksize=1)
37+
parent = _next_nonce()
38+
values = [*children, parent]
39+
assert len(set(values)) == len(values)

‎vgi/_test_fixtures/table/cache.py‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
* ``cacheable_numbers(n, ttl := 300)`` — ``n`` rows ``[0..n)``; advertises a
1111
``ttl`` freshness lifetime. The baseline cacheable generator.
1212
* ``cache_nonce()`` — ONE row whose value changes on every *real* invocation
13-
(a process-global counter). A cache HIT is provable by the value NOT
13+
(the worker pid folded over a per-process counter, so values never
14+
collide across pooled worker processes). A cache HIT is provable by the value NOT
1415
changing across calls; a MISS by it changing. Row count is fixed (1) and
1516
never depends on wall clock.
1617
* ``cache_no_store(n)`` — emits ``n`` rows but advertises
@@ -25,6 +26,7 @@
2526
from __future__ import annotations
2627

2728
import itertools
29+
import os
2830
import struct
2931
from dataclasses import dataclass
3032
from typing import Annotated, Any, ClassVar, cast
@@ -61,6 +63,22 @@
6163
_NONCE_COUNTER = itertools.count()
6264

6365

66+
def _next_nonce() -> int:
67+
"""Mint a nonce unique across every live worker process, not just this one.
68+
69+
The client's subprocess pool holds several worker processes and hands a scan
70+
whichever it pops, so two cache MISSes can land in two different processes.
71+
With a bare per-process counter both could mint the same value and a MISS
72+
would read as a HIT (``cache/transaction_scope.test``: "t2 nonce <> t1a
73+
nonce"). Folding the pid into the high 32 bits keeps the nonces of all live
74+
processes disjoint, while the low 32 bits stay a per-process count
75+
(``pid = nonce >> 32``). The pid is read at mint time, not import time, so a
76+
worker forked after import still gets its own range. Fits ``int64``: Linux
77+
and macOS pids are below 2**31.
78+
"""
79+
return (os.getpid() << 32) + next(_NONCE_COUNTER)
80+
81+
6482
# ---------------------------------------------------------------------------
6583
# cacheable_numbers
6684
# ---------------------------------------------------------------------------
@@ -187,7 +205,7 @@ class Meta:
187205
@classmethod
188206
def initial_state(cls, params: ProcessParams[CacheNonceArgs]) -> _CacheNonceState:
189207
"""Mint a fresh nonce for this (real) invocation."""
190-
return _CacheNonceState(nonce=next(_NONCE_COUNTER))
208+
return _CacheNonceState(nonce=_next_nonce())
191209

192210
@classmethod
193211
def process(
@@ -325,7 +343,7 @@ class Meta:
325343
@classmethod
326344
def initial_state(cls, params: ProcessParams[CacheScopedTxnArgs]) -> _CacheScopedTxnState:
327345
"""Create initial state; bump the nonce once per REAL invocation (MISS)."""
328-
return _CacheScopedTxnState(remaining=params.args.n, nonce=next(_NONCE_COUNTER))
346+
return _CacheScopedTxnState(remaining=params.args.n, nonce=_next_nonce())
329347

330348
@classmethod
331349
def process(
@@ -462,7 +480,7 @@ class Meta:
462480
@classmethod
463481
def initial_state(cls, params: ProcessParams[CacheRevalidatableArgs]) -> _CacheNonceState:
464482
"""Mint a fresh nonce for this (real) invocation."""
465-
return _CacheNonceState(nonce=next(_NONCE_COUNTER))
483+
return _CacheNonceState(nonce=_next_nonce())
466484

467485
@classmethod
468486
def process(

0 commit comments

Comments
 (0)