Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b46b9d5

Browse files
committed
Issue #9617: Signals received during a low-level write operation aren't
ignored by the buffered IO layer anymore.
1 parent 522180a commit b46b9d5

3 files changed

Lines changed: 87 additions & 1 deletion

File tree

Lib/test/test_io.py

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import unittest
2828
import weakref
2929
import abc
30+
import signal
31+
import errno
3032
from itertools import cycle, count
3133
from collections import deque
3234
from test import support
@@ -2463,6 +2465,75 @@ class CMiscIOTest(MiscIOTest):
24632465
class PyMiscIOTest(MiscIOTest):
24642466
io = pyio
24652467

2468+
2469+
@unittest.skipIf(os.name == 'nt', 'POSIX signals required for this test.')
2470+
class SignalsTest(unittest.TestCase):
2471+
2472+
def setUp(self):
2473+
self.oldalrm = signal.signal(signal.SIGALRM, self.alarm_interrupt)
2474+
2475+
def tearDown(self):
2476+
signal.signal(signal.SIGALRM, self.oldalrm)
2477+
2478+
def alarm_interrupt(self, sig, frame):
2479+
1/0
2480+
2481+
@unittest.skipUnless(threading, 'Threading required for this test.')
2482+
def check_interrupted_write(self, item, bytes, **fdopen_kwargs):
2483+
"""Check that a partial write, when it gets interrupted, properly
2484+
invokes the signal handler."""
2485+
read_results = []
2486+
def _read():
2487+
s = os.read(r, 1)
2488+
read_results.append(s)
2489+
t = threading.Thread(target=_read)
2490+
t.daemon = True
2491+
r, w = os.pipe()
2492+
try:
2493+
wio = self.io.open(w, **fdopen_kwargs)
2494+
t.start()
2495+
signal.alarm(1)
2496+
# Fill the pipe enough that the write will be blocking.
2497+
# It will be interrupted by the timer armed above. Since the
2498+
# other thread has read one byte, the low-level write will
2499+
# return with a successful (partial) result rather than an EINTR.
2500+
# The buffered IO layer must check for pending signal
2501+
# handlers, which in this case will invoke alarm_interrupt().
2502+
self.assertRaises(ZeroDivisionError,
2503+
wio.write, item * (1024 * 1024))
2504+
t.join()
2505+
# We got one byte, get another one and check that it isn't a
2506+
# repeat of the first one.
2507+
read_results.append(os.read(r, 1))
2508+
self.assertEqual(read_results, [bytes[0:1], bytes[1:2]])
2509+
finally:
2510+
os.close(w)
2511+
os.close(r)
2512+
# This is deliberate. If we didn't close the file descriptor
2513+
# before closing wio, wio would try to flush its internal
2514+
# buffer, and block again.
2515+
try:
2516+
wio.close()
2517+
except IOError as e:
2518+
if e.errno != errno.EBADF:
2519+
raise
2520+
2521+
def test_interrupted_write_unbuffered(self):
2522+
self.check_interrupted_write(b"xy", b"xy", mode="wb", buffering=0)
2523+
2524+
def test_interrupted_write_buffered(self):
2525+
self.check_interrupted_write(b"xy", b"xy", mode="wb")
2526+
2527+
def test_interrupted_write_text(self):
2528+
self.check_interrupted_write("xy", b"xy", mode="w", encoding="ascii")
2529+
2530+
class CSignalsTest(SignalsTest):
2531+
io = io
2532+
2533+
class PySignalsTest(SignalsTest):
2534+
io = pyio
2535+
2536+
24662537
def test_main():
24672538
tests = (CIOTest, PyIOTest,
24682539
CBufferedReaderTest, PyBufferedReaderTest,
@@ -2472,7 +2543,9 @@ def test_main():
24722543
StatefulIncrementalDecoderTest,
24732544
CIncrementalNewlineDecoderTest, PyIncrementalNewlineDecoderTest,
24742545
CTextIOWrapperTest, PyTextIOWrapperTest,
2475-
CMiscIOTest, PyMiscIOTest,)
2546+
CMiscIOTest, PyMiscIOTest,
2547+
CSignalsTest, PySignalsTest,
2548+
)
24762549

24772550
# Put the namespaces of the IO module we are testing and some useful mock
24782551
# classes in the __dict__ of each test.

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ Extensions
117117
Library
118118
-------
119119

120+
- Issue #9617: Signals received during a low-level write operation aren't
121+
ignored by the buffered IO layer anymore.
122+
120123
- Issue #843590: Make "macintosh" an alias to the "mac_roman" encoding.
121124

122125
- Create os.fsdecode(): decode from the filesystem encoding with

Modules/_io/bufferedio.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,11 @@ _bufferedwriter_flush_unlocked(buffered *self, int restore_pos)
16651665
self->write_pos += n;
16661666
self->raw_pos = self->write_pos;
16671667
written += Py_SAFE_DOWNCAST(n, Py_off_t, Py_ssize_t);
1668+
/* Partial writes can return successfully when interrupted by a
1669+
signal (see write(2)). We must run signal handlers before
1670+
blocking another time, possibly indefinitely. */
1671+
if (PyErr_CheckSignals() < 0)
1672+
goto error;
16681673
}
16691674

16701675
if (restore_pos) {
@@ -1802,6 +1807,11 @@ bufferedwriter_write(buffered *self, PyObject *args)
18021807
}
18031808
written += n;
18041809
remaining -= n;
1810+
/* Partial writes can return successfully when interrupted by a
1811+
signal (see write(2)). We must run signal handlers before
1812+
blocking another time, possibly indefinitely. */
1813+
if (PyErr_CheckSignals() < 0)
1814+
goto error;
18051815
}
18061816
if (self->readable)
18071817
_bufferedreader_reset_buf(self);

0 commit comments

Comments
 (0)