Skip to content

fix(server): a request keeps its own copy of its key label - #560

Merged
ashhart merged 1 commit into
ashhart:mainfrom
chaog992:auth-key-label-reload
Oct 10, 2026
Merged

ashhart merged 1 commit into
ashhart:mainfrom
chaog992:auth-key-label-reload

Conversation

@chaog992

@chaog992 chaog992 commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

What this changes

Store.match in zig/src/server/auth.zig returned a key file entry's label as a slice into the store's file_arena. The request keeps that slice in conn.key_label and reads it again, outside the store's mutex, when its response starts: for the access line (log.request) and the /metrics request count (countReply), both from startResponse, and for sendError's log line. For a reply that is not streamed, that is after the route has run. In between, any other request, /health included, calls match, and refresh can free that arena. refresh rereads the key file when it changed (an edit or an atomic rename, noticed at most once a second) and, after SIGHUP, at the next match whether or not it changed. A successful reread frees the old arena (auth.zig:130), and a refused one (the file removed, or made readable by others) frees it as well (auth.zig:157). The request that was authenticated by a file key then reads freed memory when it answers.

match now copies the label into the request's arena while it holds the mutex, so the label lives as long as the request. Labels from --api-key and TENSORFOLD_API_KEY are copied the same way, a few bytes per request. If the copy cannot be allocated, the request counts as carrying no key, so a gated route answers 401. The copy is never read after its arena is reset: that happens when the connection goes back to read its next request (server.zig:226), and readRequest then clears key_label first, before any reply that could log it (http_conn.zig:168). The metrics table keeps its own copy of the key (metrics.zig:79).

Receipt

  • Environment: base 78ee0f42, fix 2193d145, Zig 0.17.0, Linux x86_64 under WSL2. No GPU and no model are needed.
  • Tests: bash zig/tests/server/zig_test.sh (the server module, then the engine seam) passes 74 and 28 tests with this change; the base passes 73 and 28. zig build test passes 17/17 steps and 78/78 tests; on Linux it does not include the server module.
  • Failing receipt: the new test on the unfixed match:
    20/74 auth.test.a request's key label outlives a reload of the key file...Segmentation fault at address 0x7fa984503019
    zig/src/server/auth.zig:329:39: … in test.a request's key label outlives a reload of the key file (root.zig)
        try std.testing.expectEqualStrings("ops", label);
    
    The test's second half covers a refused reload (the file set to 0644). With the first assertion replaced by _ = label;, it fails on its own on the base, also with a segmentation fault, at line 334 (expectEqualStrings("ci", kept)). A probe that is not part of this change goes through the access line's own reader: it keeps the label on a Conn, sets reload_requested as SIGHUP does with the file unchanged, lets a request without a key call match, and reads the label back with log.keySuffix. It passes with the fix; with only the copy reverted it segfaults in label_suffix (log.zig:84).
  • Lean check: python3 tools/zig/lean_check.py reports 786 files and 510 problems on the base, and the same list with this change.
  • Format and cross-compilation: zig fmt --check build.zig zig, the check CI runs, passes. The server tests compile for aarch64-macos with -mcpu apple_m1 --test-no-exec; this is a compile check, not a macOS test run.
  • With Fix server cleanup leaks and Metal build warnings #513: it also edits auth.zig. The two merge without conflicts, and the server tests pass on the merge (77 and 28).
  • Not run: a served model on Metal or CUDA, and an end-to-end server run with the key file changing during a request; the receipt is the unit test. The change touches no inference path.

@ashhart
ashhart merged commit 17a75f5 into ashhart:main Oct 10, 2026
2 checks passed
@ashhart

ashhart commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Thank you @chaog992. Merged into main (17a75f5): a request keeps its own copy of its key label, so a key-file reload while it runs can no longer free the label under it. Your test covers both the reload and the refused reload. It ships in the next release.

@chaog992
chaog992 deleted the auth-key-label-reload branch October 10, 2026 01:09
@ashhart ashhart mentioned this pull request Oct 10, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
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.

2 participants