Skip to content

serve: fix test helpers that miss the Cargo binary, leak children and corrupt UTF-8 - #714

Merged
juliankang4 merged 3 commits into
midagedev:mainfrom
juliankang4:fix-serve-test-helpers
Oct 23, 2026
Merged

juliankang4 merged 3 commits into
midagedev:mainfrom
juliankang4:fix-serve-test-helpers

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

serve 시험 도우미의 세 가지 결함(Cargo 대상 경로 무시, 시작 실패 시 자식 프로세스 방치, SSE UTF-8 깨짐)을 고치고 각각 회귀 시험을 추가합니다.

Part of #565. Tracked in #699.

What and why

Three helpers in the bloomery-serve tests can mislead a run.

  • The API-key process test (a_key_never_reaches_what_the_process_prints_or_serves) started the server from a fixed target/debug/bloomery-serve path. The binary is not there whenever CARGO_TARGET_DIR is set, so the test stops on a missing file. It came in with 98f23be.
  • When a test server cannot start (no usable startup line, or an address that does not parse), the child process was not owned yet and was never killed or reaped. Its guard was only created after the address was read. This came in with 98f23be and 4afdd65.
  • The slot-test stream reader turned every read into a string with from_utf8_lossy, so a multi-byte character split across two reads became replacement characters in the observed chunk. It came in with 3c5a133.

This PR fixes all three, and one case of the same kind: the reader also accepted a stream that ended inside the HTTP head, inside an SSE frame, or with invalid UTF-8.

  • The API-key test moves into the serve integration tests and starts the server with CARGO_BIN_EXE_bloomery-serve. BinServe and spawn_bin in src/testserve.rs are removed, and so is the old test in src/api.rs. The test keeps its name and its assertions. It now also checks the full response head, not only the body, for the server key and the key the client sent, so Reply keeps the raw head.
  • spawn_mock takes a new spawn_command helper that wraps the child in the existing Reaped guard right after the spawn, so a panic while reading the startup line or parsing the address kills and reaps the child.
  • read_stream keeps raw bytes and checks UTF-8 only on a complete SSE frame. It now panics by name when the stream closes before the HTTP head ends, in the middle of a frame, or on invalid UTF-8. Looking at the borrowed frame also removes one string copy per non-empty frame.

The change is test code only. There is no new option, dependency, public API, GPU path or unsafe code.

Release notes: internal: the serve tests use the Cargo binary path, reap children after a bad start and keep split UTF-8 intact.

Code shape

  • No second copy. spawn_command, read_chunks and call_headers are extracted from or extend the existing test helpers, and the duplicate BinServe and spawn_bin are removed.
  • Model code holds only architecture facts. No model code changes.
  • Dead code is gone. BinServe, spawn_bin and the in-crate copy of the API-key test are removed.
  • No silent failure. A closed stream, a cut frame and invalid UTF-8 are named panics instead of defined-looking output.
  • Comments state what is true now. The new comments describe the current contract only.
  • No relaxed gate. No band, tolerance or lint moved. The API-key assertions are kept and widened to the response head.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up. rustfmt on the changed files and git diff --check are clean, and the duplicate scan found no candidate. Clippy has not been run on this head.

Duplication found or removed (path:line), or "none": removed the BinServe and spawn_bin copy in crates/serve/src/testserve.rs, which duplicated spawn_mock in crates/serve/tests/serve.rs.

What I ran

All commands ran from the workspace root with CARGO_BUILD_JOBS=4, RUSTFLAGS='-C target-cpu=native' and CARGO_TARGET_DIR="$PWD/target/servetests". The tests use no GPU and no model.

Environment: Linux x86_64 CPU, rustc 1.100.0-nightly (e457a7b0d 2026-08-27).

FAIL-first: I ran the new tests on the old helper behavior (the same test additions, with the old code and a mechanical extraction so they compile), then on the fix. Log excerpt from the old behavior, one block per defect:

thread 'api::tests::a_key_never_reaches_what_the_process_prints_or_serves' (1022230) panicked at crates/serve/src/testserve.rs:147:29:
FAIL-FIRST cargo-target raw_rc=101
startup left unreaped children: ["bad-address", "closed-stderr"]
FAIL-FIRST startup-cleanup raw_rc=101
assertion `left == right` failed: split at 63
FAIL-FIRST stream-chunks raw_rc=101

The split case shows 가é🦀 read back with its first character replaced by replacement characters. The stream-chunks run also reports the three malformed inputs (a cut head, a cut frame and invalid UTF-8) as accepted.

On the fix, the four tests run together and the older tests that use the same helpers run with --ignored:

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 101 filtered out; finished in 0.01s
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 102 filtered out; finished in 2.01s

The older tests are hw_engine_error_is_500_then_503_then_exit, hw_props_engine_object and slots::hw_drafted_passes_interleave_across_slots.

The last commit adds the response-head check to the API-key test and changes only test data and assertions in common. I ran only that test on it, on a Linux ARM CPU machine with its own CARGO_TARGET_DIR, which also checks that the server binary sits in that directory:

cargo test --locked -p bloomery-serve --test serve a_key_never_reaches_what_the_process_prints_or_serves -- --nocapture
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 104 filtered out; finished in 0.01s
PASS full response head and body contain no API key under custom CARGO_TARGET_DIR

The process guard, the stream reader and the two other regression tests are unchanged since the run above, so I did not repeat that run on the last commit.

Not run: clippy, t0 and the full workspace test suite. No kernel, launch or product path changed, so I took no timing.

Reproduction

export CARGO_BUILD_JOBS=4 RUSTFLAGS='-C target-cpu=native' CARGO_TARGET_DIR="$PWD/target/servetests"
cargo test --locked -p bloomery-serve -- --nocapture --exact a_key_never_reaches_what_the_process_prints_or_serves a_failed_startup_reaps_its_child slots::stream_chunks_preserve_utf8 slots::stream_chunks_refuse_invalid_or_incomplete_frames
cargo test --locked -p bloomery-serve --test serve -- --ignored --nocapture --exact hw_engine_error_is_500_then_503_then_exit hw_props_engine_object slots::hw_drafted_passes_interleave_across_slots

On the old helpers the first command stops on the missing binary, the unreaped children, the split at byte 63 and the three accepted inputs. On the fix the first command runs four tests and the second runs three.

What I ran into

This does not block the PR. a_failed_startup_reaps_its_child checks /proc/<pid>, so it is compiled only on Linux.

AI help

AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: CLEAN: The changed helpers retain child ownership during startup failures and preserve SSE bytes across reads.

Checked

Duplicates: 9 introduced or relocated functions/constants checked; no new duplicate owner. Dryer reported 0 pairs.

Complexity: crapper flagged no changed function. The extracted helpers retain their existing control flow.

Silent failure: 5 parsing/default sites followed. An absent request body means empty input; bad status or startup parsing and missing required stderr lines fail the test. Raw response heads remain available to the key assertions. Truncated heads, cut frames and invalid SSE UTF-8 panic by name.

Tests: static mutation review of all four added or moved tests. Replacing the Cargo binary path at serve.rs:1261 with the old fixed path fails startup under a custom target directory. Removing argv redaction at api.rs:2061 fails the moved key assertion. Delaying guard creation at serve.rs:1275 until after parsing fails the child-reaping assertion. Restoring per-read lossy decoding at slots/mod.rs:702 fails content equality. Removing the EOF assertions at slots/mod.rs:724, or using lossy frame decoding at line 711, fails the malformed-stream assertion.

Gates: no threshold or tolerance moved; the relocated key test retains its assertions and adds response-head coverage.

Escape catalog: C1–C4 do not match. The cleanup test inspects its recorded child PID.

Adversarial checks: startup failure before address parsing, splits inside UTF-8 and delimiters, malformed EOF, and several frames in one read are covered. The stream consumer also requires both final chunks. No engine hot path changes.

Notes

Builds and tests were not run in this read-only review. The PR includes CPU FAIL-first and passing-run excerpts; it contains no GPU gate evidence.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

@juliankang4

Copy link
Copy Markdown
Collaborator Author

The review is done and the contribution check passed, so I'm merging this.

@juliankang4
juliankang4 merged commit 10ce0fc into midagedev:main Oct 23, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

serve The HTTP server: crates/serve

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant