Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add timeout to asyncio subscription send wait #184

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions silverback/subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Web3SubscriptionsManager:
websocket_reconnect_max_tries: int = 3
rpc_response_timeout_count: int = 10
subscription_polling_time: float = 0.1 # secs
request_timeout: float = 50.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where'd 50 seconds come from?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's what web3.py uses as a default request timeout


def __init__(self, ws_provider_uri: str):
# TODO: Temporary until a more permanent solution is added to ProviderAPI
Expand Down Expand Up @@ -113,8 +114,11 @@ async def subscribe(self, type: SubscriptionType, **filter_params) -> str:
"eth_subscribe",
[type.value, filter_params] if type is SubscriptionType.EVENTS else [type.value],
)
await self.connection.send(json.dumps(request))
response = await self._get_response(request.get("id") or self._last_request)
await asyncio.wait_for(
self.connection.send(json.dumps(request)), timeout=self.request_timeout
)

response = await self._get_response(int(request.get("id"))) # type: ignore

sub_id = response.get("result")
if not sub_id:
Expand Down Expand Up @@ -165,9 +169,11 @@ async def unsubscribe(self, sub_id: str) -> bool:
return True

request = self._create_request("eth_unsubscribe", [sub_id])
await self.connection.send(json.dumps(request))
await asyncio.wait_for(
self.connection.send(json.dumps(request)), timeout=self.request_timeout
)

response = await self._get_response(request.get("id") or self._last_request)
response = await self._get_response(int(request.get("id"))) # type: ignore
if success := response.get("result", False):
del self._subscriptions[sub_id] # NOTE: Save memory

Expand Down
Loading