Skip to content

Commit 7f00abc

Browse files
committedJul 28, 2021
jobserver: fix rare race condition in previous timer-exception workaround.
We have to clear the setitimer *before* leaving the try/except clause, or the timer might fire between the try/except and the try/finally, leaking a TimeoutError. Reported-by: Denton Gentry
1 parent 8b2a4e9 commit 7f00abc

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed
 

‎redo/jobserver.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,17 @@ def _try_read(fd, n):
178178
signal.setitimer(signal.ITIMER_REAL, 0.01, 0.01) # emergency fallback
179179
try:
180180
b = os.read(fd, 1)
181-
except TimeoutError:
181+
finally:
182+
signal.setitimer(signal.ITIMER_REAL, 0, 0)
183+
signal.signal(signal.SIGALRM, oldh)
184+
except TimeoutError:
185+
return None # try again
186+
except OSError as e:
187+
if e.errno in (errno.EAGAIN, errno.EINTR):
188+
# interrupted or it was nonblocking
182189
return None # try again
183-
except OSError as e:
184-
if e.errno in (errno.EAGAIN, errno.EINTR):
185-
# interrupted or it was nonblocking
186-
return None # try again
187-
else:
188-
raise
189-
finally:
190-
signal.setitimer(signal.ITIMER_REAL, 0, 0)
191-
signal.signal(signal.SIGALRM, oldh)
190+
else:
191+
raise
192192
return b
193193

194194

0 commit comments

Comments
 (0)
Please sign in to comment.