Web3.py: Timeout error when I tried to send transaction

Created on 5 Jan 2017  路  19Comments  路  Source: ethereum/web3.py

I don't understanda why, but since few days, I can't send transaction.
When I do web3.eth.sendTransaction(transaction) I've this error timeout: timed out.

the transaction looks like this : transaction = {'from': user2, 'to':user1, 'data': web3.toHex("test")}
And user1 and user2are defined . Accounts are unlocked and have gas...

Have you got any idea?

Most helpful comment

For future readers who encounter this problem, one way to resolve it is by changing this:

HTTPProvider('http://137.xxx.xxxx.xxxx:8545')

To this:

HTTPProvider('http://137.xxx.xxxx.xxxx:8545',request_kwargs={'timeout':60})

As the default timeout is 10 (seconds).

All 19 comments

When you open an issue it helps for you to use the template which should have been pre-populated in the form. Can you please provide me with the following information so I can try to diagnose this.

  • Python version
  • Web3 version
  • Full stacktrace of the error.
  • How you are connecting Web3 to the blockchain (such as whether you're using the IPC provider or RPC Provider)

I'll be happy to re-open this if you can provide me with additional details about the error.

@pipermerriam ,
I've a remote server witch started by this command :

parity --chain ropsten.json --rpcapi="eth,net,web3,personal" --rpc --rpcaddr 137.xxx.xxx.xxx --rpcport 8545 --author 0x...

I installed python and web3 on an other server.

And I want to run this script :

from web3 import Web3, KeepAliveRPCProvider
web3 = Web3(KeepAliveRPCProvider(host="137.xxx.xxx.xxx", port="8545"))
user1 = web3.eth.coinbase
john  = '0x...'
transaction = {'from': user2, 'to':user1, 'data': web3.toHex("test")}
web3.eth.sendTransaction(transaction)

I've this error

File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/web3/utils/string.py", line 83, in inner
    return force_obj_to_text(fn(*args, **kwargs))
  File "/usr/local/lib/python2.7/dist-packages/web3/eth.py", line 252, in sendTransaction
    [formatters.input_transaction_formatter(self, formatted_transaction)],
  File "/usr/local/lib/python2.7/dist-packages/web3/providers/manager.py", line 42, in request_blocking
    response_raw = self.provider.make_request(method, params)
  File "/usr/local/lib/python2.7/dist-packages/web3/providers/rpc.py", line 47, in make_request
    **self.get_request_kwargs()
  File "/usr/local/lib/python2.7/dist-packages/web3/utils/compat/compat_requests.py", line 6, in make_post_request
    response = requests.post(endpoint_uri, data=data, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 110, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 499, in send
    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='10.2.210.207', port=8545): Read timed out. (read timeout=10)

Or :

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/web3/eth.py", line 243, in sendTransaction
    [formatters.input_transaction_formatter(self, formatted_transaction)],
  File "/usr/local/lib/python2.7/dist-packages/web3/providers/manager.py", line 37, in request_blocking
    response_raw = self.provider.make_request(method, params)
  File "/usr/local/lib/python2.7/dist-packages/web3/providers/rpc.py", line 161, in make_request
    response = self.client.post(self.path, body=request_data)
  File "/usr/local/lib/python2.7/dist-packages/geventhttpclient/client.py", line 218, in post
    return self.request(METHOD_POST, request_uri, body=body, headers=headers)
  File "/usr/local/lib/python2.7/dist-packages/geventhttpclient/client.py", line 200, in request
    block_size=self.block_size, method=method.upper(), headers_type=self.headers_type)
  File "/usr/local/lib/python2.7/dist-packages/geventhttpclient/response.py", line 298, in __init__
    super(HTTPSocketPoolResponse, self).__init__(sock, **kw)
  File "/usr/local/lib/python2.7/dist-packages/geventhttpclient/response.py", line 170, in __init__
    self._read_headers()
  File "/usr/local/lib/python2.7/dist-packages/geventhttpclient/response.py", line 190, in _read_headers
    data = self._sock.recv(self.block_size)
  File "/usr/local/lib/python2.7/dist-packages/gevent/_socket2.py", line 280, in recv
    self._wait(self._read_event)
  File "/usr/local/lib/python2.7/dist-packages/gevent/_socket2.py", line 179, in _wait
    self.hub.wait(watcher)
  File "/usr/local/lib/python2.7/dist-packages/gevent/hub.py", line 630, in wait
    result = waiter.get()
  File "/usr/local/lib/python2.7/dist-packages/gevent/hub.py", line 878, in get
    return self.hub.switch()
  File "/usr/local/lib/python2.7/dist-packages/gevent/hub.py", line 609, in switch
    return greenlet.switch(self)
socket.timeout: timed out

I don't understand why, and I tried to do the same thing on main network with the same result.

@ChloeCoupignyWarot can you update to the latest released version of web3 and try this with the new HTTPProvider.

from web3 import Web3, HTTPProvider
web3 = Web3(HTTPProvider('http://137.xxx.xxx.xxx:8545'))
...   # your code here

Second question is whether you've confirmed that you can actually access the RPC server. You can confirm whether this is a web3py issue or whether it's your server configuration by trying to connect using web3.js via the geth console.

```javascript
web3 = new Web3(new Web3.providers.HttpProvider('http://137.xxx.xxx.xxx:8545'))
web3.eth.blockNumber
````

@pipermerriam , Yes I can access the RPC server because when I do or exemple

from web3 import Web3, HTTPProvider
web3 = Web3(HTTPProvider('http://137.xxx.xxxx.xxxx:8545'))
web3.eth.coinbase
web3.eth.blockNumber

I've a response : 0x.. or321815

But when I want to do sendTransaction command I always have this error :

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/web3/utils/string.py", line 83, in inner
    return force_obj_to_text(fn(*args, **kwargs))
  File "/usr/local/lib/python2.7/dist-packages/web3/eth.py", line 252, in sendTransaction
    [formatters.input_transaction_formatter(self, formatted_transaction)],
  File "/usr/local/lib/python2.7/dist-packages/web3/providers/manager.py", line 42, in request_blocking
    response_raw = self.provider.make_request(method, params)
  File "/usr/local/lib/python2.7/dist-packages/web3/providers/rpc.py", line 47, in make_request
    **self.get_request_kwargs()
  File "/usr/local/lib/python2.7/dist-packages/web3/utils/compat/compat_requests.py", line 6, in make_post_request
    response = requests.post(endpoint_uri, data=data, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 110, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 499, in send
    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='137.xxx.xxx.xxx', port=8545): Read timed out. (read timeout=10)

And with web.js I've the same result :

Error: Invalid JSON RPC response: "Provided Host header is not whitelisted.\n"
    at Object.InvalidResponse (/Users/vigizen/node_modules/web3/lib/web3/errors.js:35:16)
    at HttpProvider.send (/Users/vigizen/node_modules/web3/lib/web3/httpprovider.js:91:22)
    at RequestManager.send (/Users/vigizen/node_modules/web3/lib/web3/requestmanager.js:58:32)
    at Eth.get [as blockNumber] (/Users/vigizen/node_modules/web3/lib/web3/property.js:107:62)
    at repl:1:9
    at realRunInThisContextScript (vm.js:22:35)
    at sigintHandlersWrap (vm.js:98:12)
    at ContextifyScript.Script.runInThisContext (vm.js:24:12)
    at REPLServer.defaultEval (repl.js:346:29)
    at bound (domain.js:280:14)

Looks like you need to modify how you run parity. https://github.com/ethcore/parity/issues/1812

@pipermerriam , No it's not that, yesterday I added this parameter but it doesn't change anything, I always have this error :

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/web3/utils/string.py", line 83, in inner
    return force_obj_to_text(fn(*args, **kwargs))
  File "/usr/local/lib/python2.7/dist-packages/web3/eth.py", line 252, in sendTransaction
    [formatters.input_transaction_formatter(self, formatted_transaction)],
  File "/usr/local/lib/python2.7/dist-packages/web3/providers/manager.py", line 42, in request_blocking
    response_raw = self.provider.make_request(method, params)
  File "/usr/local/lib/python2.7/dist-packages/web3/providers/rpc.py", line 47, in make_request
    **self.get_request_kwargs()
  File "/usr/local/lib/python2.7/dist-packages/web3/utils/compat/compat_requests.py", line 6, in make_post_request
    response = requests.post(endpoint_uri, data=data, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 110, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 499, in send
    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='137.xxx.xxx.xxx', port=8545): Read timed out. (read timeout=10)

when I launch parity with this :parity --chain frontier.json --rpcapi="eth,net,web3,personal,web3" --rpc --rpcaddr 137.xxx.xxx.xxx --rpcport 8545 --jsonrpc-hosts="all" --rpccorsdomain "*" --author 0x...

Have you tried again with web3.js? Do you get the same connection error?

So the fact that your node returns responses for read-only operations like web3.eth.coinbase or web3.eth.blockNumber implies that this is an issue with your node and not web3. Can you check your server logs and see what is happening with parity when these requests are happening?

I was also having the same problem, removing "nice -n 20" from the geth starting command the problem was fixed. hope this helps

@ChloeCoupignyWarot Please reopen this issue with the results from running with web3.js, again.

If other web3 implementations are having the same problem, then it is almost certainly an issue with the node setup. (whether nice, host header issues, or other)

I have the same problem here with geth as the node. I have an HTTPConnectionPool() read timeout in listener.get() > eth_getFilterChanges() > ....

Any guidance on how to deal with these read timeouts if web3.py doesn't?

I thought about wrapping listener.get() with a try{} catch{} but if listener.get() updates the last block it requested events from then we might miss some events.

if listener.get() updates the last block it requested events from then we might miss some events.

For filters in particular, if you have no tolerance for imprecise results, then you are best off not using the node's built-in filters, and just requesting a list of all events between a range of blocks in a loop, retrying on failure.

Experiencing the same issue with --config dev.

@carver I've reproduced the issue in web3.js, the following code will hang:

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
var BN = web3.utils.BN;

web3.eth.getAccounts(async (err, acc) => {
    let startingBalance = web3.utils.toBN(await web3.eth.getBalance(acc[0]))
    console.log(startingBalance.toString());
    let result = await web3.eth.sendTransaction({
        from: acc[0],
        to: acc[1],
        value: 1000000
    })
    console.log(result);
});

For future readers who encounter this problem, one way to resolve it is by changing this:

HTTPProvider('http://137.xxx.xxxx.xxxx:8545')

To this:

HTTPProvider('http://137.xxx.xxxx.xxxx:8545',request_kwargs={'timeout':60})

As the default timeout is 10 (seconds).

@barakman Why would it need more than 10 seconds to send the tx?

@shanefontaine:
It involves inter-process communication, so technically speaking, any time-duration is possible, especially when the communication is over HTTP.
Evidently, they added the timeout configuration parameter, so there's gotta be good reason for that, wouldn't you agree?
Practically, the reason I wrote this is because I ran into a sporadic timeout scenario, which I was able to resolve by increasing the timeout.
By the way, it's not about sending a transaction, it's about receiving a response to it (on the client side, where you configure the timeout).

Was this page helpful?
0 / 5 - 0 ratings