Commit 82c19c8 for stable-diffusion.cpp
commit 82c19c8fee0b3b5a754bfcb471c55fce3b2b561e
Author: leejet <leejet714@gmail.com>
Date: Wed Nov 25 22:11:33 2026 +0800
feat(server): support cancelling DGL jobs (#2420)
diff --git a/examples/server/api.md b/examples/server/api.md
index 6870769..51eddb1 100644
--- a/examples/server/api.md
+++ b/examples/server/api.md
@@ -1839,6 +1839,7 @@ the working directory's ./dgl directory unless --dgl-dir is set.
| POST /sdcpp/v1/dgl/script | Saves UTF-8 source with the given name. Pass previous_source to update an existing script. A conflict returns HTTP 409. |
| POST /sdcpp/v1/dgl/jobs | Queues the source string and returns a job ID with HTTP 202 |
| GET /sdcpp/v1/dgl/jobs/{id} | Returns the job status, error or result with tensor descriptions and inline encoded media |
+| POST /sdcpp/v1/dgl/jobs/{id}/cancel | Cancels a queued job or requests cancellation of a running job. Send `Content-Type: application/json`. |
| GET /sdcpp/v1/dgl/jobs/{id}/outputs/{index}?frame=0 | Returns a PNG of an image output or a video frame. Output and frame indexes start at zero. |
Failed jobs report `error.code` and `error.message`. When execution reports
@@ -1849,6 +1850,12 @@ Scripts share the component cache and execution settings used for generation.
Model reads are restricted to the configured model directories. Script writes
are restricted to output/ in the server's working directory.
+Cancellation returns the current job status with HTTP 200. A queued job
+becomes `cancelled` immediately. A running job sets the `cancel_requested`
+flag. Poll the status endpoint until it reports `cancelled`. Execution stops
+at a DGL cancellation check, so an operation currently running may have to
+finish first. A terminal job returns its current status.
+
### Encoded DGL results
The existing job result has `type`, `outputs` and `media`. `outputs` retains
diff --git a/examples/server/async_jobs.cpp b/examples/server/async_jobs.cpp
index 671cbbe..ac9318f 100644
--- a/examples/server/async_jobs.cpp
+++ b/examples/server/async_jobs.cpp
@@ -10,6 +10,7 @@
#include <sstream>
#include <string_view>
#include <thread>
+#include <utility>
#include "common/log.h"
#include "common/media_io.h"
@@ -25,18 +26,24 @@ class JobCancellationMonitor {
public:
// The caller holds sd_ctx_mutex until this monitor has joined, keeping the
// context alive and preventing cancellation from reaching another request.
- JobCancellationMonitor(sd_ctx_t* ctx, const AsyncGenerationJob& job)
- : thread_([this, ctx, &job]() {
+ template <typename Cancel>
+ JobCancellationMonitor(const AsyncGenerationJob& job, Cancel cancel)
+ : thread_([this, &job, cancel = std::move(cancel)]() {
std::unique_lock<std::mutex> lock(mutex_);
while (!done_) {
if (job.cancel_requested.load()) {
- // Each generation pass resets the library flag, including ADetailer.
- sd_cancel_generation(ctx, SD_CANCEL_ALL);
+ cancel();
}
cv_.wait_for(lock, std::chrono::milliseconds(10), [this]() { return done_; });
}
}) {}
+ JobCancellationMonitor(sd_ctx_t* ctx, const AsyncGenerationJob& job)
+ : JobCancellationMonitor(job, [ctx]() {
+ // Each generation pass resets the library flag, including ADetailer.
+ sd_cancel_generation(ctx, SD_CANCEL_ALL);
+ }) {}
+
~JobCancellationMonitor() {
{
std::lock_guard<std::mutex> lock(mutex_);
@@ -233,6 +240,7 @@ bool cancel_queued_job(AsyncJobManager& manager, AsyncGenerationJob& job) {
manager.queue.erase(new_end, manager.queue.end());
job.model.reset();
job.request_body = nullptr;
+ job.dgl_source.clear();
job.cancel_requested.store(true);
job.status = AsyncJobStatus::Cancelled;
job.completed_at = unix_timestamp_now_ms();
@@ -689,6 +697,11 @@ static sd_status_t execute_dgl_job(ServerRuntime& runtime,
sd_dgl_result_t* raw_result = nullptr;
{
std::lock_guard<std::mutex> lock(*runtime.sd_ctx_mutex);
+ if (job.cancel_requested.load())
+ return false;
+ JobCancellationMonitor cancellation(job, [ctx = runtime.sd_ctx]() {
+ sd_dgl_ctx_cancel(sd_ctx_get_dgl_ctx(ctx));
+ });
update_job_progress(job, "executing", "dgl");
const auto status = sd_dgl_run(sd_ctx_get_dgl_ctx(runtime.sd_ctx), source.c_str(), nullptr, 0, &raw_result);
if (status != SD_STATUS_OK) {
diff --git a/examples/server/routes_dgl.cpp b/examples/server/routes_dgl.cpp
index 7290512..803a0b0 100644
--- a/examples/server/routes_dgl.cpp
+++ b/examples/server/routes_dgl.cpp
@@ -259,6 +259,23 @@ void register_dgl_endpoints(httplib::Server& svr, ServerRuntime& runtime) {
send_json(res, 200, make_async_job_json(manager, *job));
}));
+ svr.Post(R"(/sdcpp/v1/dgl/jobs/([A-Za-z0-9_-]+)/cancel)", local_dgl_route([&runtime](const Request& req, Response& res) {
+ auto& manager = *runtime.async_job_manager;
+ std::lock_guard<std::mutex> lock(manager.mutex);
+ auto job = find_dgl_job(manager, req.matches[1].str(), res);
+ if (!job)
+ return;
+ if (job->status == AsyncJobStatus::Queued) {
+ if (!cancel_queued_job(manager, *job)) {
+ send_error(res, 409, "Job queue state changed before cancellation");
+ return;
+ }
+ } else if (job->status == AsyncJobStatus::Generating) {
+ job->cancel_requested.store(true);
+ }
+ send_json(res, 200, make_async_job_json(manager, *job));
+ }));
+
svr.Get(R"(/sdcpp/v1/dgl/jobs/([A-Za-z0-9_-]+)/outputs/([0-9]+))", local_dgl_route([&runtime](const Request& req, Response& res) {
auto& manager = *runtime.async_job_manager;
std::shared_ptr<sd_dgl_result_t> result;