js-kubo-rpc-client icon indicating copy to clipboard operation
js-kubo-rpc-client copied to clipboard

Error in fetch triggers pubsub error

Open Hugo-Trentesaux opened this issue 1 year ago • 0 comments

TL;DR A failing kubo1.dag.get(cid) triggers error in kubo2.pubsub.subscribe() .


I have two Kubo RPC instances:

// create RPC client only for dag fetch
const kubo1: KuboRPCClient = create({
  url: new URL(KUBO_RPC),
  agent: new Agent({
    maxSockets: 50000
  })
})
// create an other RPC client only for pubsub
export const kubo2: KuboRPCClient = create({
  url: new URL(KUBO_RPC),
  agent: new Agent({
    keepAlive: true,
    keepAliveMsecs: 1000
  })
})

First kubo2 is used for pubsub:

kubo2.pubsub.subscribe(TOPIC, handleMessage, {onError: pubsubErrorCallback}).catch(pubsubAbortCallback)

Then kubo1 is used to get dag:

kubo1.dag.get(cid).then(handleDag).catch(console.log)

The dag.get can timeout with UND_ERR_HEADERS_TIMEOUT (that's ok), but the problem is that this error seems to propagate to pubsub with UND_ERR_BODY_TIMEOUT, see the following stack trace:

+++ dag get error +++
TypeError: fetch failed
    at node:internal/deps/undici/undici:12344:11
    at Client.fetch (node_modules/.pnpm/[email protected][email protected]/node_modules/kubo-rpc-client/src/lib/http.ts:149:42)
    at get (node_modules/.pnpm/[email protected][email protected]/node_modules/kubo-rpc-client/src/block/get.ts:7:17)
    at Object.get (node_modules/.pnpm/[email protected][email protected]/node_modules/kubo-rpc-client/src/dag/get.ts:29:19) {
  cause: HeadersTimeoutError: Headers Timeout Error
      at Timeout.onParserTimeout [as callback] (node_modules/.pnpm/[email protected]/node_modules/undici/lib/dispatcher/client-h1.js:622:28)
      at Timeout.onTimeout [as _onTimeout] (node_modules/.pnpm/[email protected]/node_modules/undici/lib/util/timers.js:22:13)
      at listOnTimeout (node:internal/timers:573:17)
      at process.processTimers (node:internal/timers:514:7) {
    code: 'UND_ERR_HEADERS_TIMEOUT'
  }
}
+++ pubsub error +++
TypeError: terminated
    at Fetch.onAborted (node:internal/deps/undici/undici:10916:53)
    at Fetch.emit (node:events:518:28)
    at Fetch.emit (node:domain:488:12)
    at Fetch.terminate (node:internal/deps/undici/undici:10102:14)
    at Object.onError (node:internal/deps/undici/undici:11034:38)
    at Request.onError (node_modules/.pnpm/[email protected]/node_modules/undici/lib/core/request.js:298:27)
    at Object.errorRequest (node_modules/.pnpm/[email protected]/node_modules/undici/lib/core/util.js:585:13)
    at Socket.<anonymous> (node_modules/.pnpm/[email protected]/node_modules/undici/lib/dispatcher/client-h1.js:716:12)
    at Socket.emit (node:events:530:35)
    at Socket.emit (node:domain:488:12) {
  [cause]: BodyTimeoutError: Body Timeout Error
      at Timeout.onParserTimeout [as callback] (node_modules/.pnpm/[email protected]/node_modules/undici/lib/dispatcher/client-h1.js:626:28)
      at Timeout.onTimeout [as _onTimeout] (node_modules/.pnpm/[email protected]/node_modules/undici/lib/util/timers.js:22:13)
      at listOnTimeout (node:internal/timers:573:17)
      at process.processTimers (node:internal/timers:514:7) {
    code: 'UND_ERR_BODY_TIMEOUT'
  }
}
+++ pubsub abort +++
DOMException [AbortError]: This operation was aborted
    at node:internal/deps/undici/undici:12344:11
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at Client.fetch (node_modules/.pnpm/[email protected][email protected]/node_modules/kubo-rpc-client/src/lib/http.ts:149:42)

I thought these two would be independent and an error on kubo1 fetch will not trigger an error on kubo2 sub. Is there a way to work around this with additional options like abort signal, timeout, or so?

Hugo-Trentesaux avatar Apr 25 '24 13:04 Hugo-Trentesaux