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: wait_for(to_thread)) does not work as expected. Extra documentation or fix needed.
Type: behavior Stage:
Components: asyncio Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, synchronizing, yselivanov
Priority: normal Keywords:

Created on 2021-01-25 05:32 by synchronizing, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg385602 - (view) Author: Felipe Faria (synchronizing) Date: 2021-01-25 05:32
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.
msg415415 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2022-03-17 15:09
Threads are not interruptable in Python.
The same for concurrent.future executor.
asyncio and `wait_for` is not special.

I'm not sure should we document it and *if* yes -- where should we do it?

Adding a note to every function could be cumbersome.
History
Date User Action Args
2022-04-11 14:59:40adminsetgithub: 87185
2022-03-17 15:09:05asvetlovsetmessages: + msg415415
2021-01-25 05:32:51synchronizingcreate