Skip to content

cache: create the cache package auto-generating secretKey if not found - #2

Merged
kalbasit merged 6 commits into
mainfrom
cache-package
Nov 30, 2024
Merged

kalbasit merged 6 commits into
mainfrom
cache-package

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

Create the cache package, and have auto-load or auto-generate the secret key if it does not exist.

@kalbasit
kalbasit merged commit 8174116 into main Nov 30, 2024
@kalbasit
kalbasit deleted the cache-package branch November 30, 2024 21:49
kalbasit added a commit that referenced this pull request Dec 1, 2024
* main:
  cache: create the cache package auto-generating secretKey if not found (#2)
kalbasit added a commit that referenced this pull request May 24, 2026
## Summary

Three pre-existing `pkg/cache` flakes from #1247 share one root cause:
assertions that race against absolute wall-clock time and break under CI
load even when the production code is correct. Surfaced repeatedly
during the lean-flake-check measurement runs in #1249 (which sits
stacked on top of this branch).

All three fixes are **test-only**. No production code is touched.

## What changed

| Flake (from #1247) | Test | Before | After |
| --- | --- | --- | --- |
| #5 |
`TestCDCBackends/{SQLite,MySQL}/first_pull_completes_before_CDC_chunking_finishes`
| `assert.Less(elapsed, 500ms)` racing the slow chunker | Happens-before
check: `select { case <-chunkingStarted: t.Fatal(…) }` after
`io.ReadAll`. `slowChunker` grew an optional `started chan struct{}`
closed when its delay expires. Delay bumped 500 ms → 10 s (only matters
under regression). |
| #1 | `TestCacheBackends/MySQL/PutNarInfoDeadlock` |
`context.WithTimeout(2s)` then `assert.NoError`. Could not distinguish
"deadlock" from "MariaDB slow." | Timeout bumped 2 s → 60 s. A real
deadlock still fails fast; non-deadlocked slow path now passes. |
| #2 | Six instances of `assert.WithinDuration(CreatedAt,
LastAccessedAt, 2s)` across `TestCacheBackends` | 2 s tolerance was
tight enough that two CLOSE-TO-NOW() timestamps from different SQL
operations under MariaDB load could fall outside it (observed -3 s
spread). | Tolerance bumped 2 s → 30 s. Still tight enough to catch a
genuinely broken "set both timestamps at row creation" semantic. |

## Why "test-only" and not "fix the production bug"

These three are *measurement bugs*: the assertions encode an assumption
("operation X completes within Y milliseconds") that's an artifact of
the dev host's speed, not a property the codebase ever promised. The
behavior the tests *want* to verify (no deadlock, timestamps set close
together at creation, GetNar doesn't block on chunking) is still being
verified — the encoding is just no longer wall-clock-dependent.

Flakes #3 and #4 from #1247 are NOT addressed here because they are not
the same shape:

- **#3** (`ConcurrentDownloadCancelOneClientOthersContinue`, 20-min
hang) is a real hang in `io.ReadAll(readerB)` or `wg.Wait()` after all
the existing 5–10 s guards have passed — almost certainly a
production-code bug in the shared-reader-fanout path. Filed as **#1252**
for separate investigation.
- **#4** (`GetNarInfoConcurrentPutNarInfoDuringMigration`) exercises the
same concurrent-migration-vs-PutNarInfo window that the next test
(`testGetNarInfoMultipleConcurrentPutsDuringMigration`) explicitly
`t.Skip`s with `"blocked on pgx transaction-abort cascade in PutNarInfo
re-fetch path"`. That tracked production bug is the actual root cause;
this test exercising it less reliably isn't a separate issue.

## Stack

This PR is stacked under #1249 (`improve-flake-check-time`); landing
this first should unblock #1249's CI by eliminating three of the five
flakes that were causing intermittent reds.

```
main → fix-cache-test-timing-assertions (this PR, #1253)
     → improve-flake-check-time (#1249)
```

## Test plan

- [x] `nix develop -- go test -race -count=1 -run
"TestCDCBackends/SQLite/first_pull_completes_before_CDC_chunking_finishes"
./pkg/cache/` passes locally.
- [x] `nix develop -- go test -race -count=1 -run
"TestCacheBackends/SQLite/(PutNarInfoDeadlock|GetNarInfo/narinfo_exists_upstream/nar_does_exist)"
./pkg/cache/` passes locally.
- [x] `golangci-lint run` clean (run inside `nix develop` so the
toolchain resolves).

---------

Co-authored-by: Claude Opus 4.7 <[email protected]>
kalbasit added a commit that referenced this pull request Jun 10, 2026
## Summary

A **second, distinct** dev-harness bug from the chunked-body fix in
#1390 (which is necessary and stays). After #1390, `nix copy` of a large
closure (e.g. a ~9.35 GB `nixos-system`) to the `:8500` HA proxy
**still** intermittently fails with `curl error 56: Connection reset by
peer`, aborting the whole copy on the first reset.

**Root cause:** the proxy's `ProxyHandler` left `protocol_version` at
the stdlib `BaseHTTPRequestHandler` default of **HTTP/1.0**, so it
force-closed every client connection after one request. `nix copy`
pushes hundreds of objects over a small pool of HTTP/1.1 keep-alive
connections; because the proxy refused keep-alive, nix was forced to
open a brand-new TCP connection per object — thousands for a multi-GB
closure. Under that churn (amplified by the default listen backlog of 5
and a ~1 s-per-PUT stall from unanswered `Expect: 100-continue`), the
accept loop stalls and intermittently nix's upload connection is reset
**before the proxy ever handles it** (`curl error 56`).

**Reproduced & fix-validated** on a clean isolated ncps behind the real
proxy: the unpatched proxy dies at PUT #2; the patched proxy completes
**3,488 PUTs with 0 resets**. `curl -v` confirms `HTTP/1.1 200` +
"Connection left intact" (reuse).

## What changed (`dev-scripts/run.py` proxy only; ncps unchanged)

- `ProxyHandler.protocol_version = "HTTP/1.1"` — persistent, reused
client connections; also makes `BaseHTTPRequestHandler` auto-answer
`Expect: 100-continue`, removing the per-PUT stall.
- Response framing for safe reuse: preserve `Content-Length`; treat
`204`/`304`/`HEAD`/`1xx` as bodiless; send `Connection: close` when the
body length is unknown (e.g. backend chunked, stripped as hop-by-hop) so
the client reads to EOF instead of mis-framing the next response.
- Raise the listener backlog (`request_queue_size`) above the stdlib
default of 5, set before the socket is activated.
- All #1390 hardening preserved (chunked request-body forwarding, RFC
7230 §3.3.3, bounded `_fail` drain, error logging).

## Test plan

- [x] `task fmt` (0 changed), `task lint` (0 issues), `task test` (all
Go packages ok)
- [x] New keep-alive / framing / `Expect: 100-continue` regression tests
drive the real `run.py` proxy via `NCPS_RUN_PY`; confirmed RED on
HTTP/1.0, GREEN after the fix
- [x] `e2e-harness-unit` nix check green (52 passed; +4 new tests)
- [x] End-to-end: full `nix copy` of the 9.35 GB closure through the
patched proxy — 3,488 PUTs, 0 resets

---------

Co-authored-by: Claude Opus 4.8 <[email protected]>
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