Skip to content

Commit

Permalink
🐛 FIX: Task.cancel should not set state as EXCEPTED (#214)
Browse files Browse the repository at this point in the history
`asyncio.CancelledError` are generated when an async task is cancelled.
In python 3.7 it inherits from `Exception`,
whereas in python 3.8+ it inherits from `BaseException`.
This meant it python 3.7 it was being caught by the broad `except Exception`,
and setting the process state to EXCEPTED,
whereas in python 3.8+ it was being re-raised to the caller.
We now ensure in both versions it is re-raised.
  • Loading branch information
chrisjsewell authored Mar 8, 2021
1 parent db0bf60 commit 99f959b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions plumpy/process_states.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import asyncio
from enum import Enum
import sys
import traceback
Expand Down Expand Up @@ -232,6 +233,11 @@ async def execute(self) -> State: # type: ignore # pylint: disable=invalid-over
except Interruption:
# Let this bubble up to the caller
raise
except asyncio.CancelledError: # pylint: disable=try-except-raise
# note this re-raise is only required in python<=3.7,
# for python>=3.8 asyncio.CancelledError does not inherit from Exception,
# so will not be caught below
raise
except Exception: # pylint: disable=broad-except
excepted = self.create_state(ProcessState.EXCEPTED, *sys.exc_info()[1:])
return cast(State, excepted)
Expand Down
6 changes: 6 additions & 0 deletions plumpy/processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,12 @@ async def step(self) -> None:

except KeyboardInterrupt: # pylint: disable=try-except-raise
raise
except asyncio.CancelledError: # pylint: disable=try-except-raise
# note this re-raise is only required in python<=3.7,
# where asyncio.CancelledError == concurrent.futures.CancelledError
# it is encountered when the run_task is cancelled
# for python>=3.8 asyncio.CancelledError does not inherit from Exception, so will not be caught below
raise
except Exception: # pylint: disable=broad-except
# Overwrite the next state to go to excepted directly
next_state = self.create_state(process_states.ProcessState.EXCEPTED, *sys.exc_info()[1:])
Expand Down

0 comments on commit 99f959b

Please sign in to comment.