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

Skip to content

Commit 45cff66

Browse files
committed
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.
1 parent 992019c commit 45cff66

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,10 +7,12 @@
77

88
import asynchat
99
import asyncore
10+
import errno
1011
import socket
1112
import sys
1213
import time
1314
import unittest
15+
import unittest.mock
1416
try:
1517
import threading
1618
except ImportError:
@@ -273,6 +275,21 @@ class TestAsynchat_WithPoll(TestAsynchat):
273275
usepoll = True
274276

275277

278+
class TestAsynchatMocked(unittest.TestCase):
279+
def test_blockingioerror(self):
280+
# Issue #16133: handle_read() must ignore BlockingIOError
281+
sock = unittest.mock.Mock()
282+
sock.recv.side_effect = BlockingIOError(errno.EAGAIN)
283+
284+
dispatcher = asynchat.async_chat()
285+
dispatcher.set_socket(sock)
286+
self.addCleanup(dispatcher.del_channel)
287+
288+
with unittest.mock.patch.object(dispatcher, 'handle_error') as error:
289+
dispatcher.handle_read()
290+
self.assertFalse(error.called)
291+
292+
276293
class TestHelperFunctions(unittest.TestCase):
277294
def test_find_prefix_at_end(self):
278295
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
@@ -27,6 +27,9 @@ Core and Builtins
2727
Library
2828
-------
2929

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

0 commit comments

Comments
 (0)