I am trying to implement the following logic in Python3:
def f():
lock1.acquire()
task_protected_by_lock1() # Might acquire lock2 internally
lock2.acquire()
task_protected_by_lock1_and_lock2()
lock1.release()
task_protected_by_lock2() # Might acquire lock1 internally
lock2.release()
However, I found it impossible to correctly handle SIGINT because it will raise a KeyBoardInterrupt exception at random location. I need to guarantee that lock1 and lock2 are both released when control flow exits f()
(i.e. either normal return or unhandled exception).
I am aware that SIGINT can be temporarily masked. However, correctly restoring the mask becomes another challenge because it might already been masked from outside. Also, the tasks performed between locks might also tweak signal masks. I believe there has to be a better solution.
I am wondering if there exist a way for me to utilize context-manager (with
statement) to achieve it. I’ve considered the following, but none would work for my use case:
Approach 1 – single with
statement
def f():
with lock1, lock2:
task_protected_by_lock1() # Bad: acquiring lock2 internally will cause deadlock
task_protected_by_lock1_and_lock2() # Good
task_protected_by_lock2() # Bad: acquiring lock1 internally will cause deadlock
Approach 2 – nested with
statement
def f():
with lock1:
task_protected_by_lock1() # Good
with lock2:
task_protected_by_lock1_and_lock2() # Good
task_protected_by_lock2() # Bad: acquiring lock1 internally will cause deadlock
Approach 3 – manual lock management
def f():
flag1 = False
flag2 = False
try:
lock1.acquire()
# Bad: SIGINT might be raised here
flag1 = True
task_protected_by_lock1()
lock2.acquire()
# Bad: SIGINT might be raised here
flag2 = True
task_protected_by_lock1_and_lock2()
lock1.release()
# Bad: SIGINT might be raised here
flag1 = False
task_protected_by_lock2()
lock2.release()
# Bad: SIGINT might be raised here
flag2 = False
except Exception as e:
if flag1:
lock1.release()
if flag2:
lock2.release()
raise e
Approach 4 – similar to 3, but trickier
def f():
try:
lock1.acquire()
task_protected_by_lock1()
lock2.acquire()
task_protected_by_lock1_and_lock2()
lock1.release()
# Suppose SIGINT happened here, just after another thread acquired lock1
task_protected_by_lock2()
lock2.release()
except Exception as e:
if lock1.locked():
lock1.release() # Bad: lock1 is NOT acquired by this thread!
if lock2.locked():
lock2.release()
raise e
Approach 5 – breaks consistency & inefficient
def f():
with lock1:
task_protected_by_lock1()
# Bad: other thread might acquire lock1 and modify protected resources.
# This is NOT supposed to happen.
with lock1, lock2:
task_protected_by_lock1_and_lock2()
# Bad: other thread might acquire lock2 and modify protected resources.
# This is NOT supposed to happen.
with lock2:
task_protected_by_lock2()
You need to sign in to view this answers
Leave feedback about this