[Python-ideas] Protecting finally clauses of interruptions

Paul Colomiets paul at colomiets.name
Tue Apr 3 00:24:24 CEST 2012


Hi Yury,

On Tue, Apr 3, 2012 at 1:20 AM, Yury Selivanov <yselivanov.ml at gmail.com> wrote:
> On 2012-04-02, at 6:00 PM, Paul Colomiets wrote:
>
>> Hi Yury,
>>
>>> On 2012-04-02, at 4:49 PM, Paul Colomiets wrote:
>>>> l.lock()
>>>> try:
>>>>   ...
>>>> finally:
>>>>   l.unlock()
>>>>
>>>> Which will break if you interrupted just after lock is acquired.
>>>
>>> I guess the best way to solve this puzzle, is to track all locks that
>>> the thread acquires and release them in case of forced interruption.
>>
>> Same with open files, and with all other kinds of contexts. I'd go
>> he route of making __enter__ also uninterruptable (and make timeout
>> inside a lock itself).
>
>
> I still don't get how exactly do you propose to handle sudden thread
> interruption in your own example:
>
> l.lock()
> # (!) the thread may be interrupted at this point
> try:
>   ...
> finally:
>   l.unlock()
>
> You don't have a 'with' statement here.
>

By wrapping lock into a context manager.

-- 
Paul



More information about the Python-ideas mailing list