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.

Author synchronizing
Recipients asvetlov, synchronizing, yselivanov
Date 2021-01-25.05:32:50
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1611552771.11.0.601335924509.issue43019@roundup.psfhosted.org>
In-reply-to
Content
Consider the following:

```
import asyncio
import time

async def async_test():
    while True:
        await asyncio.sleep(1)
        print("in here - async")

def sync_test():
    while True:
        time.sleep(1)
        print("in here - sync")

async def main():
    async def tryexcept(func):
        try:
            await func
        except asyncio.exceptions.TimeoutError:
            print("error thrown")

    await tryexcept(asyncio.wait_for(async_test(), timeout=5))
    await tryexcept(asyncio.wait_for(asyncio.to_thread(sync_test), timeout=5))
    print("back in the main thread")

asyncio.run(main())
```

The above will lead to:

```
in here - async
error thrown
in here - sync
error thrown
back in the main thread
in here - sync
in here - sync
in here - sync
[... continues on forever ...]
```

It seems that the new thread created by `to_thread` is never cancelled and continues running forever despite the timeout error thrown. This might lead to some unwarranted (and hidden) behavior depending on the application.

Frankly, I'm unsure if a possible bug fix is even viable as from my knowledge cancelling threads in Python is not recommended. However, if not, I think a documentation update would be helpful. 

On my end messing with an existing sync library + `to_thread` + `wait_for` led me to believe that the execution had been cancelled, when instead it kept on shooting unexpected web requests. 

Thank you.
History
Date User Action Args
2021-01-25 05:32:51synchronizingsetrecipients: + synchronizing, asvetlov, yselivanov
2021-01-25 05:32:51synchronizingsetmessageid: <1611552771.11.0.601335924509.issue43019@roundup.psfhosted.org>
2021-01-25 05:32:51synchronizinglinkissue43019 messages
2021-01-25 05:32:50synchronizingcreate