When retrieving blocks from the blockchain using bitcoind RPC commands, bitcoind will temporarily stop sending responses to RPC commands after many thousands of commands if the commands are sent in quick succession. The issue has something to do with timing, since adding a delay to retrieving blocks will make the problem go away.
This node.js code will display the issue (obviously, replace "password" with your bitcoind password):
var rpc=require('json-rpc2');
var bitcoind=rpc.Client.create(8332, "localhost",
"bitcoinrpc", "password");
var chain=function(blockhash){
bitcoind.call('getblock',[blockhash],function(err,block){
if (err){
console.log(String(err));
if (String(err)=="Error: Have no response object"){
console.log("Thought so.");
}
return;
}
console.log(block.hash, block.height);
if (block.nextblockhash){
chain(block.nextblockhash);
};
});
};
chain("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f");
Note that my development machine has an SSD, and so blocks are retrieved quickly by bitcoind - it may be necessary to use an SSD to reproduce the problem.
What this code does is to retrieve the genesis block, then get the next block, and the next block, and so on, until it has got all the blocks. Except at around 25,000 or so blocks in (different every time), it will quit when it encounters a "no response object" error. This error has nothing to do with node.js or json-rpc2 - it is a bitcoind error. This can be seen by running bitcoin commands from the command prompt, which result in a "couldn't connect to server" error, e.g.:
> bitcoind getblock 00000000722ecede1cc4edd52a7d416bdafd6528f630926b0021b41a6b57ba5d
error: couldn't connect to server
After a few seconds, bitcoind will begin responding to commads again. It doesn't crash. It just stops responding to commands for a short while.
By delaying repeated RPC commands, the above code can be fixed to run without freezing bitcoind (notice the 10ms setTimeout):
var rpc=require('json-rpc2');
var bitcoind=rpc.Client.create(8332, "localhost",
"bitcoinrpc", "password");
var chain=function(blockhash){
bitcoind.call('getblock',[blockhash],function(err,block){
if (err){
console.log(String(err));
if (String(err)=="Error: Have no response object"){
console.log("Thought so.");
}
return;
}
console.log(block.hash, block.height);
if (block.nextblockhash){
setTimeout(function(){
chain(block.nextblockhash);
},10);
};
});
};
chain("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f");