Skip to content

fix(mlx_tf): refuse mis-shaped request bodies instead of panicking - #6510

Merged
ashhart merged 3 commits into
ashhart:mainfrom
joshuaswarren:w95/mlxtf-request-shape
Dec 5, 2026
Merged

ashhart merged 3 commits into
ashhart:mainfrom
joshuaswarren:w95/mlxtf-request-shape

Conversation

@joshuaswarren

Copy link
Copy Markdown
Contributor

Fixes #6509

Sibling of #6508, on the remote-request boundary rather than the local config.json boundary. Both HTTP handlers in engines/mlx_tf dereference parsed request JSON unchecked — where vulkan_tf's request layer (server_request.zig + its malformed-request tests) already returns 400 invalid_request_error, and where server_qwen4.zig already gates its own route the same way.

Panic classes verified on the merge base

Repro executing the handlers' verbatim dereference lines — 17 mis-shaped bodies, all panic on 93f187b13:

  • parsed.value.object on a non-object body (server_completion.zig:25, server_stream.zig:21)
  • msgs_val.array.items on "messages":"x"; item.object on [42] (server_completion.zig:70–134)
  • mo.get("role").?.string on missing/non-string roles (server_completion.zig:95/134)
  • prompt.?.string-style access on "prompt":7 (server_completion.zig:201)
  • @intCast(getJsonUsize(obj,"top_k",…)) on 5000000000 (server_completion.zig:61)
  • streams_val.array, per-stream item.object, idv.string, dm.array, mi.object, dp.string, sm.array, sp.string (server_stream.zig:29–105)

What changes

  • New pure module src/server_request_shape.zig — no engine imports, host-testable. checkCompletion(obj, is_chat) and checkStreamParallel(obj) validate only the invariants the consumers dereference; getJsonU32 clamps u32-wider integers (vulkan parity).
  • Both handlers: parse failure → 400 invalid JSON body; non-object body → 400 JSON body must be an object; gate refusal → 400 <reason> via the existing sendError.
  • top_k reads switch to getJsonU32. seed stays @intCast(getJsonUsize) — its field is u64, so the cast cannot overflow.
  • Presence errors stay where they were (missing messages/prompt/streams still gets the handler's own 400); tolerated shapes stay tolerated (free-form content, optional fields absent).

Tests

New test-request-shape-host step (wired into zig build test), 5 tests / ~45 cases — every refusal string plus the tolerance cases. 5/5 pass on this head; on the base the same bodies panic, per the repro above.

Notes: build.zig also carries the test-filter option hoist — zig build panics option declared twice on zig 0.17 without it (same prerequisite hunk as #6505/#6508, disclosed there too). The server files sit on the Metal-bound engine graph, so host-side verification is ast-check plus the pure-module tests.

Generated with Devin

@ashhart

ashhart commented Dec 5, 2026

Copy link
Copy Markdown
Owner

READY to land on this head. mlx_tf's completion and stream handlers now answer a mis-shaped request body with 400 invalid_request_error instead of panicking on a union-field access. Well-formed requests take the same path as before, so tokens are unchanged; the new request-shape host test covers each refused shape.

  • Synthena

@joshuaswarren

Copy link
Copy Markdown
Contributor Author

Rebased onto main after #6505 landed (shared build.zig hoist + test-step hunks). test-request-shape-host and test-config-parse-host both green host-side. New head ba54ab25c.

@ashhart ashhart added the synthena:ready Reviewed by Synthena and ready for group train label Dec 5, 2026
@ashhart

ashhart commented Dec 5, 2026

Copy link
Copy Markdown
Owner

READY to land on this head. Same change as the head we signed off, rebased onto today's main: the only difference is the test-filter option, which main now declares once itself. Host-only; the request-shape tests and config-parse tests are wired into the test step.

  • Synthena

ashhart and others added 3 commits December 5, 2026 23:40
Both HTTP handlers dereferenced parsed JSON unchecked — a non-object
body, a non-array messages/streams field, a non-object item, a missing
or non-string role/id, or a top_k wider than u32 all panicked the
server where vulkan_tf's request layer returns 400s. server_qwen4.zig
already gates its own route the same way; this adds the equivalent
shape gate (server_request_shape.zig, pure and host-testable) in front
of the completion and stream-parallel paths, keeps presence errors in
the handlers, and clamps top_k to u32.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@ashhart
ashhart force-pushed the w95/mlxtf-request-shape branch from ba54ab2 to 6cf542d Compare December 5, 2026 23:41
@ashhart
ashhart merged commit 88c133d into ashhart:main Dec 5, 2026
7 of 11 checks passed
@ashhart

ashhart commented Dec 5, 2026

Copy link
Copy Markdown
Owner

Thank you @joshuaswarren. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

ashhart added a commit to Nipale-ai/TensorFold that referenced this pull request Dec 5, 2026
ashhart added a commit that referenced this pull request Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

synthena:ready Reviewed by Synthena and ready for group train

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mlx_tf serve: malformed request JSON panics the engine instead of returning 400

2 participants