Skip to content

Upgrade websockets dependency to v10+ #2324

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

Merged
merged 2 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions newsfragments/2324.breaking-change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update ``websockets`` dependency to v10+
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"requests>=2.16.0,<3.0.0",
# remove typing_extensions after python_requires>=3.8, see web3._utils.compat
"typing-extensions>=3.7.4.1,<5;python_version<'3.8'",
"websockets>=9.1,<10",
"websockets>=10.0.0,<11",
],
python_requires='>=3.7,<3.10',
extras_require=extras_require,
Expand Down
6 changes: 4 additions & 2 deletions tests/core/providers/test_websocket_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ async def empty_server(websocket, path):
data = await websocket.recv()
await asyncio.sleep(0.02)
await websocket.send(data)
server = websockets.serve(empty_server, '127.0.0.1', open_port, loop=event_loop)

asyncio.set_event_loop(event_loop)
server = websockets.serve(empty_server, '127.0.0.1', open_port)
event_loop.run_until_complete(server)
event_loop.run_forever()

Expand All @@ -54,7 +56,7 @@ def w3(open_port, start_websocket_server):
# need new event loop as the one used by server is already running
event_loop = asyncio.new_event_loop()
endpoint_uri = 'ws://127.0.0.1:{}'.format(open_port)
event_loop.run_until_complete(wait_for_ws(endpoint_uri, event_loop))
event_loop.run_until_complete(wait_for_ws(endpoint_uri))
provider = WebsocketProvider(endpoint_uri, websocket_timeout=0.01)
return Web3(provider)

Expand Down
6 changes: 4 additions & 2 deletions tests/integration/go_ethereum/test_goethereum_ws.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import pytest

from tests.integration.common import (
Expand Down Expand Up @@ -63,8 +64,9 @@ def geth_command_arguments(geth_binary,


@pytest.fixture(scope="module")
def web3(geth_process, endpoint_uri, event_loop):
event_loop.run_until_complete(wait_for_ws(endpoint_uri, event_loop))
def web3(geth_process, endpoint_uri):
event_loop = asyncio.new_event_loop()
event_loop.run_until_complete(wait_for_ws(endpoint_uri))
_web3 = Web3(Web3.WebsocketProvider(endpoint_uri, websocket_timeout=30))
return _web3

Expand Down
4 changes: 2 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ def get_open_port():
return str(port)


async def wait_for_ws(endpoint_uri, event_loop, timeout=60):
async def wait_for_ws(endpoint_uri, timeout=10):
start = time.time()
while time.time() < start + timeout:
try:
async with websockets.connect(uri=endpoint_uri, loop=event_loop):
async with websockets.connect(uri=endpoint_uri):
pass
except (ConnectionRefusedError, OSError):
await asyncio.sleep(0.01)
Expand Down
7 changes: 3 additions & 4 deletions web3/providers/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,16 @@ def get_default_endpoint() -> URI:
class PersistentWebSocket:

def __init__(
self, endpoint_uri: URI, loop: asyncio.AbstractEventLoop, websocket_kwargs: Any
self, endpoint_uri: URI, websocket_kwargs: Any
) -> None:
self.ws: WebSocketClientProtocol = None
self.endpoint_uri = endpoint_uri
self.loop = loop
self.websocket_kwargs = websocket_kwargs

async def __aenter__(self) -> WebSocketClientProtocol:
if self.ws is None:
self.ws = await connect(
uri=self.endpoint_uri, loop=self.loop, **self.websocket_kwargs
uri=self.endpoint_uri, **self.websocket_kwargs
)
return self.ws

Expand Down Expand Up @@ -113,7 +112,7 @@ def __init__(
'found: {1}'.format(RESTRICTED_WEBSOCKET_KWARGS, found_restricted_keys)
)
self.conn = PersistentWebSocket(
self.endpoint_uri, WebsocketProvider._loop, websocket_kwargs
self.endpoint_uri, websocket_kwargs
)
super().__init__()

Expand Down