WebSocket Requests just hang

So only recently(last week or so) my event filter requests just timeout. I don’t believe it is my code that is causing the issue as I can run the same code on Rinkeby and Infura responses as expected. Other requests to Infura such as getBlock also respond correctly.

I’ve tried updating to latest version of web3.py to no avail.

Any help you could provide would be much appreciated.

Error Message

Traceback (most recent call last):
File “”, line 1, in
File “/indexer.py”, line 67, in process_events
{‘fromBlock’: from_block, ‘toBlock’: ‘latest’}
File “/interpreter/lib/python3.6/site-packages/web3/utils/decorators.py”, line 14, in _wrapper
return self.method(obj, *args, **kwargs)
File “/interpreter/lib/python3.6/site-packages/web3/utils/decorators.py”, line 57, in wrapper
return to_wrap(*args, **kwargs)
File “/interpreter/lib/python3.6/site-packages/web3/contract.py”, line 389, in eventFilter
log_filter = self.web3.eth.filter(event_filter_params)
File “/interpreter/lib/python3.6/site-packages/web3/eth.py”, line 332, in filter
[filter_params],
File “/interpreter/lib/python3.6/site-packages/web3/manager.py”, line 109, in request_blocking
response = self._make_request(method, params)
File “/interpreter/lib/python3.6/site-packages/web3/manager.py”, line 92, in _make_request
return request_func(method, params)
File “cytoolz/functoolz.pyx”, line 232, in cytoolz.functoolz.curry.call
File “/interpreter/lib/python3.6/site-packages/web3/middleware/formatting.py”, line 48, in apply_formatters
response = make_request(method, formatted_params)
File “/interpreter/lib/python3.6/site-packages/web3/middleware/gas_price_strategy.py”, line 18, in middleware
return make_request(method, params)
File “cytoolz/functoolz.pyx”, line 232, in cytoolz.functoolz.curry.call
File “/interpreter/lib/python3.6/site-packages/web3/middleware/formatting.py”, line 48, in apply_formatters
response = make_request(method, formatted_params)
File “/interpreter/lib/python3.6/site-packages/web3/middleware/attrdict.py”, line 18, in middleware
response = make_request(method, params)
File “cytoolz/functoolz.pyx”, line 232, in cytoolz.functoolz.curry.call
File “/interpreter/lib/python3.6/site-packages/web3/middleware/formatting.py”, line 48, in apply_formatters
response = make_request(method, formatted_params)
File “/interpreter/lib/python3.6/site-packages/web3/middleware/normalize_errors.py”, line 9, in middleware
result = make_request(method, params)
File “cytoolz/functoolz.pyx”, line 232, in cytoolz.functoolz.curry.call
File “/interpreter/lib/python3.6/site-packages/web3/middleware/formatting.py”, line 50, in apply_formatters
response = make_request(method, params)
File “cytoolz/functoolz.pyx”, line 232, in cytoolz.functoolz.curry.call
File “/interpreter/lib/python3.6/site-packages/web3/middleware/formatting.py”, line 48, in apply_formatters
response = make_request(method, formatted_params)
File “/interpreter/lib/python3.6/site-packages/web3/providers/websocket.py”, line 119, in make_request
return future.result()
File “/usr/lib/python3.6/concurrent/futures/_base.py”, line 432, in result
return self.__get_result()
File “/usr/lib/python3.6/concurrent/futures/_base.py”, line 384, in __get_result
raise self._exception
File “/interpreter/lib/python3.6/site-packages/web3/providers/websocket.py”, line 107, in coro_make_request
timeout=self.websocket_timeout
File “/usr/lib/python3.6/asyncio/tasks.py”, line 362, in wait_for
raise futures.TimeoutError()
concurrent.futures._base.TimeoutError

Code:

def process_events(full_sync=False):
    """
    Syncs database with events on smart contracts
    :param full_sync: Should be full sync.
    """

    rox_contract = ContractHelper(ROX_ABI_PATH, websocket=True, network=1)

    from_block = 0

    if not full_sync:
        // THIS REQUESTS WORKS
        from_block = rox_contract.w3.eth.getBlock('latest')['number'] - 20
    // THIS REQUEST JUST TIMES OUT
    events = rox_contract.contract.eventFilter(
        'Transfer',
        {'fromBlock': from_block, 'toBlock': 'latest'}
    ).get_all_entries()

   // Processes events

class ContractHelper(object):
    def __init__(self, contract_abi, websocket=False, network=1):
        self.w3 = self.get_web3_instance(websocket, network)
        self.contract = self.get_contract_for_abi(contract_abi, network)
        
    @classmethod
    def get_web3_instance(cls, websocket=False, network=1):
        """
        Returns a web 3 instance with a setup provider
        :param websocket: Should the provider use websocket
        :param network to set the provider up with.
        :return: A web3 instance with setup provider
        """
        if websocket:
            w3 = Web3(
                Web3.WebsocketProvider(
                    'wss://{}.infura.io/ws/v3/{}'.format(NETWORKS[network], INFURA_PROJECT_ID)
                )
            )
        else:
            w3 = Web3(
                Web3.HTTPProvider(
                    'https://{}.infura.io/v3/{}'.format(NETWORKS[network], INFURA_PROJECT_ID)
                )
            )
        w3.middleware_stack.inject(geth_poa_middleware, layer=0)
        
        return w3
    
    def get_contract_for_abi(self, path, network=1):
        """
        Returns a contract instance based on path and network
        :param path: Path of abi source.
        :param network: The network id to use.
        :return: A contract instance 
        """
        with open(path) as f:
            info_json = json.load(f)
        abi = info_json['abi']

        contract_address = info_json['networks'][str(network)]['address']
        contract_address = self.w3.toChecksumAddress(contract_address)
        
        return self.w3.eth.contract(address=contract_address, abi=abi)

Same problem here. Did you manage to get it fixed?

No, I didn’t I just created a light node instead.

They are fairly easy to set up with docker

To speed up syncing you should also add peers since (this will make a lot quicker to sync)

This is the command i used
docker run -it -p 8545:8545 -p 30303:30303 ethereum/client-go --rpc --rpcaddr “0.0.0.0” --syncmode light console

and of course, just add -d after run. To run/daemonize it in the background

Hope this helps