New submission from Hynek Schlawack <h...@ox.cx>:

This is something I've been procrastinating on for almost a year and working 
around it using my own version of asyncio.Condition because I wasn't sure how 
to describe it. So here's my best take:

Consider the following code:

```
import asyncio


async def tf(con):
    async with con:
        await asyncio.wait_for(con.wait(), 60)


async def f(loop):
    con = asyncio.Condition()

    t = loop.create_task(tf(con))

    await asyncio.sleep(1)
    t.cancel()

    async with con:
        con.notify_all()

    await t


loop = asyncio.get_event_loop()
loop.run_until_complete(f(loop))
```

(I'm using old-school APIs because I wanted to verify whether it was a 
regression. I ran into the bug with new-style APIs: 
https://gist.github.com/hynek/387f44672722171c901b8422320e8f9b)

`await t` will crash with:


```
Traceback (most recent call last):
  File "/Users/hynek/t.py", line 6, in tf
    await asyncio.wait_for(con.wait(), 60)
  File 
"/Users/hynek/.asdf/installs/python/3.9.0/lib/python3.9/asyncio/tasks.py", line 
466, in wait_for
    await waiter
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/hynek/t.py", line 24, in <module>
    loop.run_until_complete(f(loop))
  File 
"/Users/hynek/.asdf/installs/python/3.9.0/lib/python3.9/asyncio/base_events.py",
 line 642, in run_until_complete
    return future.result()
  File "/Users/hynek/t.py", line 20, in f
    await t
  File "/Users/hynek/t.py", line 6, in tf
    await asyncio.wait_for(con.wait(), 60)
  File 
"/Users/hynek/.asdf/installs/python/3.9.0/lib/python3.9/asyncio/locks.py", line 
20, in __aexit__
    self.release()
  File 
"/Users/hynek/.asdf/installs/python/3.9.0/lib/python3.9/asyncio/locks.py", line 
146, in release
    raise RuntimeError('Lock is not acquired.')
RuntimeError: Lock is not acquired.

```

If you replace wait_for with a simple await, it works and raises an 
asyncio.exceptions.CancelledError:

```
Traceback (most recent call last):
  File "/Users/hynek/t.py", line 6, in tf
    await con.wait()
  File 
"/Users/hynek/.asdf/installs/python/3.9.0/lib/python3.9/asyncio/locks.py", line 
290, in wait
    await fut
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/hynek/t.py", line 20, in f
    await t
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/hynek/t.py", line 24, in <module>
    loop.run_until_complete(f(loop))
  File 
"/Users/hynek/.asdf/installs/python/3.9.0/lib/python3.9/asyncio/base_events.py",
 line 642, in run_until_complete
    return future.result()
asyncio.exceptions.CancelledError
```

I have verified, that this has been broken at least since 3.5.10. The current 
3.10.0a3 is affected too.

----------
components: asyncio
messages: 382732
nosy: asvetlov, hynek, lukasz.langa, yselivanov
priority: normal
severity: normal
status: open
title: Cancelling tasks waiting for asyncio.Conditions crashes w/ RuntimeError: 
Lock is not acquired.
versions: Python 3.10, Python 3.8, Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue42600>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to