mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-11-28 19:16:33 +00:00
A request that ran the machine out of memory ended in a 500 and left the
server on the configuration that had just failed, so every later request
of that size failed the same way. The context now belongs to a
ContextKeeper (examples/server/recovery.*) and every generation of the
OpenAI, sdapi and sdcpp endpoints goes through ContextKeeper::run().
On out_of_memory or backend (the codes of sd_get_last_error()) it frees
the model, loads it again with the next lighter profile of
max > balanced > lite > lite-disk > lite-cpu and runs the same request
again. Steps that would load the same configuration are skipped, a
device failure jumps to lite-cpu, and from lite on the VAE is decoded in
tiles whatever the request asked. --profile sets the top of the ladder.
After --degradation-cooldown seconds (300; 0 = never) the server climbs
back one profile at the start of the next request. A climb that fails
on its first use is undone and doubles the wait, up to an hour. If the
model cannot be loaded again, requests get 503 "unavailable" and the
load is retried no more than every 15 seconds. --disable-degradation
keeps the old behavior of answering with the error.
Failures are answered with 503 and Retry-After (out of memory, no
context), 400 (invalid_argument) or 500, with a body of
{"error", "code", "retry_after_seconds"}. Async jobs report the same
code in error.code, 429 carries Retry-After, and std::bad_alloc in a
handler becomes a 503. New endpoints: GET /sdcpp/v1/health (status,
profile, counters, last error, queue; it never waits for a running
generation) and GET /sdcpp/v1/resources (system memory and devices).
sd-server now exits with code 1 and a message when it cannot listen or
create its HTTP threads; the second used to end in std::terminate and
the first in a silent exit 0. The lite-disk profile clears --eager-load,
which read every weight into memory and so saved nothing.
test/server_recovery_test.cpp checks the ladder, the step downs, the
climb back, the reload after a failure, the HTTP answers and use from
several threads with a scripted library and clock (114 checks, also
under ASan/UBSan and TSan). Checked with a synthetic SD 1.5 model on
the CPU under address-space limits: a 384x384 request at 2400 MiB steps
down to lite-disk and succeeds, below 2200 MiB it answers 503 and the
server stays up, the climb back and a failed climb behave as described,
and nine parallel requests of the three kinds are all served. The server
and the whole engine built with ASan/UBSan went through the ladder, the
CPU memory-guard scenarios and injected read errors without a report.
Not checked: Windows, GPUs, real models.
Claude-Session: https://claude.ai/code/session_01EhZzDBkuuMp1jBf4WtEySC
|
||
|---|---|---|
| .. | ||
| api_robustness.cpp | ||
| file_handle_pool_test.cpp | ||
| fuzz_model_loader.py | ||
| pe_imports_test.cpp | ||
| profile_test.cpp | ||
| README.md | ||
| security_smoke.sh | ||
| server_recovery_test.cpp | ||
Test di robustezza e sicurezza (fork)
Script locali di questo fork per verificare che il motore fallisca in modo pulito con input ostili, senza crash né errori di memoria. Istruzioni complete in italiano: docs/integrazione_progetti.md, sezione "Sicurezza".
| Test | Cosa verifica | Come si esegue |
|---|---|---|
security_smoke.sh |
file modello ostili + opzioni CLI estreme → mai crash (exit < 128) | ./test/security_smoke.sh build/bin/sd-cli |
api_robustness.cpp |
API C: puntatori nulli, indici fuori range, buffer piccoli, stress multi-thread (con ASan/UBSan) | vedi intestazione del file |
pe_imports_test.cpp |
lettore della tabella di importazione dei file PE (diagnostica delle DLL mancanti su Windows): file validi, troncati e corrotti (con ASan/UBSan) | vedi intestazione del file |
file_handle_pool_test.cpp |
pool dei flussi aperti sui file modello: riuso, file modificato o sostituito, flussi in errore, limiti, uso concorrente (con ASan/UBSan e TSan) | vedi intestazione del file |
profile_test.cpp |
profili di risorse (--profile): scelta automatica da dimensione del modello e memoria, ordine dei livelli, opzioni esplicite che vincono sul profilo |
vedi intestazione del file |
server_recovery_test.cpp |
custode del contesto del server (examples/server/recovery.*): scala dei profili, discesa su memoria insufficiente, risalita dopo una pausa, ricarica dopo un errore, risposte HTTP 503/400/500 con Retry-After, uso da più thread. Non serve nessun modello: libreria e orologio sono simulati |
vedi intestazione del file |
fuzz_model_loader.py |
fuzzing a mutazione del caricamento modelli (safetensors/GGUF) | python3 test/fuzz_model_loader.py <sd-cli> --seconds 300 |
Per il fuzzing è consigliata una build con AddressSanitizer:
cmake -B build_asan -DCMAKE_BUILD_TYPE=RelWithDebInfo -DSD_WEBP=OFF -DSD_WEBM=OFF \
-DCMAKE_C_FLAGS=-fsanitize=address -DCMAKE_CXX_FLAGS=-fsanitize=address \
-DCMAKE_EXE_LINKER_FLAGS=-fsanitize=address
cmake --build build_asan -j --target sd-cli
python3 test/fuzz_model_loader.py build_asan/bin/sd-cli --seconds 600
Gli input che causano una scoperta vengono salvati in test/fuzz_findings/
(da non committare). Questi test riducono il rischio ma non dimostrano
l'assenza di vulnerabilità.