This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: asyncio gather doesn't handle custom exceptions that inherit from BaseException
Type: behavior Stage: resolved
Components: asyncio, Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, cmermingas, iritkatriel, xtreak, yselivanov
Priority: normal Keywords:

Created on 2019-06-17 14:10 by cmermingas, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg345861 - (view) Author: Carlos Mermingas (cmermingas) Date: 2019-06-17 14:10
asyncio.gather doesn't handle custom exception exceptions that inherit from BaseException in the same manner that it handles those that inherit from Exception, regardless of whether return_exceptions is set to True or False.

In the example below, I am using return_exceptions=True. If the custom exception inherits from Exception, a list printed, as expected. Conversely, if the custom exception inherits from BaseException, it is propagated:


import asyncio


class CustomException(BaseException):  # It works if base class changed to Exception
    pass


async def do_this(x):
    if x == 5:
        raise CustomException()
    await asyncio.sleep(1)
    print(f'THIS DONE: {x}')


async def main():
    print('BEGIN')
    tasks = [do_this(x) for x in range(1, 11)]
    result = await asyncio.gather(*tasks, return_exceptions=True)
    print(f'Result: {result}')
    print('END')


asyncio.run(main())
msg345951 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-06-18 03:53
On master this returns the below output. Maybe this change in master was due to 431b540bf79f0982559b1b0e420b1b085f667bb7 (issue32528) that is available with 3.8+ ?

./python.exe ../backups/bpo37317.py
BEGIN
THIS DONE: 1
THIS DONE: 2
THIS DONE: 3
THIS DONE: 4
THIS DONE: 6
THIS DONE: 7
THIS DONE: 8
THIS DONE: 9
THIS DONE: 10
Result: [None, None, None, None, CustomException(), None, None, None, None, None]
END
msg381867 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-11-25 22:54
Thanks for reporting. This has been fixed in 3.8 and it's too late for 3.7.
History
Date User Action Args
2022-04-11 14:59:16adminsetgithub: 81498
2020-11-25 22:54:21iritkatrielsetstatus: open -> closed

nosy: + iritkatriel
messages: + msg381867

resolution: fixed
stage: resolved
2019-06-18 03:53:36xtreaksetnosy: + xtreak
messages: + msg345951
2019-06-17 14:40:21xtreaksetnosy: + asvetlov, yselivanov
components: + asyncio
2019-06-17 14:10:41cmermingascreate