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

Skip to content

Commit 316b16d

Browse files
committed
(Merge 3.4) Issue #16133: The asynchat.async_chat.handle_read() method now
ignores BlockingIOError exceptions. Initial patch written by Xavier de Gaye. Document also in asyncore documentation that recv() may raise BlockingIOError.
2 parents e1d24f7 + 45cff66 commit 316b16d

4 files changed

Lines changed: 26 additions & 0 deletions

File tree

Doc/library/asyncore.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ any that have been added to the map during asynchronous service) is closed.
216216
empty bytes object implies that the channel has been closed from the
217217
other end.
218218

219+
Note that :meth:`recv` may raise :exc:`BlockingIOError` , even though
220+
:func:`select.select` or :func:`select.poll` has reported the socket
221+
ready for reading.
222+
219223

220224
.. method:: listen(backlog)
221225

Lib/asynchat.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ def handle_read(self):
115115

116116
try:
117117
data = self.recv(self.ac_in_buffer_size)
118+
except BlockingIOError:
119+
return
118120
except OSError as why:
119121
self.handle_error()
120122
return

Lib/test/test_asynchat.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77

88
import asynchat
99
import asyncore
10+
import errno
1011
import socket
1112
import sys
1213
import time
1314
import unittest
1415
import warnings
16+
import unittest.mock
1517
try:
1618
import threading
1719
except ImportError:
@@ -274,6 +276,21 @@ class TestAsynchat_WithPoll(TestAsynchat):
274276
usepoll = True
275277

276278

279+
class TestAsynchatMocked(unittest.TestCase):
280+
def test_blockingioerror(self):
281+
# Issue #16133: handle_read() must ignore BlockingIOError
282+
sock = unittest.mock.Mock()
283+
sock.recv.side_effect = BlockingIOError(errno.EAGAIN)
284+
285+
dispatcher = asynchat.async_chat()
286+
dispatcher.set_socket(sock)
287+
self.addCleanup(dispatcher.del_channel)
288+
289+
with unittest.mock.patch.object(dispatcher, 'handle_error') as error:
290+
dispatcher.handle_read()
291+
self.assertFalse(error.called)
292+
293+
277294
class TestHelperFunctions(unittest.TestCase):
278295
def test_find_prefix_at_end(self):
279296
self.assertEqual(asynchat.find_prefix_at_end("qwerty\r", "\r\n"), 1)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ Core and Builtins
108108
Library
109109
-------
110110

111+
- Issue #16133: The asynchat.async_chat.handle_read() method now ignores
112+
BlockingIOError exceptions.
113+
111114
- Issue #19884: readline: Disable the meta modifier key if stdout is not
112115
a terminal to not write the ANSI sequence "\033[1034h" into stdout. This
113116
sequence is used on some terminal (ex: TERM=xterm-256color") to enable

0 commit comments

Comments
 (0)