bpo-38019: correctly handle pause/resume reading of closed asyncio unix pipe (GH-16472)

This commit is contained in:
Andrew Svetlov 2019-09-29 15:00:35 +03:00 committed by GitHub
parent 9a7d951950
commit 58498bc717
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 0 deletions

View File

@ -445,6 +445,7 @@ class _UnixReadPipeTransport(transports.ReadTransport):
self._fileno = pipe.fileno() self._fileno = pipe.fileno()
self._protocol = protocol self._protocol = protocol
self._closing = False self._closing = False
self._paused = False
mode = os.fstat(self._fileno).st_mode mode = os.fstat(self._fileno).st_mode
if not (stat.S_ISFIFO(mode) or if not (stat.S_ISFIFO(mode) or
@ -506,10 +507,20 @@ class _UnixReadPipeTransport(transports.ReadTransport):
self._loop.call_soon(self._call_connection_lost, None) self._loop.call_soon(self._call_connection_lost, None)
def pause_reading(self): def pause_reading(self):
if self._closing or self._paused:
return
self._paused = True
self._loop._remove_reader(self._fileno) self._loop._remove_reader(self._fileno)
if self._loop.get_debug():
logger.debug("%r pauses reading", self)
def resume_reading(self): def resume_reading(self):
if self._closing or not self._paused:
return
self._paused = False
self._loop._add_reader(self._fileno, self._read_ready) self._loop._add_reader(self._fileno, self._read_ready)
if self._loop.get_debug():
logger.debug("%r resumes reading", self)
def set_protocol(self, protocol): def set_protocol(self, protocol):
self._protocol = protocol self._protocol = protocol

View File

@ -736,6 +736,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase):
@mock.patch('os.read') @mock.patch('os.read')
def test_resume_reading(self, m_read): def test_resume_reading(self, m_read):
tr = self.read_pipe_transport() tr = self.read_pipe_transport()
tr.pause_reading()
tr.resume_reading() tr.resume_reading()
self.loop.assert_reader(5, tr._read_ready) self.loop.assert_reader(5, tr._read_ready)
@ -790,6 +791,32 @@ class UnixReadPipeTransportTests(test_utils.TestCase):
self.assertIsNone(tr._protocol) self.assertIsNone(tr._protocol)
self.assertIsNone(tr._loop) self.assertIsNone(tr._loop)
def test_pause_reading_on_closed_pipe(self):
tr = self.read_pipe_transport()
tr.close()
test_utils.run_briefly(self.loop)
self.assertIsNone(tr._loop)
tr.pause_reading()
def test_pause_reading_on_paused_pipe(self):
tr = self.read_pipe_transport()
tr.pause_reading()
# the second call should do nothing
tr.pause_reading()
def test_resume_reading_on_closed_pipe(self):
tr = self.read_pipe_transport()
tr.close()
test_utils.run_briefly(self.loop)
self.assertIsNone(tr._loop)
tr.resume_reading()
def test_resume_reading_on_paused_pipe(self):
tr = self.read_pipe_transport()
# the pipe is not paused
# resuming should do nothing
tr.resume_reading()
class UnixWritePipeTransportTests(test_utils.TestCase): class UnixWritePipeTransportTests(test_utils.TestCase):

View File

@ -0,0 +1 @@
Correctly handle pause/resume reading of closed asyncio unix pipe.