feat(server): support cancelling DGL jobs (#2420)
Some checks are pending
CI / ubuntu-latest-cmake (push) Waiting to run
CI / ubuntu-latest-cmake-vulkan (push) Waiting to run
CI / Build and push container images (push) Waiting to run
CI / macOS-latest-cmake (push) Waiting to run
CI / windows-latest-cmake (cpu, -DGGML_NATIVE=OFF -DSD_BUILD_SHARED_LIBS=ON -DSD_BUILD_SHARED_GGML_LIB=ON -DGGML_BACKEND_DL=ON -DGGML_CPU_ALL_VARIANTS=ON) (push) Waiting to run
CI / windows-latest-cmake (cuda12, -DSD_CUDA=ON -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_CUDA_COMPILER_LAUNCHER=ccache -DSD_BUILD_SHARED_LIBS=ON -DCMAKE_CUDA_ARCHITECTURES='61;70;75;80;86;89;90;100;120' -DCMAKE_CUDA_FL… (push) Waiting to run
CI / windows-latest-cmake (vulkan, -DSD_VULKAN=ON -DSD_BUILD_SHARED_LIBS=ON -DGGML_NATIVE=OFF -DSD_BUILD_SHARED_GGML_LIB=ON -DGGML_BACKEND_DL=ON -DGGML_CPU_ALL_VARIANTS=ON) (push) Waiting to run
CI / windows-latest-rocm (push) Waiting to run
CI / ubuntu-latest-rocm (10.1.0, x64, gfx900;gfx906;gfx908;gfx90a;gfx90c;gfx942;gfx950;gfx1010;gfx1011;gfx1012;gfx1030;gfx1031;gfx1032;gfx1033;gfx1034;gfx1035;gfx1036;gfx1100;gfx1101;gfx1102;gfx1103;gfx1150;gfx1151;gfx1152;gfx1153;gfx1200;gfx1201) (push) Waiting to run
CI / release (push) Blocked by required conditions

This commit is contained in:
leejet 2026-11-25 22:11:33 +08:00 • committed by GitHub
parent f296cb047e
commit 82c19c8fee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 41 additions and 4 deletions

View file

@ -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

View file

@ -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) {

View file

@ -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;