Skip to content

Commit 0405dcb

Browse files
committed
check _request_information_cache value is a dict before .get-ing from it
1 parent 5dda5d6 commit 0405dcb

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

newsfragments/3642.bugfix.rst

Lines changed: 1 addition & 0 deletions
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.

tests/core/providers/test_websocket_provider.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,3 +534,19 @@ async def test_req_info_cache_size_can_be_set_and_warns_when_full(caplog):
534534
"behavior. Consider increasing the ``request_information_cache_size`` "
535535
"on the provider."
536536
) in caplog.text
537+
538+
539+
@pytest.mark.asyncio
540+
async def test_raise_stray_errors_from_cache_handles_list_response():
541+
provider = WebSocketProvider("ws://mocked")
542+
_mock_ws(provider)
543+
544+
bad_response = [
545+
{"id": None, "jsonrpc": "2.0", "error": {"code": 21, "message": "oops"}}
546+
]
547+
provider._request_processor._request_response_cache._data["bad_key"] = bad_response
548+
549+
try:
550+
provider._raise_stray_errors_from_cache()
551+
except Exception as e:
552+
pytest.fail(f"{e}")

web3/providers/persistent/persistent.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -320,17 +320,16 @@ def _raise_stray_errors_from_cache(self) -> None:
320320
for (
321321
response
322322
) in self._request_processor._request_response_cache._data.values():
323-
request = (
324-
self._request_processor._request_information_cache.get_cache_entry(
323+
if isinstance(response, dict):
324+
request = self._request_processor._request_information_cache.get_cache_entry( # noqa: E501
325325
generate_cache_key(response["id"])
326326
)
327-
)
328-
if "error" in response and request is None:
329-
# if we find an error response in the cache without a corresponding
330-
# request, raise the error
331-
validate_rpc_response_and_raise_if_error(
332-
response, None, logger=self.logger
333-
)
327+
if "error" in response and request is None:
328+
# if we find an error response in the cache without a
329+
# corresponding request, raise the error
330+
validate_rpc_response_and_raise_if_error(
331+
cast(RPCResponse, response), None, logger=self.logger
332+
)
334333

335334
async def _message_listener(self) -> None:
336335
self.logger.info(

0 commit comments

Comments
 (0)