Skip to content

Commit a83310b

Browse files
committed
check _request_information_cache value is a dict before .get-ing from it
1 parent 6279d99 commit a83310b

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

Diff for: newsfragments/3642.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Checks that ``PersistentConnectionProvider`` response cache value is a dict before attempting to access it like one.

Diff for: tests/core/providers/test_websocket_provider.py

+18
Original file line numberDiff line numberDiff line change
@@ -510,3 +510,21 @@ async def test_websocket_provider_raises_errors_from_cache_not_tied_to_a_request
510510
with pytest.raises(Web3RPCError, match="Request shutdown"):
511511
await asyncio.sleep(0.1)
512512
await async_w3.eth.block_number
513+
514+
515+
@pytest.mark.asyncio
516+
async def test_raise_stray_errors_from_cache_handles_list_response():
517+
provider = WebSocketProvider("ws://mocked")
518+
519+
provider._ws = AsyncMock()
520+
provider._ws.closed = False
521+
522+
bad_response = [
523+
{"id": None, "jsonrpc": "2.0", "error": {"code": 21, "message": "oops"}}
524+
]
525+
provider._request_processor._request_response_cache._data["bad_key"] = bad_response
526+
527+
try:
528+
provider._raise_stray_errors_from_cache()
529+
except Exception as e:
530+
pytest.fail(f"{e}")

Diff for: web3/providers/persistent/persistent.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -318,17 +318,16 @@ def _raise_stray_errors_from_cache(self) -> None:
318318
for (
319319
response
320320
) in self._request_processor._request_response_cache._data.values():
321-
request = (
322-
self._request_processor._request_information_cache.get_cache_entry(
321+
if isinstance(response, dict):
322+
request = self._request_processor._request_information_cache.get_cache_entry( # noqa: E501
323323
generate_cache_key(response["id"])
324324
)
325-
)
326-
if "error" in response and request is None:
327-
# if we find an error response in the cache without a corresponding
328-
# request, raise the error
329-
validate_rpc_response_and_raise_if_error(
330-
response, None, logger=self.logger
331-
)
325+
if "error" in response and request is None:
326+
# if we find an error response in the cache without a
327+
# corresponding request, raise the error
328+
validate_rpc_response_and_raise_if_error(
329+
cast(RPCResponse, response), None, logger=self.logger
330+
)
332331

333332
async def _message_listener(self) -> None:
334333
self.logger.info(

0 commit comments

Comments
 (0)