Skip to content

fix(testaws): record a request before its caller can observe the response - #216

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/harness-request-log-race
Aug 2, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/harness-request-log-race

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

The failure

CI failed on 61b54e2 (the #96 merge) with the commits either side green. One test:

--- FAIL: TestShortFileIsServedFromCache (0.06s)
    read_path_test.go:145: the first read issued no GET, so nothing below distinguishes a cache hit from a miss

That is the test's precondition, not its assertion. It reads as the FUSE read path having served
10240 bytes from a cache the fixture constructed empty two lines earlier — a cache-correctness test
reporting that the cache did something impossible.

What it actually was

internal/testaws proxies every request and logs it. The read-path suite asserts on that log —
bytes transferred and GETs issued are how read amplification and cache behaviour get measured,
because neither the AWS SDK nor the emulator reports them.

The handler appended the log entry after proxy.ServeHTTP returned. The proxy writes the
response body to the socket inside that call, so the sequence was:

  1. proxy writes 10240 bytes to the socket
  2. client reads them, read path returns, bytes.Equal passes
  3. test calls GETs("small.dat") → empty
  4. only now does the server goroutine append

The bytes compared equal because the GET did happen. It just wasn't recorded yet.

Diagnosis

By elimination first — the read returned correct bytes, and the fixture's cache was new, so the GET
must have reached the endpoint — then reproduced directly: 45–70 of 640 concurrent ranged reads
observed an empty GET log against the previous recorder.

Concurrency is required to see it. The window is between two statements on one goroutine, so a
single caller alternating request and assertion never lands in it; my first two standalone
reproductions returned 0/6000 for exactly that reason.

Fix

Publish the request on arrival, fill in its response fields once served, and have snapshot and
reset wait for anything still in flight. Waiting in snapshot means every accessor built on it
inherits the guarantee instead of each one restating it.

reset waits for the mirrored reason: clearing mid-flight leaves a request in the log after the
reset meant to precede it, so the next assertion counts traffic from the phase the test just
declared finished.

Documented on Fault that OnFire must not read the log — the triggering request is counted in
flight for the hook's duration, so a hook that read it would wait on itself. Neither existing hook
does; both only issue requests, which is fine.

Verification

Both directions, because a regression test that cannot fail proves nothing:

recorder result
previous (git show HEAD:) 59/640, 70/640, 45/640 requests missing — FAIL
this one 0 missing — pass, 5 consecutive runs
  • go test -race -count=1 ./... — 32 packages, 0 failures
  • TestShortFileIsServedFromCache × 60 under GOMAXPROCS=2 — clean
  • internal/testaws × 15 — clean
  • golangci-lint run --new-from-merge-base=origin/main — 0 issues
  • gofmt -l — empty

Why this one matters more than its size

The audit's premise is that 32,680 lines of tests missed ~45 defects because mocks agree with their
callers by construction. internal/testaws exists to remove that. A harness whose observations lag
the behavior it observes reintroduces the same problem in a new shape: the assertion becomes a coin
flip weighted by machine load, and when it loses, it blames the code under test.

…onse

Everything in internal/testaws that asserts on transfer behavior reads the
request log immediately after the operation it measures returns. That is only
sound if a completed response implies a recorded request, and it did not: the
handler appended to the log after proxy.ServeHTTP returned, while the proxy
writes the response body to the socket inside that call. A client could hold
every byte of a fully-Content-Length'd response whose request was not yet in
the log. Measured at 45-70 of 640 concurrent ranged reads.

The consequence landed in a different package. internal/fuse
TestShortFileIsServedFromCache failed on its precondition -- "the first read
issued no GET" -- which reads as the read path having served from a cache the
fixture had just constructed empty, in a test whose whole subject is cache
correctness. One CI run in seven, on 61b54e2, with the commits either side
green.

A harness whose observations lag the behavior it observes makes every assertion
built on it a coin flip weighted by machine load, and the failure blames the
code under test rather than the instrument.

Requests are now published on arrival and their response fields filled in once
served; snapshot and reset wait for anything still in flight, so every accessor
inherits the guarantee rather than each one restating it. Documented on Fault
that OnFire must not read the log -- the triggering request is counted in flight
for the hook's duration, so it would wait on itself.

Verified in both directions: the new test loses 45-70 of 640 requests against
the previous recorder and zero against this one. Full suite green, 32 packages,
-race.
@scttfrdmn
scttfrdmn merged commit 15b001a into main Aug 2, 2026
22 checks passed
@scttfrdmn
scttfrdmn deleted the fix/harness-request-log-race branch August 2, 2026 20:54
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.

1 participant