Interrupt Comfy gens on cancel

52497ea96ddec110091d7f3f50d72457e032f3db

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

1 files changed, +15 -0Ignore whitespace
src/endpoints/stable-diffusion.js+15 -0
@@ -569,6 +569,17 @@ comfy.post('/generate', jsonParser, async (request, response) => {
569569 const url = new URL(request.body.url);
570570 url.pathname = '/prompt';
571571
572+ const controller = new AbortController();
573+ request.socket.removeAllListeners('close');
574+ request.socket.on('close', function () {
575+ if (!response.writableEnded && !item) {
576+ const interruptUrl = new URL(request.body.url);
577+ interruptUrl.pathname = '/interrupt';
578+ fetch(interruptUrl, { method: 'POST', headers: { 'Authorization': getBasicAuthHeader(request.body.auth) } });
579+ }
580+ controller.abort();
581+ });
582+
572583 const promptResult = await fetch(url, {
573584 method: 'POST',
574585 body: request.body.prompt,
@@ -594,6 +605,9 @@ comfy.post('/generate', jsonParser, async (request, response) => {
594605 }
595606 await delay(100);
596607 }
608+ if (item.status.status_str === 'error') {
609+ throw new Error('ComfyUI generation did not succeed.');
610+ }
597611 const imgInfo = Object.keys(item.outputs).map(it => item.outputs[it].images).flat()[0];
598612 const imgUrl = new URL(request.body.url);
599613 imgUrl.pathname = '/view';
@@ -605,6 +619,7 @@ comfy.post('/generate', jsonParser, async (request, response) => {
605619 const imgBuffer = await imgResponse.buffer();
606620 return response.send(imgBuffer.toString('base64'));
607621 } catch (error) {
622+ console.log(error);
608623 return response.sendStatus(500);
609624 }
610625});