feat: cancel task api to return more accurate response #6550

Closed
opened 2025-11-11 16:59:17 -06:00 by GiteaMirror · 1 comment
Owner

Originally created by @hassan-ajek on GitHub (Sep 30, 2025).

Check Existing Issues

  • I have searched the existing issues and discussions.

Problem Description

Calling the POST /api/tasks/stop/{task_id} is returning the following (at least locally when there's no redis)

return {"status": False, "message": f"Failed to stop task {task_id}."}

Desired Solution you'd like

I suppose status is always true, and the difference is the message

Alternatives Considered

 try:
        await asyncio.wait_for(task, timeout=0.1)
    except (asyncio.CancelledError, asyncio.TimeoutError):
        pass # Expected - cancel or enforced timeout was hit
    except Exception as e:
        print(f"Task {task_id} raised exception during cancellation: {e}")
    finally:
        tasks.pop(task_id, None)

    if task.cancelled() or task.done(): # For some reason cancelled is False but done is True
        return {"status": True, "message": f"Task {task_id} successfully cancelled."}
    else:
        return {"status": True, "message": f"Cancellation requested for {task_id}."}

Additional Context

From the chat UI upon sending a prompt and after starting to get tokens in the assistant message, when clicking Stop button calls this API and noticed Failed to stop task message with status false.

Originally created by @hassan-ajek on GitHub (Sep 30, 2025). ### Check Existing Issues - [x] I have searched the existing issues and discussions. ### Problem Description Calling the `POST /api/tasks/stop/{task_id}` is returning the following (at least locally when there's no redis) `return {"status": False, "message": f"Failed to stop task {task_id}."}` ### Desired Solution you'd like I suppose status is always true, and the difference is the message ### Alternatives Considered ```py try: await asyncio.wait_for(task, timeout=0.1) except (asyncio.CancelledError, asyncio.TimeoutError): pass # Expected - cancel or enforced timeout was hit except Exception as e: print(f"Task {task_id} raised exception during cancellation: {e}") finally: tasks.pop(task_id, None) if task.cancelled() or task.done(): # For some reason cancelled is False but done is True return {"status": True, "message": f"Task {task_id} successfully cancelled."} else: return {"status": True, "message": f"Cancellation requested for {task_id}."} ``` ### Additional Context From the chat UI upon sending a prompt and after starting to get tokens in the assistant message, when clicking `Stop` button calls this API and noticed Failed to stop task message with status false.
Author
Owner

@tjbck commented on GitHub (Oct 2, 2025):

Addressed in dev!

@tjbck commented on GitHub (Oct 2, 2025): Addressed in dev!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#6550