Skip to content

Commit 7490fe3

Browse files
committed
Add documentation around read buffer; Raise clear exception:
- Document ``read_buffer_limit`` on the ``AsyncIPCProvider`` class. - Add ``ReadBufferLimitReached`` exception to be raised when the read buffer limit is reached, prompting the user to increase the limit. - Add a base ``PersistentConnectionError`` exception class for persistent connection errors to inherit from.
1 parent 517399c commit 7490fe3

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

docs/providers.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ AsyncIPCProvider
269269
JSON-RPC server.
270270

271271
* ``ipc_path`` is the filesystem path to the IPC socket:
272+
* ``read_buffer_limit`` is the maximum size of data, in bytes, that can be read
273+
from the socket at one time. Defaults to 20MB (20 * 1024 * 1024). Raises
274+
``ReadBufferLimitReached`` if the limit is reached, suggesting that the buffer
275+
limit be increased.
272276

273277
This provider inherits from the
274278
:class:`~web3.providers.persistent.PersistentConnectionProvider` class. Refer to

newsfragments/3492.feature.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Add a configuration option for the ``read_buffer_limit`` for ``AsyncIPCProvider`` in order to control the expected message size limit. Set this default value to 20MB.
1+
Add a configuration option for the ``read_buffer_limit`` for ``AsyncIPCProvider`` in order to control the expected message size limit (defaults to 20MB). Add ``ReadBufferLimitReached`` for when the read limit is reached, extend from ``PersistentConnectionError``.

tests/core/providers/test_async_ipc_provider.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
from web3.datastructures import (
2020
AttributeDict,
2121
)
22+
from web3.exceptions import (
23+
ReadBufferLimitReached,
24+
)
2225
from web3.providers import (
2326
AsyncIPCProvider,
2427
)
@@ -312,7 +315,13 @@ async def test_async_ipc_reader_can_read_20mb_message(
312315
async def test_async_ipc_reader_raises_on_msg_over_20mb(
313316
jsonrpc_ipc_pipe_path, serve_larger_than_20mb_response
314317
):
315-
with pytest.raises(ValueError):
318+
with pytest.raises(
319+
ReadBufferLimitReached,
320+
match=(
321+
rf"Read buffer limit of `{TWENTY_MB}` bytes was reached. "
322+
"Consider increasing the ``read_buffer_limit`` on the AsyncIPCProvider."
323+
),
324+
):
316325
async with AsyncWeb3(
317326
AsyncIPCProvider(pathlib.Path(jsonrpc_ipc_pipe_path))
318327
) as w3:

web3/exceptions.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,20 @@ def __init__(
334334
super().__init__(message)
335335

336336

337-
class PersistentConnectionClosedOK(Web3Exception):
337+
class PersistentConnectionError(Web3Exception):
338+
"""
339+
Raised when a persistent connection encounters an error.
340+
"""
341+
342+
343+
class ReadBufferLimitReached(PersistentConnectionError):
344+
"""
345+
Raised when the read buffer limit is reached while reading data from a persistent
346+
connection.
347+
"""
348+
349+
350+
class PersistentConnectionClosedOK(PersistentConnectionError):
338351
"""
339352
Raised when a persistent connection is closed gracefully by the server.
340353
"""

web3/providers/persistent/async_ipc.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from ...exceptions import (
2424
PersistentConnectionClosedOK,
2525
ProviderConnectionError,
26+
ReadBufferLimitReached,
2627
Web3TypeError,
2728
)
2829
from ..ipc import (
@@ -96,7 +97,17 @@ async def socket_send(self, request_data: bytes) -> None:
9697
)
9798

9899
async def socket_recv(self) -> RPCResponse:
99-
data = await self._reader.readline()
100+
try:
101+
data = await self._reader.readline()
102+
except ValueError as e:
103+
if all(kw in str(e) for kw in ("limit", "chunk")):
104+
raise ReadBufferLimitReached(
105+
f"Read buffer limit of `{self.read_buffer_limit}` bytes was "
106+
"reached. Consider increasing the ``read_buffer_limit`` on the "
107+
"AsyncIPCProvider."
108+
) from e
109+
raise
110+
100111
if not data:
101112
raise PersistentConnectionClosedOK("Socket reader received end of stream.")
102113
return self.decode_rpc_response(data)

0 commit comments

Comments
 (0)