Skip to content

Commit 9e4def5

Browse files
committed
pbio/task: don't run completed task on cancel
This adds a check to avoid canceling an already completed task. Otherwise, it was possible that running one iteration of the protothread could actually restart the task.
1 parent 1648303 commit 9e4def5

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

lib/pbio/src/task.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ void pbio_task_init(pbio_task_t *task, pbio_task_thread_t thread, void *context)
3030
* Runs the task protothread until the next yield.
3131
* @param [in] task The task.
3232
* @returns True if the protothread has completed, otherwise false.
33+
*
34+
* Do not call this on a completed task.
3335
*/
3436
bool pbio_task_run_once(pbio_task_t *task) {
37+
assert(task->status == PBIO_ERROR_AGAIN);
38+
3539
if (PT_SCHEDULE(task->thread(&task->pt, task))) {
3640
return false;
3741
}
@@ -43,10 +47,14 @@ bool pbio_task_run_once(pbio_task_t *task) {
4347
}
4448

4549
/**
46-
* Cancels @p task and runs one iteration.
50+
* Cancels @p task and runs one iteration unless the task is already complete.
4751
* @param [in] task The task.
4852
*/
4953
void pbio_task_cancel(pbio_task_t *task) {
54+
if (task->status != PBIO_ERROR_AGAIN) {
55+
return;
56+
}
57+
5058
task->cancel = true;
5159
pbio_task_run_once(task);
5260
}

0 commit comments

Comments
 (0)