Skip to content

Studio sd.cpp: stage server-mode image LoRAs as hard links so sd-server lists them - #14860

Merged
danielhanchen merged 2 commits into
mainfrom
fix-sdcpp-server-lora-hardlink
Oct 23, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
fix-sdcpp-server-lora-hardlink

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

Since the master-982 pin (#14565), the resident sd-server ignores image LoRAs that Studio stages for it. materialize_native_dir stages each adapter as a symlink from the per-request gen_* directory, under --lora-model-dir, to the file in the LoRA cache. In sd-server at 0518b34 (unchanged in the Unsloth patch set):

  • examples/server/runtime.cpp:294-298 (refresh_lora_cache) calls fs::canonical on every entry and skips any entry that resolves outside the LoRA directory.
  • examples/server/runtime.cpp:336-338 (get_lora_full_path) repeats the containment check on the canonical path and requires a regular file.

So the staged symlink is never listed. The structured lora entry then fails to resolve, and the request is rejected.

With the real master-982 CPU sd-server, /sdapi/v1/loras lists a regular file and a hard link but leaves out Studio's symlink. The server log shows skipping inaccessible LoRA or path outside LoRA directory ... runtime.cpp:297.

Fix

materialize_native_dir gains real_files=True, which stages a hard link with the existing copy fallback (for example across filesystems). The server generation path passes it. A hard link's canonical path stays inside the server directory, so sd-server's containment check is satisfied and stays fully in force. sd-cli staging is unchanged: it resolves <lora:...> tags with fs::exists / is_regular_file, which follow symlinks.

Tests

  • test_materialize_native_dir_real_files_hardlinks_inside_dest: checks the staged file is not a symlink, its canonical path is inside the destination, and it is the same inode as the source.
  • test_materialize_native_dir_real_files_copies_when_hardlink_fails: checks the copy fallback when os.link raises.
  • test_generate_server_stages_loras_as_real_files_inside_the_lora_dir: runs the real materializer through server-mode generate, checks the staged file resolves inside --lora-model-dir, and checks that cleanup leaves the source intact.
  • On main, these 3 tests fail. With the fix, test_diffusion_lora.py and test_sd_cpp_backend.py give 190 passed.
  • With the real master-982 sd-server, the main materializer's output is skipped and the fixed materializer's output is listed.

Not covered

  • No full image render with a LoRA through the server: there is no tiny sd.cpp image GGUF on this host. Only the scan and resolve steps were exercised against the real binary.
  • No Windows run. os.link on NTFS should work, and the copy fallback covers FAT and exFAT.

sd-server master-982 canonicalizes every file under --lora-model-dir and
skips one that resolves outside it, so the symlink Studio staged into the
LoRA cache was never listed and a request naming it was rejected. Server
staging now hard links the adapter (copy fallback), which keeps the
canonical path inside the server directory. sd-cli staging is unchanged.
@danielhanchen danielhanchen self-assigned this Oct 23, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T02:55:58.904112Z cab6583 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 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: f4cf7333b3

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

Comment on lines +4746 to +4748
materialized = diffusion_lora.materialize_native_dir(
lora_resolved, lora_stage, real_files = True
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Restage LoRAs after restarting the server on CPU

This stages the hard links only in the current server's lora_dir. If generation triggers the unsupported-GPU-op fallback, _restart_server_on_cpu_backend stops that server—deleting its scratch directory—and starts a replacement with a different lora_dir, but the retry reuses the original lora payload. Consequently, LoRA requests requiring the automatic CPU fallback reference a nonexistent gen_* path and fail; restage the adapters in the replacement server and rebuild the payload, as is already done for the hires upscaler.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch, fixed. LoRA staging now lives in a per-server helper, like the hires upscaler. When the CPU-backend restart replaces the server, the adapters are staged again in the new server's lora_dir and the retried payload's lora block is rebuilt. If the replacement has no LoRA directory, the request fails with a clear error instead of silently dropping the LoRAs. All staged directories are removed in the existing finally. Covered by test_server_cpu_restart_restages_loras_in_the_new_server_dir, which fails on the previous head.

Comment on lines 629 to 631
except OSError:
import shutil
shutil.copy2(r.path, link)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Remove partial staging files when the copy fallback fails

When the cache and server scratch directory are on different filesystems, os.link raises EXDEV and this branch copies the potentially multi-GB adapter. If copy2 then fails, such as when /tmp runs out of space, it leaves the partial file and any earlier adapters in dest; the server caller invokes materialization before entering its cleanup try/finally, so that gen_* directory persists until the server is stopped and repeated attempts can retain exhausted disk space. Clean dest on failure or move server staging inside the cleanup scope.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed. A failed copy fallback now removes its partial file before re-raising. Server staging also moved inside the generation try/finally, so any adapters already staged in the gen_* directory are removed on failure too. Covered by test_server_lora_staging_failure_leaves_no_partial_stage (hard link refused, copy fails part-way), which fails on the previous head.

…ed copies

The CPU-backend fallback replaces sd-server with one that has its own
scratch directory, but the retry reused the LoRA payload staged in the
stopped server's directory. Stage the adapters again in the replacement,
as the hires upscaler already is. Staging now runs inside the cleanup
scope, and a copy fallback that fails removes its partial file.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: cab65835ce

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant