[issue35391] threading.RLock exception handling while waiting

2021-08-28 Thread Antoine Pitrou


Change by Antoine Pitrou :


--
resolution:  -> out of date
stage:  -> resolved
status: open -> closed

___
Python tracker 

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



[issue35391] threading.RLock exception handling while waiting

2021-08-28 Thread Antoine Pitrou


Antoine Pitrou  added the comment:

RLock is implemented in C nowadays so this problem doesn't occur anymore:
https://github.com/python/cpython/blob/main/Modules/_threadmodule.c#L436-L459

You can of course, however, import the pure Python RLock under the name 
"_PyRLock", which is still vulnerable to this issue:
https://github.com/python/cpython/blob/main/Lib/threading.py#L204-L206

--
type: crash -> behavior
versions:  -Python 3.4, Python 3.5, Python 3.6, Python 3.7, Python 3.8

___
Python tracker 

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



[issue35391] threading.RLock exception handling while waiting

2021-08-28 Thread Irit Katriel


Irit Katriel  added the comment:

Have you reproduced the issue on 3.8?

I am unable to reproduce this on main (on a Mac). Adding Antoine since I think 
he fixed a few issues in this area.

--
nosy: +iritkatriel, pitrou

___
Python tracker 

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



[issue35391] threading.RLock exception handling while waiting

2018-12-03 Thread Omer Bartal


New submission from Omer Bartal :

(tested on python 2.7)

Created a threading.Condition(threading.RLock())
A piece of code acquires the lock using a with block and waits (for a notify)
While wait() is running a KeyboardInterrupt is raised
An exception is raised while exiting the lock's with block:
  File "/usr/lib/python2.7/threading.py", line 289, in __exit__
return self.__lock.__exit__(*args)
  File "/usr/lib/python2.7/threading.py", line 216, in __exit__
self.release()
  File "/usr/lib/python2.7/threading.py", line 204, in release
raise RuntimeError("cannot release un-acquired lock")

example code running on the main thread:
def waiting(lock): # (the lock was created using Condition(RLock()))
  with lock:
lock.wait(timeout=xxx) # while waiting a keyboard interrupt is raised

The issue is caused because threading.RLock doesn't handle the exception 
correctly:
def _acquire_restore(self, count_owner):
count, owner = count_owner
self.__block.acquire()
self.__count = count
self.__owner = owner
if __debug__:
self._note("%s._acquire_restore()", self)

The exception is raised after the acquire() returns and the count and owner are 
not restored.

The problem was solved using the following fix (added try, finally):
def _acquire_restore(self, count_owner):
count, owner = count_owner
try:
self.__block.acquire()
finally:
self.__count = count
self.__owner = owner
if __debug__:
self._note("%s._acquire_restore()", self)


Looking at the code, this issue exists in python 3.8 as well.

--
components: Library (Lib)
messages: 330972
nosy: Omer Bartal
priority: normal
severity: normal
status: open
title: threading.RLock exception handling while waiting
type: crash
versions: Python 2.7, Python 3.4, Python 3.5, Python 3.6, Python 3.7, Python 3.8

___
Python tracker 

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