diff --git a/panda/src/event/pythonTask.cxx b/panda/src/event/pythonTask.cxx index d2515a9312..a978b9d613 100644 --- a/panda/src/event/pythonTask.cxx +++ b/panda/src/event/pythonTask.cxx @@ -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) { diff --git a/tests/event/test_futures.py b/tests/event/test_futures.py index c1bf870c1c..a54037bdc7 100644 --- a/tests/event/test_futures.py +++ b/tests/event/test_futures.py @@ -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()