ruimarinho / bitcoin-core

A modern Bitcoin Core REST and RPC client.
477 stars 186 forks source link

UnhandledPromiseRejectionWarning: Error: ESOCKETTIMEDOUT #107

Closed Papabyte closed 4 years ago

Papabyte commented 4 years ago

Hello

I have a script that fetches BTC blockchain block after block using getBlockByHash.

client.getBlockByHash(block_hash, { extension: 'json' }).then(async (block) => {

}).catch((error)=>{ console.log(error); });

Sometimes it crashes with this error:

(node:26497) UnhandledPromiseRejectionWarning: Error: ESOCKETTIMEDOUT at ClientRequest. (/home/btc/proxy/node_modules/request/request.js:816:19) at Object.onceWrapper (events.js:313:30) at emitNone (events.js:106:13) at ClientRequest.emit (events.js:208:7) at Socket.emitTimeout (_http_client.js:718:34) at Object.onceWrapper (events.js:313:30) at emitNone (events.js:106:13) at Socket.emit (events.js:208:7) at Socket._onTimeout (net.js:422:8) at ontimeout (timers.js:498:11) (node:26497) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:26497) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Since the error happens in request module and is not caught by getBlockByHash's catch, I suspect the problem can be fixed only in bitcoin-core module.

Unfortunately I cannot give a way to reproduce the issue since it only happens randomly every few 10000's blocks.

Nodejs v8.16.1 bitcoin-core 3.0.0 Bitcoin Core 0.18.1

pedrobranco commented 4 years ago

If the request takes more than 30 seconds to respond it will throw ESOCKETTIMEDOUT on client side. Run with

DEBUG=*bitcoin-core* node <your_file.js> | bunyan

and check the daemon request response metrics.

Papabyte commented 4 years ago

Indeed, it's due to the client taking too long to respond:

[2019-10-22T10:52:04.162Z] DEBUG: bitcoin-core/22229 on ns3265332: Making request a5087589-c887-4f8a-80b1-40b697b96646 to GET http://localhost:8000/rest/block/00000000000000000023d939afafb4eafb9f7027139c4ed329f0e48d0317755c.json request: { "headers": { "host": "localhost:8000" }, "id": "a5087589-c887-4f8a-80b1-40b697b96646", "method": "GET", "type": "request", "uri": "http://localhost:8000/rest/block/00000000000000000023d939afafb4eafb9f7027139c4ed329f0e48d0317755c.json" } [2019-10-22T10:52:34.810Z] DEBUG: bitcoin-core/22229 on ns3265332: Making request a5087589-c887-4f8a-80b1-40b697b96646 to GET http://localhost:8000/rest/block/00000000000000000023d939afafb4eafb9f7027139c4ed329f0e48d0317755c.json request: { "duration": 30655, "error": { "code": "ESOCKETTIMEDOUT", "connect": false }, "headers": { "host": "localhost:8000" }, "id": "a5087589-c887-4f8a-80b1-40b697b96646", "method": "GET", "type": "error", "uri": "http://localhost:8000/rest/block/00000000000000000023d939afafb4eafb9f7027139c4ed329f0e48d0317755c.json" } (node:22229) UnhandledPromiseRejectionWarning: Error: ESOCKETTIMEDOUT at ClientRequest. (/home/btc/proxy/node_modules/request/request.js:816:19) at Object.onceWrapper (events.js:313:30) at emitNone (events.js:106:13) at ClientRequest.emit (events.js:208:7) at Socket.emitTimeout (_http_client.js:718:34) at Object.onceWrapper (events.js:313:30) at emitNone (events.js:106:13) at Socket.emit (events.js:208:7) at Socket._onTimeout (net.js:422:8) at ontimeout (timers.js:498:11) (node:22229) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:22229) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Is there any way to avoid my script to crash in this case? I would like to just retry the request later.

pedrobranco commented 4 years ago

Is there any way to avoid my script to crash in this case? I would like to just retry the request later.

You have two options:

Papabyte commented 4 years ago

Sorry I'm confused, I don't know what I did wrong but actually the .catch as in the code sample works perfectly. Thanks for your help!

pedrobranco commented 4 years ago

No problem.