event: Make cancel() for regular tasks work like remove()

Fixes #1741
This commit is contained in:
rdb 2025-07-28 10:56:14 +02:00
parent ddcfee18b4
commit 5bc9b70e86
2 changed files with 18 additions and 2 deletions

View File

@ -503,7 +503,7 @@ cancel() {
--_chain->_num_awaiting_tasks;
return true;
}
else if (must_cancel || _fut_waiter != nullptr) {
else if (_generator != nullptr && (must_cancel || _fut_waiter != nullptr)) {
// We may be polling an external future, so we still need to throw a
// CancelledException and allow it to be caught.
if (must_cancel) {

View File

@ -133,13 +133,29 @@ def test_future_wait_cancel():
fut.result()
def test_task_cancel():
def test_task_remove():
task_mgr = core.AsyncTaskManager.get_global_ptr()
task = core.PythonTask(lambda task: task.done)
task_mgr.add(task)
assert not task.done()
task_mgr.remove(task)
assert not task.is_alive()
assert task.done()
assert task.cancelled()
with pytest.raises(CancelledError):
task.result()
def test_task_cancel():
task_mgr = core.AsyncTaskManager.get_global_ptr()
task = core.PythonTask(lambda task: task.done)
task_mgr.add(task)
assert not task.done()
task.cancel()
assert not task.is_alive()
assert task.done()
assert task.cancelled()