altangent / ccxws

WebSocket client for 38 cryptocurrency exchanges
MIT License
619 stars 187 forks source link

Bittrex requesting REST snapshot error: flowcontrol/throttle.js Cannot read property 'unref' of undefined #226

Closed evan-coygo closed 3 years ago

evan-coygo commented 3 years ago

node 12.14

I see this after upgrading to 0.39.2. It worked fine previously on 0.38.2.

When subscribing to l2 orderbook on Bittrex this error occurs when it requests the snapshot over REST

TypeError: Cannot read property 'unref' of undefined
    at Throttle._schedule (/my/local/path/node_modules/ccxws/src/flowcontrol/throttle.js:27:22)
    at Throttle._process (/my/local/path/node_modules/ccxws/src/flowcontrol/throttle.js:37:12)
    at Throttle.add (/my/local/path/node_modules/ccxws/src/flowcontrol/throttle.js:12:29)
    at BittrexClient._sendSubLevel2Updates (/my/local/path/node_modules/ccxws/src/exchanges/bittrex-client.js:141:10)

I believe it stems from the bittrex-client constructor using the throttle.js utility

this._requestLevel2Snapshot = throttle(
      this._requestLevel2Snapshot.bind(this),
      throttleL2Snapshot
    );

This is the line within throttle.js. setTimeout is returning undefined so this._handle is undefined, causing this._handle.unref to be undefined;

 _schedule() {
    this._handle = setTimeout(this._process.bind(this), this.delayMs);
    if (this._handle.unref) {   // <---- here
      this._handle.unref();
    }
  }
evan-coygo commented 3 years ago

this turned out to be due to being run in a Jest environment in a test. running it in a plain node environment worked fine, so I'm closing

bmancini55 commented 3 years ago

Gotcha, thanks for digging into it!