Skip to content

fix(studio): return /load immediately and load models in the background (#5206) - #7010

Closed
rodboev wants to merge 5 commits into
unslothai:mainfrom
rodboev:pr/inference-load-async
Closed

rodboev wants to merge 5 commits into
unslothai:mainfrom
rodboev:pr/inference-load-async

Conversation

@rodboev

@rodboev rodboev commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Large model loads can exceed proxy response-header timeouts before POST /api/inference/load sends a response.

Fix

  • Add an opt-in async_load request field that returns an accepted response before slow loading work starts.
  • Keep one process-wide admission owner for async and synchronous model loads, with the existing request-id cancellation handshake and lifecycle gate.
  • Expose the pending model immediately through /status, preserve redacted public labels, and prevent matching local model deletion until the operation settles.
  • Publish only the current operation's terminal error, preserve active-generation checks and explicit user-load provenance, and keep the synchronous response path unchanged.

async_load remains opt-in, so existing clients continue using the synchronous contract.

Testing

Focused async-load tests: 13 passed. Tunnel-safe load and unload-cancel regressions: 86 passed. Python compilation and diff checks passed.

@rodboev
rodboev requested a review from danielhanchen as a code owner July 9, 2026 02:19
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a0af8406ad

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
@rodboev
rodboev force-pushed the pr/inference-load-async branch from e20ab7c to 8eb835c Compare July 9, 2026 04:06

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 89dabf0b8d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
@Imagineer99 Imagineer99 self-assigned this Jul 9, 2026
@Imagineer99

Copy link
Copy Markdown
Member

Ran a focused async-load check here: https://github.com/Imagineer99/unsloth/actions/runs/29036817489

A few edge cases still reproduce:
if load A is running, load B is accepted, then A fails, /status can still show A’s error for B
repeated async /load calls can queue multiple accepted background loads
after /load returns accepted, /status can still look idle/previous-model until the backend task actually reaches loading
So I think this still needs a small generation/pending-load guard before merge.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 93186ec9b8

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
@rodboev

rodboev commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the focused async-load check. I agreed with the three gaps and reworked the async path around a single current pending load.

The updated route records the pending model and a generation before scheduling the background task, so /status can show the accepted model immediately instead of idle or only the previous model. A second async /load while that task is still pending now returns 409 with the pending model in the message, so repeated calls cannot queue multiple accepted background loads behind the lifecycle gate.

Background success and failure now check the captured generation before writing shared error state, and the task cleanup only clears the accepted slot for the matching active task. In practice the 409 guard prevents A-running/B-accepted overlap, and the generation check keeps state writes tied to the accepted load that owns the slot.

I also merged the route-level pending model into both GGUF and non-GGUF /status responses. The backend-owned loading and loaded state stays unchanged once the real load reaches _load_model_impl().

The new focused coverage hits duplicate accepted-load rejection, immediate pending visibility before backend loading state exists, the previous-GGUF-loaded plus new-pending status case, and the existing sync-path contract.

@rodboev
rodboev force-pushed the pr/inference-load-async branch from d52b811 to 8bad79a Compare July 9, 2026 18:03

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e4bc42a98d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f9a5d003bc

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d0a7d1207d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
@Imagineer99

Copy link
Copy Markdown
Member

Ran the focused async-load check again on latest head: https://github.com/Imagineer99/unsloth/actions/runs/29095092203
Looks good now. The async lifecycle cases I was worried about are covered

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 26b1762c6d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b096aa7822

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Resolved
@danielhanchen

Copy link
Copy Markdown
Member

Confirmed main has no async load path and that this keeps POST /api/inference/load unchanged unless async_load=True, with the pending model surfaced in /status and guarded against deletion. Could you rebase, since the branch conflicts with main right now, and then I will get this reviewed.

@rodboev
rodboev force-pushed the pr/inference-load-async branch from 7d07de5 to 5cd1fbe Compare August 19, 2026 13:23
@rodboev

rodboev commented Aug 19, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for confirming. I rebuilt the async path on current main. async_load=True now claims the shared load owner before returning, keeps pending status and deletion identity visible, preserves the existing cancellation and active-generation path, and leaves sync loads unchanged.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed main has no async load path and that this keeps POST /api/inference/load unchanged unless async_load=True, with the pending model surfaced in /status and blocked from deletion while in flight; thanks for the rebase.

rodboev and others added 5 commits August 20, 2026 05:41
Signal pending async admissions before ordinary unload waits on the lifecycle
gate, classify cancellation as cancellation, and clear stale async errors after
successful synchronous loads.
Reuse verified native path grants, keep synchronous request ownership during
tunnel padding, classify backend cancellation correctly, and cancel pending
async operations before ordinary unload queues on the lifecycle gate.
@danielhanchen

Copy link
Copy Markdown
Member

Re-checked the rebased branch and the synchronous /load contract is still unchanged with async_load off, so nothing further needed from you right now; it is queued for review.

@danielhanchen

Copy link
Copy Markdown
Member

Thank you for the PR and for the careful iteration on the pending-load lifecycle!

Closing this as superseded. The underlying issue (#5206) has been fixed on main by a different approach: POST /api/inference/load (and /unload) now run through _tunnel_safe_json in studio/backend/routes/inference.py, which commits the response headers after 15s and pads the body every 20s until the load finishes, so Cloudflare / nginx response-header timers no longer fire. A failure after the headers are committed is reported in the body as _deferred_error, which the web UI already handles in chat-api.ts.

That fix applies to every existing client, including the official web UI that the reporter was using behind Cloudflare, without needing a new opt-in field. Since async_load is opt-in and the frontend never sets it, this branch would not change behaviour for that case, and the extra admission / pending-status state would be a second load path to maintain. The branch also conflicts with main.

If there is a client that still needs a fire-and-forget load on top of the padded response, feel free to open an issue describing it and we can revisit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants