Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

asyncio.run_coroutine_threadsafe drops specified exceptions' traceback #117459

Closed
Labels
type-bugAn unexpected behavior, bug, or error
@rsp4jack

Description

@rsp4jack

Bug report

Bug description:

importasyncioimportthreadingimporttracebackasyncdefraiseme():raiseValueError(42)asyncdefraiseme2():raiseasyncio.TimeoutError()loop=asyncio.new_event_loop()asyncio.set_event_loop(loop)thr=threading.Thread(target=loop.run_forever,daemon=True)thr.start()print('raiseme() run_coroutine_threadsafe')try:task=asyncio.run_coroutine_threadsafe(raiseme(),loop)task.result()except:traceback.print_exc()print('raiseme2() run_coroutine_threadsafe')try:task=asyncio.run_coroutine_threadsafe(raiseme2(),loop)task.result()except:traceback.print_exc()
raiseme() run_coroutine_threadsafeTraceback (most recent call last):  File "g:\Projects\NowPlaying\test.py", line 18, in <module>    task.result()  File "C:\Program Files\Python312\Lib\concurrent\futures\_base.py", line 456, in result    return self.__get_result()           ^^^^^^^^^^^^^^^^^^^  File "C:\Program Files\Python312\Lib\concurrent\futures\_base.py", line 401, in __get_result    raise self._exception  File "g:\Projects\NowPlaying\test.py", line 6, in raiseme    raise ValueError(42)ValueError: 42raiseme2() run_coroutine_threadsafeTraceback (most recent call last):  File "g:\Projects\NowPlaying\test.py", line 25, in <module>    task.result()  File "C:\Program Files\Python312\Lib\concurrent\futures\_base.py", line 456, in result    return self.__get_result()           ^^^^^^^^^^^^^^^^^^^  File "C:\Program Files\Python312\Lib\concurrent\futures\_base.py", line 401, in __get_result    raise self._exceptionTimeoutError

The traceback of the second exception (TimeoutError) is dropeed.

The reason is that_convert_future_exc drops the origin exception's traceback:

def_convert_future_exc(exc):
exc_class=type(exc)
ifexc_classisconcurrent.futures.CancelledError:
returnexceptions.CancelledError(*exc.args)
elifexc_classisconcurrent.futures.TimeoutError:
returnexceptions.TimeoutError(*exc.args)
elifexc_classisconcurrent.futures.InvalidStateError:
returnexceptions.InvalidStateError(*exc.args)
else:
returnexc

To fix it, construct the new exception with the original traceback like that:

returnexceptions.CancelledError(*exc.args).with_traceback(exc.__traceback__)

CPython versions tested on:

3.10, CPython main branch

Operating systems tested on:

Linux, Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp