mirror of https://github.com/joan2937/pigpio
Address #189 ensuring socket locks are released
The code previous assume exception-free execution of critical blocks between lock acquire() and lock release(); however, in Python exceptions can be thrown in many situations which would then result in a dead-lock of the entire program using pigpio. This is resolved by using the acquire/try/finally/release pattern to ensure that the lock is always released, even when an exception occurs. Also addresses #186, but takes a slightly different approach by using RLock to handle the nested lock requirement, which overall should be safer because it handles additional situations that can cause a deadlock.
This commit is contained in:
parent
8996b7eaca
commit
fb081b7cf6
326
pigpio.py
326
pigpio.py
|
@ -860,7 +860,7 @@ class _socklock:
|
||||||
"""
|
"""
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.s = None
|
self.s = None
|
||||||
self.l = threading.Lock()
|
self.l = threading.RLock()
|
||||||
|
|
||||||
class error(Exception):
|
class error(Exception):
|
||||||
"""pigpio module exception"""
|
"""pigpio module exception"""
|
||||||
|
@ -970,7 +970,7 @@ def _u2i(uint32):
|
||||||
raise error(error_text(v))
|
raise error(error_text(v))
|
||||||
return v
|
return v
|
||||||
|
|
||||||
def _pigpio_command(sl, cmd, p1, p2, rl=True):
|
def _pigpio_command(sl, cmd, p1, p2):
|
||||||
"""
|
"""
|
||||||
Runs a pigpio socket command.
|
Runs a pigpio socket command.
|
||||||
|
|
||||||
|
@ -980,12 +980,14 @@ def _pigpio_command(sl, cmd, p1, p2, rl=True):
|
||||||
p2:= command parameter 2 (if applicable).
|
p2:= command parameter 2 (if applicable).
|
||||||
"""
|
"""
|
||||||
sl.l.acquire()
|
sl.l.acquire()
|
||||||
sl.s.send(struct.pack('IIII', cmd, p1, p2, 0))
|
try:
|
||||||
dummy, res = struct.unpack('12sI', sl.s.recv(_SOCK_CMD_LEN))
|
sl.s.send(struct.pack('IIII', cmd, p1, p2, 0))
|
||||||
if rl: sl.l.release()
|
dummy, res = struct.unpack('12sI', sl.s.recv(_SOCK_CMD_LEN))
|
||||||
|
finally:
|
||||||
|
sl.l.release()
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def _pigpio_command_ext(sl, cmd, p1, p2, p3, extents, rl=True):
|
def _pigpio_command_ext(sl, cmd, p1, p2, p3, extents):
|
||||||
"""
|
"""
|
||||||
Runs an extended pigpio socket command.
|
Runs an extended pigpio socket command.
|
||||||
|
|
||||||
|
@ -1003,9 +1005,11 @@ def _pigpio_command_ext(sl, cmd, p1, p2, p3, extents, rl=True):
|
||||||
else:
|
else:
|
||||||
ext.extend(x)
|
ext.extend(x)
|
||||||
sl.l.acquire()
|
sl.l.acquire()
|
||||||
sl.s.sendall(ext)
|
try:
|
||||||
dummy, res = struct.unpack('12sI', sl.s.recv(_SOCK_CMD_LEN))
|
sl.s.sendall(ext)
|
||||||
if rl: sl.l.release()
|
dummy, res = struct.unpack('12sI', sl.s.recv(_SOCK_CMD_LEN))
|
||||||
|
finally:
|
||||||
|
sl.l.release()
|
||||||
return res
|
return res
|
||||||
|
|
||||||
class _event_ADT:
|
class _event_ADT:
|
||||||
|
@ -2854,13 +2858,15 @@ class pi():
|
||||||
# process read failure
|
# process read failure
|
||||||
...
|
...
|
||||||
"""
|
"""
|
||||||
# Don't raise exception. Must release lock.
|
self.sl.l.acquire()
|
||||||
bytes = u2i(_pigpio_command(self.sl, _PI_CMD_I2CRK, handle, reg, False))
|
try:
|
||||||
if bytes > 0:
|
bytes = u2i(_pigpio_command(self.sl, _PI_CMD_I2CRK, handle, reg))
|
||||||
data = self._rxbuf(bytes)
|
if bytes > 0:
|
||||||
else:
|
data = self._rxbuf(bytes)
|
||||||
data = ""
|
else:
|
||||||
self.sl.l.release()
|
data = ""
|
||||||
|
finally:
|
||||||
|
self.sl.l.release()
|
||||||
return bytes, data
|
return bytes, data
|
||||||
|
|
||||||
def i2c_block_process_call(self, handle, reg, data):
|
def i2c_block_process_call(self, handle, reg, data):
|
||||||
|
@ -2904,14 +2910,16 @@ class pi():
|
||||||
## extension ##
|
## extension ##
|
||||||
# s len data bytes
|
# s len data bytes
|
||||||
|
|
||||||
# Don't raise exception. Must release lock.
|
self.sl.l.acquire()
|
||||||
bytes = u2i(_pigpio_command_ext(
|
try:
|
||||||
self.sl, _PI_CMD_I2CPK, handle, reg, len(data), [data], False))
|
bytes = u2i(_pigpio_command_ext(
|
||||||
if bytes > 0:
|
self.sl, _PI_CMD_I2CPK, handle, reg, len(data), [data]))
|
||||||
data = self._rxbuf(bytes)
|
if bytes > 0:
|
||||||
else:
|
data = self._rxbuf(bytes)
|
||||||
data = ""
|
else:
|
||||||
self.sl.l.release()
|
data = ""
|
||||||
|
finally:
|
||||||
|
self.sl.l.release()
|
||||||
return bytes, data
|
return bytes, data
|
||||||
|
|
||||||
def i2c_write_i2c_block_data(self, handle, reg, data):
|
def i2c_write_i2c_block_data(self, handle, reg, data):
|
||||||
|
@ -2982,14 +2990,16 @@ class pi():
|
||||||
# I count
|
# I count
|
||||||
extents = [struct.pack("I", count)]
|
extents = [struct.pack("I", count)]
|
||||||
|
|
||||||
# Don't raise exception. Must release lock.
|
self.sl.l.acquire()
|
||||||
bytes = u2i(_pigpio_command_ext(
|
try:
|
||||||
self.sl, _PI_CMD_I2CRI, handle, reg, 4, extents, False))
|
bytes = u2i(_pigpio_command_ext(
|
||||||
if bytes > 0:
|
self.sl, _PI_CMD_I2CRI, handle, reg, 4, extentse))
|
||||||
data = self._rxbuf(bytes)
|
if bytes > 0:
|
||||||
else:
|
data = self._rxbuf(bytes)
|
||||||
data = ""
|
else:
|
||||||
self.sl.l.release()
|
data = ""
|
||||||
|
finally:
|
||||||
|
self.sl.l.release()
|
||||||
return bytes, data
|
return bytes, data
|
||||||
|
|
||||||
def i2c_read_device(self, handle, count):
|
def i2c_read_device(self, handle, count):
|
||||||
|
@ -3013,14 +3023,16 @@ class pi():
|
||||||
(count, data) = pi.i2c_read_device(h, 12)
|
(count, data) = pi.i2c_read_device(h, 12)
|
||||||
...
|
...
|
||||||
"""
|
"""
|
||||||
# Don't raise exception. Must release lock.
|
self.sl.l.acquire()
|
||||||
bytes = u2i(
|
try:
|
||||||
_pigpio_command(self.sl, _PI_CMD_I2CRD, handle, count, False))
|
bytes = u2i(
|
||||||
if bytes > 0:
|
_pigpio_command(self.sl, _PI_CMD_I2CRD, handle, count))
|
||||||
data = self._rxbuf(bytes)
|
if bytes > 0:
|
||||||
else:
|
data = self._rxbuf(bytes)
|
||||||
data = ""
|
else:
|
||||||
self.sl.l.release()
|
data = ""
|
||||||
|
finally:
|
||||||
|
self.sl.l.release()
|
||||||
return bytes, data
|
return bytes, data
|
||||||
|
|
||||||
def i2c_write_device(self, handle, data):
|
def i2c_write_device(self, handle, data):
|
||||||
|
@ -3115,14 +3127,16 @@ class pi():
|
||||||
## extension ##
|
## extension ##
|
||||||
# s len data bytes
|
# s len data bytes
|
||||||
|
|
||||||
# Don't raise exception. Must release lock.
|
self.sl.l.acquire()
|
||||||
bytes = u2i(_pigpio_command_ext(
|
try:
|
||||||
self.sl, _PI_CMD_I2CZ, handle, 0, len(data), [data], False))
|
bytes = u2i(_pigpio_command_ext(
|
||||||
if bytes > 0:
|
self.sl, _PI_CMD_I2CZ, handle, 0, len(data), [data]))
|
||||||
data = self._rxbuf(bytes)
|
if bytes > 0:
|
||||||
else:
|
data = self._rxbuf(bytes)
|
||||||
data = ""
|
else:
|
||||||
self.sl.l.release()
|
data = ""
|
||||||
|
finally:
|
||||||
|
self.sl.l.release()
|
||||||
return bytes, data
|
return bytes, data
|
||||||
|
|
||||||
|
|
||||||
|
@ -3284,14 +3298,16 @@ class pi():
|
||||||
## extension ##
|
## extension ##
|
||||||
# s len data bytes
|
# s len data bytes
|
||||||
|
|
||||||
# Don't raise exception. Must release lock.
|
self.sl.l.acquire()
|
||||||
bytes = u2i(_pigpio_command_ext(
|
try:
|
||||||
self.sl, _PI_CMD_BSPIX, CS, 0, len(data), [data], False))
|
bytes = u2i(_pigpio_command_ext(
|
||||||
if bytes > 0:
|
self.sl, _PI_CMD_BSPIX, CS, 0, len(data), [data]))
|
||||||
data = self._rxbuf(bytes)
|
if bytes > 0:
|
||||||
else:
|
data = self._rxbuf(bytes)
|
||||||
data = ""
|
else:
|
||||||
self.sl.l.release()
|
data = ""
|
||||||
|
finally:
|
||||||
|
self.sl.l.release()
|
||||||
return bytes, data
|
return bytes, data
|
||||||
|
|
||||||
|
|
||||||
|
@ -3421,14 +3437,16 @@ class pi():
|
||||||
## extension ##
|
## extension ##
|
||||||
# s len data bytes
|
# s len data bytes
|
||||||
|
|
||||||
# Don't raise exception. Must release lock.
|
self.sl.l.acquire()
|
||||||
bytes = u2i(_pigpio_command_ext(
|
try:
|
||||||
self.sl, _PI_CMD_BI2CZ, SDA, 0, len(data), [data], False))
|
bytes = u2i(_pigpio_command_ext(
|
||||||
if bytes > 0:
|
self.sl, _PI_CMD_BI2CZ, SDA, 0, len(data), [data]))
|
||||||
data = self._rxbuf(bytes)
|
if bytes > 0:
|
||||||
else:
|
data = self._rxbuf(bytes)
|
||||||
data = ""
|
else:
|
||||||
self.sl.l.release()
|
data = ""
|
||||||
|
finally:
|
||||||
|
self.sl.l.release()
|
||||||
return bytes, data
|
return bytes, data
|
||||||
|
|
||||||
def event_trigger(self, event):
|
def event_trigger(self, event):
|
||||||
|
@ -3551,19 +3569,21 @@ class pi():
|
||||||
## extension ##
|
## extension ##
|
||||||
# s len data bytes
|
# s len data bytes
|
||||||
|
|
||||||
# Don't raise exception. Must release lock.
|
self.sl.l.acquire()
|
||||||
bytes = u2i(_pigpio_command_ext(
|
try:
|
||||||
self.sl, _PI_CMD_BSCX, bsc_control, 0, len(data), [data], False))
|
bytes = u2i(_pigpio_command_ext(
|
||||||
if bytes > 0:
|
self.sl, _PI_CMD_BSCX, bsc_control, 0, len(data), [data]))
|
||||||
rx = self._rxbuf(bytes)
|
if bytes > 0:
|
||||||
status = struct.unpack('I', rx[0:4])[0]
|
rx = self._rxbuf(bytes)
|
||||||
bytes -= 4
|
status = struct.unpack('I', rx[0:4])[0]
|
||||||
data = rx[4:]
|
bytes -= 4
|
||||||
else:
|
data = rx[4:]
|
||||||
status = bytes
|
else:
|
||||||
bytes = 0
|
status = bytes
|
||||||
data = bytearray(b'')
|
bytes = 0
|
||||||
self.sl.l.release()
|
data = bytearray(b'')
|
||||||
|
finally:
|
||||||
|
self.sl.l.release()
|
||||||
return status, bytes, data
|
return status, bytes, data
|
||||||
|
|
||||||
def bsc_i2c(self, i2c_address, data=[]):
|
def bsc_i2c(self, i2c_address, data=[]):
|
||||||
|
@ -3818,14 +3838,16 @@ class pi():
|
||||||
# error path
|
# error path
|
||||||
...
|
...
|
||||||
"""
|
"""
|
||||||
# Don't raise exception. Must release lock.
|
self.sl.l.acquire()
|
||||||
bytes = u2i(_pigpio_command(
|
try:
|
||||||
self.sl, _PI_CMD_SPIR, handle, count, False))
|
bytes = u2i(_pigpio_command(
|
||||||
if bytes > 0:
|
self.sl, _PI_CMD_SPIR, handle, count))
|
||||||
data = self._rxbuf(bytes)
|
if bytes > 0:
|
||||||
else:
|
data = self._rxbuf(bytes)
|
||||||
data = ""
|
else:
|
||||||
self.sl.l.release()
|
data = ""
|
||||||
|
finally:
|
||||||
|
self.sl.l.release()
|
||||||
return bytes, data
|
return bytes, data
|
||||||
|
|
||||||
def spi_write(self, handle, data):
|
def spi_write(self, handle, data):
|
||||||
|
@ -3882,14 +3904,16 @@ class pi():
|
||||||
## extension ##
|
## extension ##
|
||||||
# s len data bytes
|
# s len data bytes
|
||||||
|
|
||||||
# Don't raise exception. Must release lock.
|
self.sl.l.acquire()
|
||||||
bytes = u2i(_pigpio_command_ext(
|
try:
|
||||||
self.sl, _PI_CMD_SPIX, handle, 0, len(data), [data], False))
|
bytes = u2i(_pigpio_command_ext(
|
||||||
if bytes > 0:
|
self.sl, _PI_CMD_SPIX, handle, 0, len(data), [data]))
|
||||||
data = self._rxbuf(bytes)
|
if bytes > 0:
|
||||||
else:
|
data = self._rxbuf(bytes)
|
||||||
data = ""
|
else:
|
||||||
self.sl.l.release()
|
data = ""
|
||||||
|
finally:
|
||||||
|
self.sl.l.release()
|
||||||
return bytes, data
|
return bytes, data
|
||||||
|
|
||||||
def serial_open(self, tty, baud, ser_flags=0):
|
def serial_open(self, tty, baud, ser_flags=0):
|
||||||
|
@ -3988,14 +4012,16 @@ class pi():
|
||||||
# process read data
|
# process read data
|
||||||
...
|
...
|
||||||
"""
|
"""
|
||||||
# Don't raise exception. Must release lock.
|
self.sl.l.acquire()
|
||||||
bytes = u2i(
|
try:
|
||||||
_pigpio_command(self.sl, _PI_CMD_SERR, handle, count, False))
|
bytes = u2i(
|
||||||
if bytes > 0:
|
_pigpio_command(self.sl, _PI_CMD_SERR, handle, count))
|
||||||
data = self._rxbuf(bytes)
|
if bytes > 0:
|
||||||
else:
|
data = self._rxbuf(bytes)
|
||||||
data = ""
|
else:
|
||||||
self.sl.l.release()
|
data = ""
|
||||||
|
finally:
|
||||||
|
self.sl.l.release()
|
||||||
return bytes, data
|
return bytes, data
|
||||||
|
|
||||||
def serial_write(self, handle, data):
|
def serial_write(self, handle, data):
|
||||||
|
@ -4217,18 +4243,20 @@ class pi():
|
||||||
(s, pars) = pi.script_status(sid)
|
(s, pars) = pi.script_status(sid)
|
||||||
...
|
...
|
||||||
"""
|
"""
|
||||||
# Don't raise exception. Must release lock.
|
self.sl.l.acquire()
|
||||||
bytes = u2i(
|
try:
|
||||||
_pigpio_command(self.sl, _PI_CMD_PROCP, script_id, 0, False))
|
bytes = u2i(
|
||||||
if bytes > 0:
|
_pigpio_command(self.sl, _PI_CMD_PROCP, script_id, 0))
|
||||||
data = self._rxbuf(bytes)
|
if bytes > 0:
|
||||||
pars = struct.unpack('11i', _str(data))
|
data = self._rxbuf(bytes)
|
||||||
status = pars[0]
|
pars = struct.unpack('11i', _str(data))
|
||||||
params = pars[1:]
|
status = pars[0]
|
||||||
else:
|
params = pars[1:]
|
||||||
status = bytes
|
else:
|
||||||
params = ()
|
status = bytes
|
||||||
self.sl.l.release()
|
params = ()
|
||||||
|
finally:
|
||||||
|
self.sl.l.release()
|
||||||
return status, params
|
return status, params
|
||||||
|
|
||||||
def stop_script(self, script_id):
|
def stop_script(self, script_id):
|
||||||
|
@ -4308,14 +4336,16 @@ class pi():
|
||||||
(count, data) = pi.bb_serial_read(4)
|
(count, data) = pi.bb_serial_read(4)
|
||||||
...
|
...
|
||||||
"""
|
"""
|
||||||
# Don't raise exception. Must release lock.
|
self.sl.l.acquire()
|
||||||
bytes = u2i(
|
try:
|
||||||
_pigpio_command(self.sl, _PI_CMD_SLR, user_gpio, 10000, False))
|
bytes = u2i(
|
||||||
if bytes > 0:
|
_pigpio_command(self.sl, _PI_CMD_SLR, user_gpio, 10000))
|
||||||
data = self._rxbuf(bytes)
|
if bytes > 0:
|
||||||
else:
|
data = self._rxbuf(bytes)
|
||||||
data = ""
|
else:
|
||||||
self.sl.l.release()
|
data = ""
|
||||||
|
finally:
|
||||||
|
self.sl.l.release()
|
||||||
return bytes, data
|
return bytes, data
|
||||||
|
|
||||||
|
|
||||||
|
@ -4410,14 +4440,16 @@ class pi():
|
||||||
## extension ##
|
## extension ##
|
||||||
# s len argx bytes
|
# s len argx bytes
|
||||||
|
|
||||||
# Don't raise exception. Must release lock.
|
self.sl.l.acquire()
|
||||||
bytes = u2i(_pigpio_command_ext(
|
try:
|
||||||
self.sl, _PI_CMD_CF2, arg1, retMax, len(argx), [argx], False))
|
bytes = u2i(_pigpio_command_ext(
|
||||||
if bytes > 0:
|
self.sl, _PI_CMD_CF2, arg1, retMax, len(argx), [argx]))
|
||||||
data = self._rxbuf(bytes)
|
if bytes > 0:
|
||||||
else:
|
data = self._rxbuf(bytes)
|
||||||
data = ""
|
else:
|
||||||
self.sl.l.release()
|
data = ""
|
||||||
|
finally:
|
||||||
|
self.sl.l.release()
|
||||||
return bytes, data
|
return bytes, data
|
||||||
|
|
||||||
def get_pad_strength(self, pad):
|
def get_pad_strength(self, pad):
|
||||||
|
@ -4610,14 +4642,16 @@ class pi():
|
||||||
# process read data
|
# process read data
|
||||||
...
|
...
|
||||||
"""
|
"""
|
||||||
# Don't raise exception. Must release lock.
|
self.sl.l.acquire()
|
||||||
bytes = u2i(
|
try:
|
||||||
_pigpio_command(self.sl, _PI_CMD_FR, handle, count, False))
|
bytes = u2i(
|
||||||
if bytes > 0:
|
_pigpio_command(self.sl, _PI_CMD_FR, handle, count))
|
||||||
data = self._rxbuf(bytes)
|
if bytes > 0:
|
||||||
else:
|
data = self._rxbuf(bytes)
|
||||||
data = ""
|
else:
|
||||||
self.sl.l.release()
|
data = ""
|
||||||
|
finally:
|
||||||
|
self.sl.l.release()
|
||||||
return bytes, data
|
return bytes, data
|
||||||
|
|
||||||
def file_write(self, handle, data):
|
def file_write(self, handle, data):
|
||||||
|
@ -4716,14 +4750,16 @@ class pi():
|
||||||
## extension ##
|
## extension ##
|
||||||
# s len data bytes
|
# s len data bytes
|
||||||
|
|
||||||
# Don't raise exception. Must release lock.
|
self.sl.l.acquire()
|
||||||
bytes = u2i(_pigpio_command_ext(
|
try:
|
||||||
self.sl, _PI_CMD_FL, 60000, 0, len(fpattern), [fpattern], False))
|
bytes = u2i(_pigpio_command_ext(
|
||||||
if bytes > 0:
|
self.sl, _PI_CMD_FL, 60000, 0, len(fpattern), [fpattern]))
|
||||||
data = self._rxbuf(bytes)
|
if bytes > 0:
|
||||||
else:
|
data = self._rxbuf(bytes)
|
||||||
data = ""
|
else:
|
||||||
self.sl.l.release()
|
data = ""
|
||||||
|
finally:
|
||||||
|
self.sl.l.release()
|
||||||
return bytes, data
|
return bytes, data
|
||||||
|
|
||||||
def shell(self, shellscr, pstring=""):
|
def shell(self, shellscr, pstring=""):
|
||||||
|
|
Loading…
Reference in New Issue