Interrupt AUTO1111 gens on cancel

7e4abaabff3e130ab1e50c8b16842a6b271c5a5f

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +13 -0Ignore whitespace
src/endpoints/stable-diffusion.js+13 -0
@@ -323,6 +323,17 @@ router.post('/generate', jsonParser, async (request, response) => {
323323 const url = new URL(request.body.url);
324324 url.pathname = '/sdapi/v1/txt2img';
325325
326+ const controller = new AbortController();
327+ request.socket.removeAllListeners('close');
328+ request.socket.on('close', function () {
329+ if (!response.writableEnded) {
330+ const url = new URL(request.body.url);
331+ url.pathname = '/sdapi/v1/interrupt';
332+ fetch(url, { method: 'POST', headers: { 'Authorization': getBasicAuthHeader(request.body.auth) } });
333+ }
334+ controller.abort();
335+ });
336+
326337 const result = await fetch(url, {
327338 method: 'POST',
328339 body: JSON.stringify(request.body),
@@ -331,6 +342,8 @@ router.post('/generate', jsonParser, async (request, response) => {
331342 'Authorization': getBasicAuthHeader(request.body.auth),
332343 },
333344 timeout: 0,
345+ // @ts-ignore
346+ signal: controller.signal,
334347 });
335348
336349 if (!result.ok) {