Rewrite Semaphore.acquire/release#271
Conversation
Add a failing test for original Semaphore: when a ready waiter be cancelled, the other pending waiters will not aware of it.
…s abundant, no matter whether there is any older acquire().
… Semaphore. Add a failing test for the bug.
…aphore. Remove old tests violating the semantic of the semaphore.
|
Following the discussion of #269, the behavior of
Now the implementation of I am still thinking over
Will |
|
I need a little time to review this. I think you're on the right track though! |
|
I found that my comment above is wrong.
Anyway, I should not say " |
There was a problem hiding this comment.
Can we make this <= 0? I know it could never be < 0, but somehow it still gives me the willies imagining that if somehow it could become < 0 and we'd never recover.
There was a problem hiding this comment.
Ok! I think we could
- change the condition to
<= 0and add comment above it. - leave the condition be == 0 and add an assertion in the while loop
Maybe the second choice can make programmers aware of the error. I'm not sure.
There was a problem hiding this comment.
No, I just want <= 0 and no comment. :-)
|
Thanks! I've added some nits, let me know when you've addressed them and I'll merge this baby. |
|
Oh, one more thing. You need to have an account on bugs.python.org and you need to sign the online PSF contributor form: https://www.python.org/psf/contrib/contrib-form/ I will then check your status in the bug tracker so we know you've signed it. (This only needs to happen for your first contribution to Python. :-) |
|
@gvanrossum Thank you very much for the review! |
|
Oh and I think that when a pending task being deleted (raise |
|
The On the other hand, the document says
A pending task will be awoken by a callback function I am not sure whether or not "the spirit of |
|
We certainly can trust that when we pass |
|
Applied cc20f39. I did a manual merge, so you may have to mark the PR as applied manually. Thanks!! |
|
I apologize for sticking to this issue for several days. I want to make sure whether or not "the exceptions other than
I think that a generator G (or a coroutine) calling When I was testing This is why I think getting the
Do I just leave a comment on this PR, or are there other steps I miss? |
|
Here's a way that GeneratorExit can be caused: from asyncio import coroutine, sleep, ensure_future, get_event_loop
@coroutine
def foo():
try:
yield from sleep(1)
except BaseException as err:
print('hoho')
@coroutine
def bar():
coro = foo()
task = ensure_future(coro)
yield from sleep(0.1)
coro.close()
get_event_loop().run_until_complete(bar())However I don't think that this is a very realistic scenario. Another scenario is when you receive a KeyboardInterrupt. Since that is a BaseException, it barrels right past Looking back at it I think that's probably a mistake -- when we use try/finally, the finally action definition happens even when a BaseException is raised, but more explicit cleanup in PS. I think just closing the issue was enough to take the PR off the list. |
|
The thought that "calling I think I'm too naive to think up these things relating to |
|
It is wrong to do that. But if we can write the code robustly, so that we don't care about wrong things people do, without making it unreadable or obfuscated or slower, we should write the code robustly. And I think that's what we've accomplished. Do you agree? |
|
Yes, I do. I learned a lot from the discussion! Thank you for all your help and guidance! |
For #270
Add a failing test for original Semaphore: when a ready waiter be cancelled, the other pending waiters will not aware of it.
test_acquirein tests/test_locks.py assumes that if task A callssemaphore.acquire()before task B, A will acquire the semaphore before B. I useself._num_readyto make sure that if there is any awoken waiter, the newacquirewill not cut in line.test_acquire_cancelin tests/test_locks.py doesn't allowSemaphoreto leaving cancelled waiters inSemaphore._waiters.I'm not sure whether or not these two tests should be modified.