Skip to content

Studio: keep the server running when several PDFs are indexed at once - #18017

Merged
danielhanchen merged 3 commits into
unslothai:mainfrom
NilayYadav:pymupdf-ingest-lock
Nov 17, 2026
Merged

danielhanchen merged 3 commits into
unslothai:mainfrom
NilayYadav:pymupdf-ingest-lock

Conversation

@NilayYadav

@NilayYadav NilayYadav commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #17981. Indexing two or more damaged PDFs at once (a linked folder, or several files added to a chat) segfaulted the Studio server: "Server stopped unexpectedly", exit code 0xc0000028 on Windows. PyMuPDF is not thread-safe, and only web PDF reads took a lock; ingestion parsed on several threads without one.

All PyMuPDF use now shares one lock: parsing, page and figure rendering, OCR, the citation highlight lookup and web PDF reads. Four healthy PDFs index as fast as before (290-420 s here, 361-912 s on main). Files in a batch finish closer together, since the highlight lookup waits for PDFs still being parsed.

Six damaged PDFs added to a chat: the server dies mid-indexing on main, all six index with this PR.

Before After
before after

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T06:33:27.094205Z 68e3604 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: db8ab96be8

ℹ️ 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 that referenced this pull request Nov 16, 2026
…t (fsdp2 red since accelerate 1.16) (#18025)

## Summary

`Repo tests (CPU, fsdp2)` fails on main since accelerate 1.16.0 reached
PyPI today (seen on #18012 and #18017, both on `20d05c20`):

```
tests/test_fsdp2_wrap_gloo.py::test_activation_checkpointed_blocks_keep_the_last_block_gathered
    assert r["blocks"] == N_LAYERS
E   assert 0 == 3
```

## Cause

With activation checkpointing on, accelerate wraps each decoder block in
a `CheckpointWrapper` and then calls `fully_shard`. Up to 1.15 it
sharded the block inside the wrapper. 1.16 shards the wrapper instead
and skips the block inside ("Shard the wrapper to keep FSDP hooks
outside compiled code", `fsdp2_prepare_model`).

`after_fsdp2_wrap` finds the blocks with `_decoder_blocks(model)` and
keeps the ones that are `FSDPModule`s. On 1.16 the block is no longer
one, and the wrapper's class is not a decoder layer class, so it found
no blocks. The fused LoRA gate does not depend on that list (it checks
that each fused layer's owning unit sits in the layer list, which the
wrapper does). What was lost is the per-block tuning: the last block was
no longer kept gathered after forward
(`set_reshard_after_forward(False)`), so with activation checkpointing
every step gathered it again at the start of backward.

The test is right. accelerate's change is reasonable, and Unsloth's
block lookup assumed the old layout.

## Change

- `unsloth/models/fsdp2.py`: new `_block_units`, which returns each
decoder block's FSDP unit: the block itself, or the checkpoint wrapper
around it when that wrapper is the `FSDPModule`
(`_checkpoint_wrapped_module is block`). `after_fsdp2_wrap` uses it for
the block list, the block count and the last-block reshard setting.
- New `tests/test_fsdp2_block_units.py` (in the `fsdp2` shard): both
layouts with stand-in classes, so the 1.16 layout is checked whatever
accelerate CI installs, plus a case where neither the block nor the
wrapper is sharded.

## Verification

- `tests/test_fsdp2_wrap_gloo.py` on accelerate 1.16.0: the checkpointed
test fails on main (`0 == 3`) and all 5 pass with this change. On
accelerate 1.15.0: 5 passed with this change.
- `tests/test_fsdp2_block_units.py`: 3 passed. Without the wrapper
lookup, the wrapper case fails.
- The whole `fsdp2` shard file set on accelerate 1.16.0: 247 passed, 16
skipped.
@danielhanchen danielhanchen self-assigned this Nov 17, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 7c0bacfe2f

ℹ️ 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 Etherll/unsloth that referenced this pull request Nov 17, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 68e3604609

ℹ️ 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".

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.

[Bug] After last lama.cpp update unsloth desktop crashes

2 participants