[Python-ideas] Asynchronous exception handling around with/try statement borders

Nick Coghlan ncoghlan at gmail.com
Wed Jun 28 08:26:08 EDT 2017


On 28 June 2017 at 21:40, Erik Bray <erik.m.bray at gmail.com> wrote:
> My colleague's contention is that given
>
> lock = threading.Lock()
>
> this is simply *wrong*:
>
> lock.acquire()
> try:
>     do_something()
> finally:
>     lock.release()
>
> whereas this is okay:
>
> with lock:
>     do_something()

Technically both are slightly racy with respect to async signals (e.g.
KeyboardInterrupt), but the with statement form is less exposed to the
problem (since it does more of its work in single opcodes).

Nathaniel Smith posted a good write-up of the technical details to the
issue tracker based on his work with trio:
https://bugs.python.org/issue29988

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list