0

When using the web3 API in a loop, in node.js, after a certain number of iterations the script freezes, and in any other terminal I get a:
-bash: fork: Resource temporarily unavailable
error when executing any shell command. As soon as I kill the script, shell commands start working again.

As a simple test, the following script leads to this issue:

const Web3 = require("web3");
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

for (var i = 0; i < 10000; i++) {
  console.log("Start: " + i);
  console.log(web3.eth.getBalance("0x0000000000000000000000000000000000000000"));
  console.log(web3.eth.getCode("0x0000000000000000000000000000000000000000"));
}

after 225 iterations (so the last output lines shown before it freezes are):

Start: 225
{ [String: '7.221712092140691731494e+21'] s: 1, e: 21, c: [ 72217120, 92140691731494 ] }

This is running on a mac, against a geth node, with "web3": "^0.18.4".

Any help / advice / better patterns for this type of thing, much appreciated!

Thanks,
Adam

1

First you should update at leaset to web3 v0.20.*.

The issue with web3 is in synchronus mode (when you do not pass a callback) it forks a new instance of node and launches a script to create a new connection, and wait for the answer before continuing. It appears you are reaching some limit impossed by your os there.

You can switch to async mode passing a callback function. But in that case you will have a different limit because each web3 will create a new connection for every request, and likely your os limit the amount of handles open simultaneously. You can workaround that by modifying HttpProvider to share connections between calls.

My take after doing all this things is that the RPC/web3 is not well prepared to handle thousands of calls per second.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.