Skip to content

[Feature] Add abort/cancellation mechanism for interruptible generationΒ #1036

@shakfu

Description

@shakfu

Feature Summary

Currently there's no way to abort/cancel an in-progress generate_image() or generate_video() call from the calling code.

Detailed Description

Summary

Currently there's no way to abort/cancel an in-progress generate_image() or generate_video() call from the calling code. This makes it impossible to handle user interrupts (Ctrl+C) or implement timeouts in applications using the library.

Problem

The progress callback is defined as:

typedef void (*sd_progress_cb_t)(int step, int steps, float time, void* data);

Since it returns void, there's no way for the caller to signal "please stop" back to the library.

The underlying ggml library has an abort callback mechanism (ggml_abort_callback returning bool), but this isn't exposed through the stable-diffusion.cpp API.

Suggested Solutions

Any of these would work:

  1. Change progress callback to return bool - Return false to continue, true to abort:
typedef bool (*sd_progress_cb_t)(int step, int steps, float time, void* data);
  1. Add a separate abort callback - Expose ggml's mechanism:
typedef bool (*sd_abort_cb_t)(void* data);

SD_API void sd_set_abort_callback(sd_abort_cb_t cb, void* data);
  1. Add explicit cancel function - Set a flag that's checked during iteration:
SD_API void sd_cancel(sd_ctx_t* sd_ctx);

Use Case

I'm developing Python bindings for stable-diffusion.cpp. Without an abort mechanism, the only option is to kill the process.

Related user-reported issue

Alternatives you considered

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions