mirror of
https://github.com/hibiken/asynq.git
synced 2026-04-25 23:15:51 +03:00
[GH-ISSUE #532] [BUG] Canceled task goes into retry state #252
Labels
No labels
CLI
bug
designing
documentation
duplicate
enhancement
good first issue
good first issue
help wanted
idea
invalid
investigate
needs-more-info
performance
pr-welcome
pull-request
question
wontfix
work in progress
work in progress
work-around-available
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/asynq#252
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @patriceshot on GitHub (Sep 1, 2022).
Original GitHub issue: https://github.com/hibiken/asynq/issues/532
Originally assigned to: @hibiken on GitHub.
Describe the bug
Canceled task goes into retry state, and is consumed again.
If I return a Skip retry error, it is ignored.
To Reproduce
Steps to reproduce the behavior (Code snippets if applicable):
Expected behavior
A canceled task should be archived or completed, we do not want it to be consumed again.
Code sample
Here's the code of my handler (the rest of the setup is standard):
Environment (please complete the following information):
asynqpackage: v0.23.0Additional context
When I watch the error in processor.go, handleFailedMessage, the error is "Context canceled", even if I return a "skip retry" error in my task, it is ignored. Naively, I would add "context canceled" to the condition to skip retry, but perhaps I'm missing something ?
@dxl3811051 commented on GitHub (Sep 14, 2022):
我也碰到了这个问题 我以为就我碰到了呢,我想把任务终止掉 而且设置了重试次数为0, 但是当我cancel task时 这个任务会重新启动,而且原来的task也没有终止掉 最终时跑了2个任务。。。
@linhbkhn95 commented on GitHub (Sep 21, 2022):
As I know based on
asynqcode, When a message is assigned to a worker, it also is created a cancel() function viadiagram like:
explain by code:
When you action
Cancel the task (either with inspector or asynqmon)thenasynqwill Publish msg toasynq:cancelAt the Subscriber will receive msg and action
cancel() func.Therefore,
asynqwill performcancelfunc => returncontext canceledincontinently instead ofSkipRetry. It means your's handler has not run to return theSkipRetryerror. => your msg don't move to archive status.I think it can it is the author's intention.
Please confirm for me if I miss understanding @hibiken
@dxl3811051 commented on GitHub (Sep 21, 2022):
why?
I terminated the mission, but the mission run again
@linhbkhn95 commented on GitHub (Sep 21, 2022):
I think the
task cancelinghere is to stop work at the moment and then action at another time. It mean status reflectRunning->Stop. NotRunningtoArchive@linhbkhn95 commented on GitHub (Sep 22, 2022):
if @dxl3811051 wants as your expected, I can update it into my repo. Then you can use it for now. We need to confirm the author to merge to this repo.
@dxl3811051 commented on GitHub (Sep 22, 2022):
thanks
@OAyomide commented on GitHub (Nov 29, 2022):
@linhbkhn95 how do you move to archive then? i tried cancelling the task but it doesnt work. it says i should use
CancelTaskand i cannot see reference to this anywhere.@linhbkhn95 commented on GitHub (Dec 3, 2022):
We cannot move directly from a normal task to an archive task. It only performs it when num of retry reach >= the limit. If you need it can improve at my repo instead of at this repo. @OAyomide
@hungtcs commented on GitHub (Aug 9, 2023):
+1 Same Issues, are there any existing solutions?
@agorman commented on GitHub (Feb 29, 2024):
I'm running into this issue as well. It would be trivial to write middleware that could check if the context has been cancelled and return an
asynq.SkipRetry. However, the context may have been cancelled for other reasons. For example a timeout or a deadline.I propose that instead of using
context.CancelFuncto cancel the task internally you use acontext.CancelCauseFunc. That way we could usecontext.Cause(ctx)to see why it was cancelled and callasynq.SkipRetryaccordingly.Thoughts?
@agorman commented on GitHub (Mar 1, 2024):
After more investigation my solution above will not work. Asynq returns context.Cancelled error right away and does not wait for the task to finish or read it's error message. This was also explained above by @linhbkhn95 but I missed it.
I think it would be nice if we could have a mechanism to cancel and skip retry via the inspector.
@kamikazechaser commented on GitHub (Mar 3, 2024):
I don't think we can have a case where any context.Cancelled error pushes a task into archived state here. The potential side effects are just too many.
Ideally we want something in the
handleFailedMessagethat either reads the task message metadata (set immediately before signalling context cancellation) or another error type.@handsomefox commented on GitHub (Jul 31, 2024):
As a temporary solution until the library itself solves this issue, I've ended up using this code to cancel tasks:
It might not work if you have no retry delay set, so use at your own risk.
@alexshopee commented on GitHub (Aug 24, 2024):
Suppose there is a long running task and it would not success anymore, then the best practice is to just cancel it without any retries to save the worker resources
@s-k-yx commented on GitHub (Sep 17, 2025):
github.com/hibiken/asynq@c327bc40a2/processor.go (L236C1-L255C5)problem is here, user expects to handle the context on their own, maybe
case <-ctx.Done():is not needed, user should check it in their own function, and return some error