You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
According to PEP-475, standard library modules should handle EINTR, but this appears to not be the case for the fcntl module.
Test script:
importfcntlimportsignalimportosdefhandle_alarm(signum, frame):
print("Received alarm in process {}!".format(os.getpid()))
child=os.fork()
ifchild:
signal.signal(signal.SIGALRM, handle_alarm)
signal.alarm(1)
withopen("foo", "w") asf:
print("Locking in process {}...".format(os.getpid()))
fcntl.flock(f, fcntl.LOCK_EX)
print("Locked in process {}.".format(os.getpid()))
os.waitpid(child, 0)
else:
signal.signal(signal.SIGALRM, handle_alarm)
signal.alarm(1)
withopen("foo", "w") asf:
print("Locking in process {}...".format(os.getpid()))
fcntl.flock(f, fcntl.LOCK_EX)
print("Locked in process {}.".format(os.getpid()))
It looks to me that interrupting fcntl can be used for implementing blocking lock with a timeout. If automatically retry fcntl() on EINTR, this will break such code.
It looks to me that interrupting fcntl can be used for implementing
blocking lock with a timeout. If automatically retry fcntl() on EINTR, this
will break such code.
If the signal handler doesn't raise an exception, the code is not reliable.
It's a common complain about the PEP-475.
If the signal handler raises an exception, it's work on Python 2.7 and on
Python 3.5 with my patch.
I think it would be nice to publish somewhere (on ActoveState receipts, on StackOverflow) well-searchable correct example.
Yeah, it's always possible to enhance the doc. I'm not an user of ActiveState or StackOverflow websites. Don't hesitate to submit a comment to explain the PEP-475 ;-)
Is it ok to fix Python 3.5? Or is it safer to only fix the issue in Python 3.6?
I just don't know whether the patch will break existing code that relies on current behavior. If EINTR handling in other functions was not backported to older versions, I think this is good argument against fixing this issue in 3.5.
Could you write tests with signal handlers raising and not raising an exception?
Activity
JackZhou commentedon Apr 8, 2016
According to PEP-475, standard library modules should handle EINTR, but this appears to not be the case for the fcntl module.
Test script:
python-dev commentedon Apr 9, 2016
New changeset 60ac28b612af by Victor Stinner in branch '3.5':
Update fcntl doc: replace IOError with OSError
https://hg.python.org/cpython/rev/60ac28b612af
vstinner commentedon Apr 9, 2016
Oh, we forgot the fcntl module in the PEP-475.
Here is a fix for Python 3.5 (and 3.6).
serhiy-storchaka commentedon Apr 9, 2016
It looks to me that interrupting fcntl can be used for implementing blocking lock with a timeout. If automatically retry fcntl() on EINTR, this will break such code.
vstinner commentedon Apr 9, 2016
Serhiy Storchaka added the comment:
If the signal handler doesn't raise an exception, the code is not reliable.
It's a common complain about the PEP-475.
If the signal handler raises an exception, it's work on Python 2.7 and on
Python 3.5 with my patch.
serhiy-storchaka commentedon Apr 10, 2016
The signal handler doesn't raise an exception in the example on StackOverflow. [1]
I think it would be nice to publish somewhere (on ActoveState receipts, on StackOverflow) well-searchable correct example.
[1] http://stackoverflow.com/questions/5255220/fcntl-flock-how-to-implement-a-timeout
vstinner commentedon Apr 12, 2016
Yeah, it's always possible to enhance the doc. I'm not an user of ActiveState or StackOverflow websites. Don't hesitate to submit a comment to explain the PEP-475 ;-)
Is it ok to fix Python 3.5? Or is it safer to only fix the issue in Python 3.6?
serhiy-storchaka commentedon Apr 12, 2016
I just don't know whether the patch will break existing code that relies on current behavior. If EINTR handling in other functions was not backported to older versions, I think this is good argument against fixing this issue in 3.5.
Could you write tests with signal handlers raising and not raising an exception?
vstinner commentedon Apr 12, 2016
Ok, I will update the patch to include an unit test and only apply it
to Python 3.5.
python-dev commentedon Apr 14, 2016
vstinner commentedon Apr 15, 2016
Patch version 2:
serhiy-storchaka commentedon Apr 15, 2016
Left nitpicks about tests.
vstinner commentedon Apr 17, 2016
I updated the test, does it look better now?
serhiy-storchaka commentedon Apr 21, 2025
It was implemented in bpo-35189 (#79370).