[issue43019] wait_for(to_thread)) does not work as expected. Extra documentation or fix needed.

2022-03-17 Thread Andrew Svetlov


Andrew Svetlov  added the comment:

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.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43019] wait_for(to_thread)) does not work as expected. Extra documentation or fix needed.

2021-01-24 Thread Felipe Faria


New submission from Felipe Faria :

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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com