Skip to content

修复 TaskWrapper.throw 方法以支持取消任务时传递异常#221

Open
tingQian wants to merge 5 commits intomosquito:masterfrom
tingQian:feature/abc-docs-update
Open

修复 TaskWrapper.throw 方法以支持取消任务时传递异常#221
tingQian wants to merge 5 commits intomosquito:masterfrom
tingQian:feature/abc-docs-update

Conversation

@tingQian
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown
Author

@tingQian tingQian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修复 TaskWrapper.throw 方法以支持取消任务时传递异常

@mosquito
Copy link
Copy Markdown
Owner

Could you please add some tests reproduces this problem?

@tingQian
Copy link
Copy Markdown
Author

@tingQian ➜ /workspaces/aiormq (feature/abc-docs-update) $ poetry run pytest tests/test_taskwrapper_throw.py -s
============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.12.1, pytest-9.0.1, pluggy-1.6.0
rootdir: /workspaces/aiormq
configfile: pyproject.toml
plugins: aiomisc-pytest-1.3.4, asyncio-1.3.0
asyncio: mode=Mode.STRICT, debug=False, asyncio_default_fixture_loop_scope=None, asyncio_default_test_loop_scope=function
collected 3 items

tests/test_taskwrapper_throw.py
test_throw_with_instance 1 <ExceptionInfo CancelledError(RuntimeError('boom')) tblen=2>

test_throw_with_instance 2 <ExceptionInfo RuntimeError('boom') tblen=2>
.
test_throw_with_type <ExceptionInfo CancelledError(<class 'RuntimeError'>) tblen=2>
.
test_throw_with_cancellederror 1 <ExceptionInfo CancelledError(CancelledError()) tblen=2>

test_throw_with_cancellederror 2 <ExceptionInfo CancelledError() tblen=2>
.

@tingQian
Copy link
Copy Markdown
Author

这是问题分析
TaskWrapper.throw 方法(当前实现)

aiormq/abc.py:TaskWrapper.throw()

def throw(self, exception: ExceptionType) -> None:
self._exception = exception # 保存原始异常
self.task.cancel() # ❌ 问题:没有传递异常信息!
TaskWrapper 的异常替换机制

aiormq/abc.py:TaskWrapper.__inner()

async def __inner(self) -> Any:
try:
return await self.task
except asyncio.CancelledError as e:
raise self._exception from e # 只有在 await task_wrapper 时才会替换

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants