Repository navigation
feat(responses): track background response jobs in ResponsesStore - #2915
Merged
Merged
Conversation
Member
Author
Implementation Review SummaryIntent
Findings AddressedNone. No CRITICAL or HIGH findings; auto-fix was scoped to those severities. Remaining Items
Verification
|
Member
Author
Security and performance review (head 031aa48)No CRITICAL or HIGH findings, so no commits were pushed. Checked: memory bounds, lock ordering and contention, poisoning and panics, integer overflow.
Remaining, not fixed:
|
inureyes
added a commit
that referenced
this pull request
Nov 10, 2026
Add an active-job map to ResponsesStore, outside the TTL and LRU entries, so background responses have a record from creation. The API (admit_background, mark_background_in_progress, finish_background, background_snapshot, background_status, request_background_cancel, wait_background_terminal, active_background_jobs) follows the contract in the issue; finish moves a job to a stored entry in one critical section, store:false terminals are ephemeral and hidden from get(), and an oversized terminal is recorded as a failed object. Add --responses-background-max-jobs (default 64) and --responses-background-ephemeral-ttl-secs (default 600, at least 1) to mlxcel serve and mlxcel-server, wired into ResponsesStoreConfig and documented.
…s lock finish_background serialized the terminal object twice while holding the jobs mutex, so a multi-megabyte result briefly blocked every admit, poll and cancel. Measure it once before taking any lock, keep the input-item size computed at admission, and pass the precomputed entry size into the insert. Foreground insert() now also sizes its payload before taking the store lock.
inureyes
force-pushed
the
feature/issue-2897-background-response-jobs
branch
from
November 10, 2026 01:18
58f0396 to
9e1da9b
Compare
inureyes
marked this pull request as ready for review
November 10, 2026 01:18
12 tasks done
inureyes
added a commit
that referenced
this pull request
Nov 30, 2026
clap reads MLXCEL_* env defaults at parse time, and the ScopedEnv tests in src/main_tests.rs set those variables process-wide while holding CLI_ENV_LOCK. 48 tests parsed the CLI without the lock, so under the default parallel test runner they could see another test's value. On GB10, settings_cli_mlxcel_serve_defaults_off_and_explicit_flag_enables failed once because serve_responses_background_ephemeral_ttl_zero_is_refused (#2915) had MLXCEL_RESPONSES_BACKGROUND_EPHEMERAL_TTL_SECS=0 set. Every CLI parsing test now takes a shared cli_env_lock() helper, which also recovers from a poisoned lock so one failing test no longer fails the other lock holders. The 11 existing lock sites use the same helper. A new scan test, every_cli_parsing_test_holds_the_env_lock, fails when a test that parses the CLI (directly or through a helper in the file) does not take the lock. Validation: removing the lock from one test makes the scan test fail and name it; cargo test --bin mlxcel passed 10 of 10 consecutive default-parallel runs (365 passed); clippy with -D warnings and fmt are clean.
inureyes
added a commit
that referenced
this pull request
Nov 30, 2026
clap reads `MLXCEL_*` env defaults at parse time, and the `ScopedEnv` tests in `src/main_tests.rs` set those variables process-wide while holding `CLI_ENV_LOCK`. 48 tests parsed the CLI without the lock, so under the default parallel runner they could observe another test's value. During the auto-impl run for #3567 to #3569, `settings_cli_mlxcel_serve_defaults_off_and_explicit_flag_enables` failed once on GB10 with `0 is not in 1..` because `serve_responses_background_ephemeral_ttl_zero_is_refused` (added in #2915) had `MLXCEL_RESPONSES_BACKGROUND_EPHEMERAL_TTL_SECS=0` set at the same moment. ## Changes - Every CLI parsing test takes a shared `cli_env_lock()` helper. The helper recovers from a poisoned lock, so one failing test no longer fails every other lock holder. The 11 existing lock sites use it too. - New scan test `every_cli_parsing_test_holds_the_env_lock` reads `main_tests.rs` and fails when a `#[test]` that parses the CLI, directly or through a helper in the file, does not take the lock. ## Validation - Revert check: removing the lock from one test makes the scan test fail and name that test. - `cargo test --profile test-fast --features cuda --bin mlxcel`: 10 of 10 consecutive default-parallel runs passed (365 passed). - `cargo clippy --features cuda --bin mlxcel --tests -- -D warnings`, `cargo fmt --check` and the four `scripts/ci` contract checks pass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2897
Part of #2903
Summary
ResponsesStorecan now trackbackground: trueresponses from the moment they are created. Active jobs live in a separatejobsmap under their own lock, outsideentries, so TTL and LRU eviction never touch them. This PR adds no HTTP behavior; the create-and-poll sub-issue (#2898) consumes the API.Store API
Implemented with the signatures from the issue:
BackgroundAdmitError { Disabled, LimitReached { limit }, OverBudget }andBackgroundCancel { NotFound, Requested, Terminal(ResponseObject) }.admit_background(snapshot, input_items, effective_store) -> Result<InFlightToken, BackgroundAdmitError>: forces the snapshot status toqueued.Disabledwhen the job limit,max_entriesormax_bytesis 0,LimitReachedat the job limit,OverBudgetwhen active bytes plus the new record exceedmax_bytes.mark_background_in_progress(id) -> bool:queuedtoin_progressonly.finish_background(id, terminal) -> ResponseObject: removes the job and inserts the terminal entry in one critical section (lock orderjobs, theninner). First writer wins; later calls return the recorded object while it is retained, otherwise their own argument. A terminal object larger thanmax_bytesis recorded as afailedobject witherror.code: "server_error", empty output, and a message naming the budget.background_snapshot(id): live snapshot of an active job or the retained terminal object, including astore: falseone. Does not refresh LRU.background_status(id): active jobs only.request_background_cancel(id): flips the job's token and keeps it active; returnsTerminalfor a retained finished response.wait_background_terminal(id, timeout): returns the recorded terminal object (from a shared slot set before waiters are woken, so eviction cannot race it), orNoneon timeout or for an unknown id.active_background_jobs().Active bytes are tracked separately, included in
approximate_total_bytes, and never driveevict_to_limits. The terminal object of astore: falsejob is an ephemeral entry:get()(and soprevious_response_id) skips it,background_snapshotreads it, and the existing lazy sweep removes it after the ephemeral TTL.Flags
--responses-background-max-jobs/MLXCEL_RESPONSES_BACKGROUND_MAX_JOBS, default 64,0disables background mode.--responses-background-ephemeral-ttl-secs/MLXCEL_RESPONSES_BACKGROUND_EPHEMERAL_TTL_SECS, default 600, at least 1 (clap range;0fails startup).Both are wired through
mlxcel serve(src/main.rs,src/commands/serve.rs),mlxcel-server(src/bin/mlx_server.rs),cli_input.rsandstartup.rsintoResponsesStoreConfig, and documented indocs/responses-api.mdanddocs/environment-variables.md.Notes for sibling sub-issues
ResponsesStoreConfiggained two fields, so struct literals need..ResponsesStoreConfig::default(). The two existing literals in the store tests were updated that way; no existing test assertion changed.remove()(the DELETE path) also deletes astore: falseterminal entry; feat: Create and poll background Responses #2898 decides what DELETE returns for those ids.max_bytes, even the trimmedfailedobject can exceed the budget and is then evicted at once; waiters still receive it.Entry.ephemeral_ttl) rather than anInstant, which avoids instant overflow for very large TTL values. It is private and does not affect the API.Changes during review
finish_backgroundnow serializes the terminal object once, before taking any lock, and reuses the input-item size computed at admission, so a large result no longer blocks admits and polls behind thejobsmutex. Foregroundinsert()also sizes its payload before taking the store lock.Tests
src/server/responses_store_background_tests.rs: limit and slot release, disabled and over-budget admission, queued to in_progress, survival under TTL expiry and LRU pressure, 8-thread concurrent finish,store: falsevisibility and expiry, oversized terminal tofailed, active-byte accounting through a lifecycle, cancel, and wait wake-up and timeout.src/main_tests.rsandsrc/bin/mlx_server.rs.Verification
cargo test --profile test-fast --features cuda -p mlxcel --lib responses_store -- --test-threads=1: 29 passed (16 existing, 13 new).responses_backgroundselector): 3 + 3 passed.cli_input,server::routes::responsesandserveselectors pass unchanged.-D warnings,cargo fmt --check,check_env_var_docs.pyand the license header check pass.mlxcel-server --helplists both flags with their env vars;--responses-background-ephemeral-ttl-secs 0andMLXCEL_RESPONSES_BACKGROUND_EPHEMERAL_TTL_SECS=0both exit 2 with a range error; a non-numericMLXCEL_RESPONSES_BACKGROUND_MAX_JOBSis refused, proving the env var is read. A server onmodels/qwen3-0.6b-4bitstarted with both env vars set servedPOST /v1/responses(store: true, statuscompleted, output2 + 3 = 5.) and returned it fromGET /v1/responses/{id}.