New submission from Felipe Faria <[email protected]>:
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.
----------
components: asyncio
messages: 385602
nosy: asvetlov, felipefaria, yselivanov
priority: normal
severity: normal
status: open
title: wait_for(to_thread)) does not work as expected. Extra documentation or
fix needed.
type: behavior
versions: Python 3.9
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue43019>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com