|
3 | 3 | import collections |
4 | 4 | import errno |
5 | 5 | import selectors |
| 6 | +import signal |
6 | 7 | import socket |
7 | 8 | import sys |
8 | 9 | import unittest |
@@ -150,6 +151,119 @@ def test_read_from_self_exception(self): |
150 | 151 | self.loop._ssock.recv.side_effect = OSError |
151 | 152 | self.assertRaises(OSError, self.loop._read_from_self) |
152 | 153 |
|
| 154 | + def test_read_from_self_eof_rebuilds_self_pipe(self): |
| 155 | + # gh-156344: a clean EOF (recv returns b'') must rebuild the |
| 156 | + # socketpair instead of leaving the reader registered on a socket |
| 157 | + # that is readable forever, which would busy-loop the CPU at 100%. |
| 158 | + loop = self.loop |
| 159 | + old_ssock = loop._ssock |
| 160 | + loop._ssock.recv.return_value = b'' |
| 161 | + loop._remove_reader = mock.Mock() |
| 162 | + loop._add_reader = mock.Mock() |
| 163 | + with mock.patch('asyncio.selector_events.socket.socketpair', |
| 164 | + return_value=(mock.Mock(), mock.Mock())) as socketpair: |
| 165 | + self.assertIsNone(loop._read_from_self()) |
| 166 | + self.assertTrue(socketpair.called) |
| 167 | + self.assertIsNot(loop._ssock, old_ssock) |
| 168 | + loop._remove_reader.assert_called_with(old_ssock.fileno()) |
| 169 | + loop._add_reader.assert_called_with(loop._ssock.fileno(), |
| 170 | + loop._read_from_self) |
| 171 | + |
| 172 | + def test_read_from_self_blocking_is_not_eof(self): |
| 173 | + # gh-156344: only a clean EOF triggers the rebuild -- a would-block |
| 174 | + # read must not. |
| 175 | + self.loop._ssock.recv.side_effect = BlockingIOError |
| 176 | + with mock.patch('asyncio.selector_events.socket.socketpair') as sp: |
| 177 | + self.assertIsNone(self.loop._read_from_self()) |
| 178 | + self.assertFalse(sp.called) |
| 179 | + |
| 180 | + def test_self_pipe_eof_rebuild_functional(self): |
| 181 | + # gh-156344 functional test on a real selector loop: kill the |
| 182 | + # self-pipe with a graceful half-close and verify the pair is |
| 183 | + # rebuilt, the reader lives only on the new fd, and wakeups keep |
| 184 | + # working through the new pair. |
| 185 | + loop = selector_events.BaseSelectorEventLoop() |
| 186 | + self.addCleanup(loop.close) |
| 187 | + old_ssock = loop._ssock |
| 188 | + old_fd = old_ssock.fileno() |
| 189 | + old_csock = loop._csock |
| 190 | + |
| 191 | + old_csock.shutdown(socket.SHUT_WR) |
| 192 | + loop._read_from_self() |
| 193 | + |
| 194 | + # pair rebuilt and reader registered on the new fd only |
| 195 | + self.assertIsNot(loop._ssock, old_ssock) |
| 196 | + self.assertNotEqual(loop._ssock.fileno(), old_fd) |
| 197 | + self.assertNotIn(old_fd, loop._selector.get_map()) |
| 198 | + self.assertIn(loop._ssock.fileno(), loop._selector.get_map()) |
| 199 | + |
| 200 | + # wakeups through the new pair still work |
| 201 | + loop._write_to_self() |
| 202 | + data = loop._ssock.recv(4096) |
| 203 | + self.assertEqual(data, b'\0') |
| 204 | + |
| 205 | + @mock.patch('asyncio.selector_events.socket.socketpair') |
| 206 | + def test_rebuild_self_pipe_moves_wakeup_fd(self, socketpair): |
| 207 | + # gh-156344: on Unix the wakeup fd registered by add_signal_handler() |
| 208 | + # names _csock; a rebuild must move it to the new socket and must not |
| 209 | + # touch a registration owned by someone else. |
| 210 | + loop = self.loop |
| 211 | + old_ssock, old_csock = loop._ssock, loop._csock |
| 212 | + loop._remove_reader = mock.Mock() |
| 213 | + loop._add_reader = mock.Mock() |
| 214 | + |
| 215 | + new_ssock, new_csock = mock.Mock(), mock.Mock() |
| 216 | + socketpair.return_value = (new_ssock, new_csock) |
| 217 | + |
| 218 | + # Simulate the Unix mixin's signal state: _signal_handlers non-empty |
| 219 | + # and the wakeup fd naming our _csock. |
| 220 | + loop._signal_handlers = {signal.SIGINT: mock.Mock()} |
| 221 | + with mock.patch('asyncio.selector_events.signal.set_wakeup_fd', |
| 222 | + return_value=old_csock.fileno()) as m_wakeup_fd: |
| 223 | + loop._rebuild_self_pipe() |
| 224 | + # moved: new fd registered, old one not re-registered |
| 225 | + self.assertEqual(m_wakeup_fd.call_args_list, |
| 226 | + [mock.call(new_csock.fileno())]) |
| 227 | + # old reader removed, old sockets closed, reader re-armed on the new |
| 228 | + loop._remove_reader.assert_called_with(old_ssock.fileno()) |
| 229 | + self.assertTrue(old_ssock.close.called) |
| 230 | + self.assertTrue(old_csock.close.called) |
| 231 | + self.assertIs(loop._ssock, new_ssock) |
| 232 | + self.assertIs(loop._csock, new_csock) |
| 233 | + loop._add_reader.assert_called_with(new_ssock.fileno(), |
| 234 | + loop._read_from_self) |
| 235 | + |
| 236 | + @mock.patch('asyncio.selector_events.socket.socketpair') |
| 237 | + def test_rebuild_self_pipe_leaves_foreign_wakeup_fd(self, socketpair): |
| 238 | + # set_wakeup_fd returned a fd that is not ours: it belongs to someone |
| 239 | + # else and must be restored untouched. |
| 240 | + loop = self.loop |
| 241 | + loop._remove_reader = mock.Mock() |
| 242 | + loop._add_reader = mock.Mock() |
| 243 | + new_ssock, new_csock = mock.Mock(), mock.Mock() |
| 244 | + socketpair.return_value = (new_ssock, new_csock) |
| 245 | + |
| 246 | + loop._signal_handlers = {signal.SIGINT: mock.Mock()} |
| 247 | + with mock.patch('asyncio.selector_events.signal.set_wakeup_fd', |
| 248 | + return_value=999) as m_wakeup_fd: |
| 249 | + loop._rebuild_self_pipe() |
| 250 | + self.assertEqual(m_wakeup_fd.call_args_list, |
| 251 | + [mock.call(new_csock.fileno()), |
| 252 | + mock.call(999)]) |
| 253 | + |
| 254 | + @mock.patch('asyncio.selector_events.socket.socketpair') |
| 255 | + def test_rebuild_self_pipe_no_signals(self, socketpair): |
| 256 | + # Without add_signal_handler() state the wakeup fd is untouched. |
| 257 | + self.loop._remove_reader = mock.Mock() |
| 258 | + self.loop._add_reader = mock.Mock() |
| 259 | + new_ssock, new_csock = mock.Mock(), mock.Mock() |
| 260 | + socketpair.return_value = (new_ssock, new_csock) |
| 261 | + |
| 262 | + with mock.patch('asyncio.selector_events.signal.set_wakeup_fd', |
| 263 | + return_value=self.loop._csock.fileno()) as m_wakeup_fd: |
| 264 | + self.loop._rebuild_self_pipe() |
| 265 | + self.assertFalse(m_wakeup_fd.called) |
| 266 | + |
153 | 267 | def test_write_to_self_tryagain(self): |
154 | 268 | self.loop._csock.send.side_effect = BlockingIOError |
155 | 269 | with test_utils.disable_logger(): |
|
0 commit comments