szn

Bug 530 — timing-dependent status-cache test

530. A test asserts pointer identity across an operation the wall clock can legitimately invalidate

Severity: MEDIUM Status: ✅ FIXED Files: src/server/server.zig:3320-3323, src/server/server.zig:5893

Problem

The test written for #425 verifies that a second render with status_dirty == false reuses the cached status line rather than reallocating it, by comparing the buffer pointer:

server.renderToDisplayClient();
const ptr1 = server.display_clients.items[0].status_line.?.ptr;

server.dirty = true;
server.status_dirty = false;
server.renderToDisplayClient();

const ptr2 = server.display_clients.items[0].status_line.?.ptr;
try testing.expectEqual(ptr1, ptr2);

But the cache is keyed on the wall-clock second as well:

// src/server/server.zig:3320-3323
const current_sec = time(null);
...
const use_cache = !self.status_dirty and dc.status_line_width == dc.sx
    and dc.status_line != null and dc.status_line_copy_mode == pane_in_copy_mode
    and dc.status_line_time == current_sec;

So if the clock ticks between the two renders, use_cache is false, the line is rebuilt, and the pointer changes. The test then fails with a pointer mismatch:

error: 'render caches status line across frames without reallocation — bug #425' failed:
       expected u8@127500080, found u8@127500180

This is inherent to the behaviour, which is correct: the status bar can show a clock, so it must be rebuilt when the second changes. The defect is in the test, which asserts a property that only holds within a single second and does nothing to guarantee one.

Why it is not seed-reproducible

The test runner randomises execution order, but this failure is driven by elapsed time, not ordering. Running the suite repeatedly with the same seed that produced a failure passes on the next attempt, which is what identified the cause: the two renders are microseconds apart, so a second boundary lands between them only occasionally.

Fix

A test-only pin for the clock, following the existing test_after_command_dispatch_hook precedent for test hooks on Server:

// src/server/server.zig — Server
/// Test-only pin for the wall-clock second used as part of the status-line
/// cache key. The cache is invalidated whenever the second changes, so a test
/// asserting pointer reuse must be able to stop the clock — otherwise it fails
/// intermittently whenever a render straddles a second boundary.
test_clock_sec: ?i64 = null,
// src/server/server.zig — renderToDisplayClient
const current_sec: i64 = self.test_clock_sec orelse @intCast(time(null));

The test now sets server.test_clock_sec = 1_000_000 before the first render, so both renders are in the same second and the pointer-reuse assertion is deterministic. Production behaviour is unchanged: test_clock_sec is null, so the real clock is used.

Verification

Note: an attempted stronger assertion did not work

The first attempt at this fix also asserted the other half of the contract — a new second must invalidate the cache — by rendering a third time with test_clock_sec = 1_000_001 and checking status_line_time had advanced. That failed (expected 1000001, found 1000000) and also pushed the 0.16 suite over its shared DebugAllocator budget (a Grid.init OOM in an unrelated test), because the extra render changes the suite’s allocation profile.

Both symptoms are consistent with buildStatusLine failing on the third render under the exhausted budget, leaving status_line_time untouched — but that was not confirmed, so the assertion was dropped rather than kept on a guess. The shipped fix is the minimal one: pin the clock, keep the original two renders. Anyone extending this should note that this test is allocation-sensitive — the suite runs on one shared testing allocator and is close to its ceiling.

#425 is the fix that introduced the second-keyed cache; #309 is the original status-line caching work. The DebugAllocator budget fragility that the first attempt tripped is described in src/test.zig’s header comment.