Skip to content

Studio: quiet the image and video generation progress polls - #7869

Merged
danielhanchen merged 5 commits into
mainfrom
quiet-image-progress-polls
Aug 5, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
quiet-image-progress-polls

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Generating a single image floods stdout with one access-log line per progress poll:

{"level": "info", "event": "request_completed", "method": "GET", "path": "/api/inference/images/generate-progress", "status_code": 200, "process_time_ms": 1.64}

The Image and Video tabs poll generate-progress every 300ms while a job is running, plus a 1s recovery loop, so a one minute generation produces a couple of hundred of these lines and buries everything else.

Why the existing suppressors missed it

The access log already has two guards in studio/backend/loggers/handlers.py, and both are exact-path matches:

  • _QUIET_SUCCESS_PATHS drops the 2xx line entirely for polls whose signal is carried by real events.
  • _QUIET_POLL_PATHS collapses liveness polls to a 10s heartbeat.

Everything else falls back to _ACCESS_LOG_DEDUP_MS, a 300ms burst window meant for the SPA fanning one cache invalidation into several list fetches. A poll running at 300ms or slower always lands just outside that window, so every single request logged.

Both lists were written before the image and video routes existed, and those routes live one segment deeper (/api/inference/images/..., /api/inference/video/...) than the already-listed /api/inference/load-progress, so none of the new polls ever matched. The same applies to the diffusion training status poll.

What changed

Added the timer-polled paths to the existing lists. No new mechanism.

Heartbeat every 10s (_QUIET_POLL_PATHS):

  • /api/inference/images/generate-progress
  • /api/inference/video/generate-progress
  • /api/train/diffusion/status

Generation is fire-and-forget: the POST returns immediately and the terminal outcome only reaches the UI through these polls, so I kept a pulse rather than dropping them outright.

Suppressed on 2xx (_QUIET_SUCCESS_PATHS):

  • /api/inference/images/load-progress
  • /api/inference/video/load-progress

These mirror /api/inference/load-progress, which is already on the list. The load phases are logged by diffusion.loaded and video.loaded.

What stays logged

  • Any non-2xx on those paths, every time. A failing poll stays fully visible.
  • The first hit of each heartbeat burst.
  • All mutations, including the POST that starts a generation.
  • The event-driven reads that are not on a timer, such as /api/inference/images/status, /api/inference/images/info and /api/inference/video/status.

Tests

Extended studio/backend/tests/test_logging_middleware.py with four cases: the polls collapse to one heartbeat, a repeated 500 on the same path logs every time, the load polls are suppressed while a 503 still logs, and the unrelated image and video routes are untouched.

studio/backend/tests: 16523 passed, 20 failed, 68 skipped. The 20 failures are pre-existing on main (mcp stdio, openai tool passthrough, live studio_api server tests) and reproduce unchanged on an untouched checkout.
studio/backend/hub/tests: 172 passed.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: ab46013368

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

… load polls

Two defects in the original change, both found by reviewing it against what the
flags promise.

--verbose sets both dedup windows to 0 and promises to log every request, but the
drop-the-2xx suppressor has no window of its own, so it kept swallowing those paths
regardless. It now reads the same signal, and --verbose really does log everything.

The image and video load-progress polls were fully suppressed on the reasoning that
diffusion.loaded and video.loaded already cover the load. They do not: those fire
once the load is finished, and unlike /api/inference/load-progress, whose handler
emits inference_load_progress every 10%, these endpoints only read state. A
diffusion load runs minutes through download, quantise and compile, so full
suppression left no sign it was still moving. They move to the 10s heartbeat
instead, which is the shape the rest of that list already uses.
@danielhanchen

Copy link
Copy Markdown
Member Author

Re-review: measured effect, and two defects this round fixed

I measured the actual line rate in a running Studio rather than reasoning about it from the poll intervals, then went looking for cases where the new suppression is wrong rather than merely effective. Two real defects came out, both in code this PR introduced.

1. Before and after

Measured live in a running Studio, counting access-log lines across the five newly quieted paths:

lines/min
before 419
after 18

A 95.7% cut. Reproduced under Chromium, Firefox and WebKit, which matters because the rate is set by the frontend's timers, not by anything server-side.

The five paths and the timers behind them:

path interval site
/api/inference/images/generate-progress 300ms images-page.tsx:1660, :2234
/api/inference/video/generate-progress 300ms video-page.tsx:1012
/api/inference/images/load-progress 1s images-page.tsx:1616
/api/inference/video/load-progress 1s video-page.tsx:950
/api/train/diffusion/status 1.5s diffusion-train-panel.tsx:552

The two 300ms setIntervals are the direct cause of the report, and for a reason worth naming: _ACCESS_LOG_DEDUP_MS is also 300, so each poll lands just outside the previous one's dedup window and none of them ever collapsed. The 1s and 1.5s pollers are clear of that window by construction and never deduped either.

2. Is the issue real

Yes, and it is worth being precise about why the existing dedup did not already handle it. The generic access-log dedup window and the frontend poll interval are both 300ms, so the two beat against each other: every poll is "new" by a millisecond or two. The fix is not a bigger generic window (which would start hiding real request pairs) but a named set of paths whose line means only "still polling", collapsed to a 10s heartbeat.

3. What this round changed

a. --verbose could not reach the suppressed paths. unsloth studio --verbose promises to log every request, and it delivers on that by setting both dedup windows to 0 (unsloth_cli/commands/studio.py:35-36). But the drop-the-2xx suppressor _is_quiet_success has no window of its own, so zeroing the windows did not affect it: --verbose still silently dropped those paths, which is the exact opposite of what the flag is for, and it would have made a poll-related bug report undiagnosable. _VERBOSE_ACCESS_LOG now reads the same signal and disables the whole suppressor.

b. The two diffusion load-progress paths would have gone silent during a load. They were in the full-suppression set on the rationale that the diffusion.loaded / video.loaded events carry the signal instead. That rationale is wrong, and I verified it in the handlers rather than from the comment: the sibling /api/inference/load-progress is safe to suppress fully because its handler emits inference_load_progress on every 10% step from inside the handler, but these two are pure state reads that log nothing, and diffusion.loaded / video.loaded are terminal events fired once after the load has already finished. A diffusion load runs for minutes across download, quantization and torch.compile, so under full suppression a load stuck in any of those phases would emit nothing at all on any channel. They are in the 10s heartbeat set now, not the drop set, so there is always a pulse showing the load is still moving.

4. Does merging break anything

The suppressor is GET-only and 2xx-only, so no mutation, no error and no non-GET request is affected on any path. Every quieted path still logs its first hit and every error; only the repeat 2xx polls collapse. Nothing outside the two literal path sets changes behaviour, so no route this PR does not name can be affected.

Operationally the escape hatch is complete in both directions: UNSLOTH_STUDIO_ACCESS_LOG_POLL_DEDUP_MS=0 restores per-poll logging for the heartbeat set, and --verbose now genuinely restores everything. Both are read once at import, so an existing deployment that sets neither sees only the reduced volume.

5. Verification

  • Live line-rate measurement under Chromium, Firefox and WebKit.
  • Handler-level confirmation for every path in the drop set that some other channel actually carries its signal, which is what turned up defect (b).
  • Full backend suite: the same pre-existing failure set as origin/main, no new failures. Worth recording that the known-failure baseline of 20 is stale by 3 for a reason unrelated to this PR: test_studio_api.py has grown to 16 tests, and all 16 fail or error environmentally because the fixture shells out to an installed ~/.unsloth/studio rather than the worktree.

6. One thing a path list cannot fix

/api/inference/audio/stt/status is fetched with a ?refresh=<nonce> cache-buster (studio-model-dictation-adapter.ts:163), and the dedup key includes the query string, so every request is unique and no path list of any kind can quiet that route. Adding more literals would not help. That needs the dedup key to be built from the path alone for known-noisy routes, which is a design change rather than another entry, so I would rather do it as a focused follow-up than widen this one.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

Verification summary

Two changes landed on top of the original fix after a closer look at what the poll channel is actually carrying. Details below.

1. Before and after, measured

Driven against the real LoggingMiddleware, one minute of polling per path at the cadence the frontend actually uses (images-page.tsx:1660,2234 and video-page.tsx:1012 at 300ms, diffusion-train-panel.tsx:552 / diffusion-train-dialog.tsx:112 at 1500ms, images-page.tsx:1616 at 1000ms):

path poll before after
/api/inference/images/generate-progress 300ms 199 lines/min 6
/api/inference/video/generate-progress 300ms 199 lines/min 6
/api/train/diffusion/status 1.5s 40 lines/min 6
/api/inference/images/load-progress 1s 60 lines/min 6
/api/inference/video/load-progress 1s 60 lines/min 6
total 558 lines/min 30

A 10 minute video generation goes from roughly 1,990 access-log lines to 60.

Confirmed on a live Studio instance built from this branch and driven through a real browser, in all three engines. The spread before the fix is genuine setInterval jitter against the 300ms window; after, all three collapse to exactly 6.0/min because the heartbeat is server side:

engine before after
Chromium 148 125.6/min 6.0/min
Firefox 150 148.5/min 6.0/min
WebKit 26.4 194.3/min 6.0/min

Edge and Safari were not run. Edge is Blink and Safari is WebKit, so they track the Chromium and WebKit rows; the dedup is entirely server side and no client can influence it.

2. Is the fix the right shape

The symptom was real, and the root cause is confirmed: both suppressor lists are exact-string matches, so an unlisted path falls back to the 300ms burst window, which a 300ms poll timer straddles. Measured directly, about 61% of polls survive that window, erratically, because the comparison is decided by float error at that spacing.

Raising _ACCESS_LOG_DEDUP_MS instead would have been worse and would not even have fixed it: 300ms polls under a 1000ms window still emit about 1/s, roughly 240 lines for a 4 minute generation. No global value works, because poll cadences in this app span 300ms to 4s; anything wide enough for the 4s polls has re-implemented the 10s heartbeat for every endpoint in the app. That window is also documented as a burst collapser for SPA cache-invalidation fan-out, which is a different phenomenon from a poll cadence.

Heartbeat rather than full suppression is the right call for generate-progress, and it turns out to be load-bearing: the entire generate() body in core/inference/diffusion.py contains exactly two logger calls, both warning, and generate_progress() is a pure state read. On the happy path a healthy generation emits nothing else at all, so the 10s pulse is the only liveness evidence there is.

First change. That same reasoning did not hold for the two load-progress paths, which were originally sent to full suppression on the grounds that diffusion.loaded / video.loaded already cover the phases. Those are terminal events, fired once the load has finished. The sibling this was modelled on, /api/inference/load-progress, is genuinely safe to silence because its handler emits inference_load_progress every 10% (routes/inference.py:7876); the image and video handlers only read state and log nothing. A diffusion load runs minutes through download, quantization and compile, so full suppression left a stuck load with no signal on that channel. Both now heartbeat instead: still a 90% cut, with a pulse retained.

Second change. unsloth studio --verbose documents "Log every API request, including the high-frequency polling that is deduplicated by default", and _enable_verbose_access_logs() zeroes both dedup windows. But _is_quiet_success had no window of its own and read no env var, so --verbose could never reach _QUIET_SUCCESS_PATHS or _CHAT_LIST_PATHS. Reproduced: with both knobs at zero, five polls of a suppressed path produced zero lines. LOG_LEVEL=DEBUG cannot help either, since the suppression happens before logger.info is ever called. That was pre-existing for the paths already listed, and this change would have widened it. _is_quiet_success now stands down when both windows are off, which is exactly the state the flag produces, so no CLI change is needed and an already-installed CLI picks it up.

3. Merge risk

Observability only, so the risk is lost signal rather than a crash. Checked:

  • Non-2xx always logs, every time, no dedup: verified for 404, 500 and 503 on all five paths. Both suppressors are GET/2xx only.
  • Nothing sharing a prefix is caught. Enumerated every route neighbouring the five (images/gallery, images/status, images/info, video/gallery, video/status, diffusion/runs, diffusion/dataset, and the rest, 25 in total) and confirmed each still logs.
  • HEAD, OPTIONS and every mutation always log. A browser preflight never even reaches this middleware, since CORS sits outside it.
  • No unbounded state. Two tabs and two concurrent generations collapse to one shared 10s pulse, since the dedup key carries no client identity. That is pre-existing behaviour of the 300ms window, not new here, and concurrency is still legible from the never-deduped POST .../generate lines.
  • Cadence does not drift. Over 600s of simulated polling and 200s of real browser polling, every gap was 10.2s (ceil(10/0.3) ticks), max minus min 2ms across 20 intervals.
  • time.perf_counter is monotonic and unaffected by system clock updates on Windows, macOS and Linux, so no NTP or DST hazard. This is pure Python with no path handling, and URL paths are case-sensitive per RFC 3986 6.2.2.1, so no client OS can perturb matching. No GPU relevance.

4. Compatibility, and whether to generalize

Backwards: purely additive to two module-private sets. Nothing outside handlers.py imports either set, no test asserts their membership or length, and no signature, config or env change. A listed path that an old frontend never requests simply costs nothing.

Forwards is the real question, and the exact-string matching is indeed what caused this. The lists have been amended four times in seven weeks, always reactively, and the rot window here was five hours: the routes landed in #6763 at 12:11 and this was filed at 17:10.

I looked hard at generalizing and concluded it should not happen in this change:

  • A /api/inference/ prefix rule would silence eight non-poll routes under that prefix, including one that mints a 12 hour signed credential, two that bypass bearer auth, and an unauthenticated preview route. That is a worse outcome than the rot.
  • A /status suffix rule would silence /api/inference/images/status and /api/inference/video/status, which this change's own test asserts must keep logging. A bare progress suffix would catch /api/train/progress, which is an SSE stream.
  • More literals do not close it either. Four timer-polled paths remain uncovered, and two of them a path list structurally cannot express: /api/rag/{scope}/{id}/documents is templated, and /api/inference/audio/stt/status is fetched with a ?refresh={nonce} cache-buster, and the dedup key includes the query string, so no list of any kind can quiet it.

The durable fix is to move the declaration to the route, which is viable here: FastAPI populates scope["route"] and Starlette populates scope["endpoint"], and because Router.app mutates the same scope dict, both are readable by this pure-ASGI middleware after it delegates downstream. Verified against the installed starlette 1.3.1 and fastapi 0.138.0 rather than taken from the widely repeated claim that middleware cannot see the route, which is about BaseHTTPMiddleware.dispatch running before call_next and does not apply here. On a 404 both are None, so an unmatched path can never be silenced by accident. Paired with a test that enumerates poll-shaped GET paths from app.openapi() and fails when one is neither marked nor listed nor explicitly allowlisted, that is what actually stops the rot; a marker alone just gives the author somewhere to record the choice. Happy to open that as a follow-up rather than widen a noise fix into a refactor.

Tests

studio/backend/tests/ and studio/backend/hub/tests/ run separately. test_logging_middleware.py is 23 passing, up from 21, with the two new cases covering the verbose restore and the default staying quiet. hub/tests/ is 172 passing, no failures. tests/ is 16,522 passing with 23 non-passing, all pre-existing and unrelated: 16 in test_studio_api.py, which shells out to a separately installed Studio that has no torch on this machine, plus test_mcp_stdio_improvements 3, test_openai_tool_passthrough 2, test_openai_auto_download 1 and test_hf_xet_fallback 1.

The one red check on the previous head, test_chat_response_details_ui_contract.py::test_response_model_badge_is_user_configurable_and_rendered_once_per_message, is pre-existing on main and unrelated. This branch touches two backend logging files and no frontend file; the test fails identically at origin/main.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 10d5903df8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 49325471eb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Aug 5, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Aug 5, 2026
@danielhanchen
danielhanchen merged commit 07b395f into main Aug 5, 2026
39 of 42 checks passed
@danielhanchen
danielhanchen deleted the quiet-image-progress-polls branch August 5, 2026 12:50
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