stable-diffusion.cpp/examples/server
Claude eeec4916a5
feat(server): stop a job that is generating
POST /sdcpp/v1/jobs/{id}/cancel used to answer 409 for a job that had
started. It now tells the library to stop and waits for the job to end:
200 with the cancelled job as soon as it has ended, or 202 with the job and
"cancel_requested": true when it is still stopping after ?wait= seconds
(0 to 60, default 10; 0 answers at once). Repeating the call is harmless,
and GET /sdcpp/v1/jobs/{id} shows the pending request. /capabilities now
reports cancel_generating as true.

The request belongs to the job: ContextKeeper::run() takes a CancelToken
and registers it, with the context, only while that job's attempt is
executing. A stop for one job can therefore never reach another request
(including a synchronous /sdapi one), a job cancelled before it reaches the
context generates nothing, and the context can be released or rebuilt
without cancel() ever touching a context that is gone.

The library forgets a stop request when a generation begins, so a request
that arrives a moment earlier would be lost. The route repeats it every
100 ms until the job ends.

A cancelled run is neither a failure nor a reason to step down: it does not
reload the model, does not touch last_error, and has its own counter,
counters.cancelled in /health. When a stop and a recoverable failure meet,
the stop wins and the model is not reloaded for a request nobody waits for.

Checked on a real sd-server with a CPU build and a synthetic model: stop of
a running job, repeated stop, queued job, job already finished, unknown id,
wait=0, invalid wait, and the next job on the same context. Tests and
documentation follow in separate commits.

Claude-Session: https://claude.ai/code/session_01EhZzDBkuuMp1jBf4WtEySC
2026-10-14 16:35:58 +02:00
..
frontend@8e7c115059 sync: update frontend 2026-10-06 20:04:34 +08:00
api.md feat: keep sd-server serving when a request runs out of memory 2026-10-11 10:40:29 +02:00
async_jobs.cpp feat(server): stop a job that is generating 2026-10-14 16:35:58 +02:00
async_jobs.h feat(server): stop a job that is generating 2026-10-14 16:35:58 +02:00
CMakeLists.txt feat: keep sd-server serving when a request runs out of memory 2026-10-11 10:40:29 +02:00
main.cpp feat: keep sd-server serving when a request runs out of memory 2026-10-11 10:40:29 +02:00
README.md feat: keep sd-server serving when a request runs out of memory 2026-10-11 10:40:29 +02:00
recovery.cpp feat(server): stop a job that is generating 2026-10-14 16:35:58 +02:00
recovery.h feat(server): stop a job that is generating 2026-10-14 16:35:58 +02:00
routes.h feat: keep sd-server serving when a request runs out of memory 2026-10-11 10:40:29 +02:00
routes_index.cpp feat: add sdcpp api support (#1407) 2026-04-11 17:49:00 +08:00
routes_openai.cpp feat: keep sd-server serving when a request runs out of memory 2026-10-11 10:40:29 +02:00
routes_sdapi.cpp feat: keep sd-server serving when a request runs out of memory 2026-10-11 10:40:29 +02:00
routes_sdcpp.cpp feat(server): stop a job that is generating 2026-10-14 16:35:58 +02:00
runtime.cpp feat: keep sd-server serving when a request runs out of memory 2026-10-11 10:40:29 +02:00
runtime.h feat: keep sd-server serving when a request runs out of memory 2026-10-11 10:40:29 +02:00

Example

The following example starts sd-server with a standalone diffusion model, VAE, and LLM text encoder:

.\bin\Release\sd-server.exe --diffusion-model  ..\models\diffusion_models\z_image_turbo_bf16.safetensors --vae ..\models\vae\ae.sft  --llm ..\models\text_encoders\qwen_3_4b.safetensors --diffusion-fa --offload-to-cpu -v --cfg-scale 1.0

What this example does:

  • --diffusion-model selects the standalone diffusion model
  • --vae selects the VAE decoder
  • --llm selects the text encoder / language model used by this pipeline
  • --diffusion-fa enables flash attention in the diffusion model
  • --offload-to-cpu reduces VRAM pressure by keeping weights in RAM when possible
  • -v enables verbose logging (equivalent to --log-level verbose)
  • --cfg-scale 1.0 sets the default CFG scale for generation

Logging defaults to info. Use --log-level <level> to select debug, verbose, info, warn, or error (from most to least detailed). Each level includes messages at that level and all less detailed levels. -v and --verbose are equivalent to --log-level verbose. If repeated, the last logging option wins.

After the server starts successfully:

  • the web UI is available at http://127.0.0.1:1234/
  • the native async API is available under /sdcpp/v1/...
  • the compatibility APIs are available under /v1/... and /sdapi/v1/...

If you want to use a different host or port, pass:

--listen-ip <ip> --listen-port <port>

When the memory runs short

A request that makes the machine run out of memory does not stop the server. It frees the model, loads it again with a lighter configuration (profiles max > balanced > lite > lite-disk > lite-cpu, see --profile) and runs the same request again; from lite on, the VAE is decoded in tiles. The request is then served normally. When even the lightest profile does not fit, the request is answered with 503 and a Retry-After header, and the server stays up. After a quiet period the server climbs back one profile at a time.

  • --degradation-cooldown <seconds> sets how long the server waits before climbing back (default 300; 0 = stay on the lighter profile until the next restart)
  • --disable-degradation turns this off: a request that runs out of memory is answered with the error at once
  • GET /sdcpp/v1/health tells which profile is in use, how many times the server had to step down and the last error; GET /sdcpp/v1/resources reports the free memory of the machine and of each device

If the model does not fit at startup, the server starts on the first profile that loads it (the log says which). When the model cannot be loaded at all, or the address cannot be used, sd-server exits with a non-zero code and says why. See Failures and recovery for the answers a client gets.

Frontend

Build with Frontend

The server can optionally build the web frontend and embed it into the binary as gen_index_html.h.

Requirements

Install the following tools:

npm install -g pnpm

Verify installation:

node -v
pnpm -v

Install frontend dependencies

Go to the frontend directory and install dependencies:

cd examples/server/frontend
pnpm install

Build the server with CMake

Enable the frontend build option when configuring CMake:

cmake -B build -DSD_SERVER_BUILD_FRONTEND=ON
cmake --build build --config Release

If pnpm is available, the build system will automatically run:

pnpm run build
pnpm run build:header

and embed the generated frontend into the server binary.

Frontend Repository

The web frontend is maintained in a separate repository, https://github.com/leejet/sdcpp-webui.

If you want to modify the UI or frontend logic, please submit pull requests to the frontend repository.

This repository (stable-diffusion.cpp) only vendors the frontend periodically. Changes from the frontend repo are synchronized:

  • approximately every 1–2 weeks, or
  • when there are major frontend updates

Because of this, frontend changes will not appear here immediately after being merged upstream.

Using an external frontend

By default, the server uses the embedded frontend generated during the build (gen_index_html.h).

You can also serve a custom frontend file instead of the embedded one by using:

--serve-html-path <path-to-index.html>

For example:

sd-server --serve-html-path ./index.html

In this case, the server will load and serve the specified index.html file instead of the embedded frontend. This is useful when:

  • developing or testing frontend changes
  • using a custom UI
  • avoiding rebuilding the binary after frontend modifications

Usage

For detailed command-line arguments, run:

./bin/sd-server -h

For completely black or white images or videos, NaNs, and the --linear-scale / --attn-scale startup options, see Troubleshooting.