web3.js icon indicating copy to clipboard operation
web3.js copied to clipboard

Websocket Provider "error" and "end" events not fired when client disconnects

Open cgewecke opened this issue 6 years ago • 5 comments

As reported by @hfa0 in #3190 comment, 1.2.7 introduces a bug where "error" and "end" listeners directly attached to the Websocket provider aren't firing as expected on close.

Subscriptions and events do have their error handlers fired, and pending requests should also error correctly.

provider = new Web3.providers.WebsocketProvider(url);
provider.on('end', () => {
  // not fired
});
provider.on('error', () => {
  // not fired
});
provider.on('close', (event) => {
  // event fired
})
contract.events[type]({ fromBlock }).on('error', (err) => {
  // event fired
})
provider.disconnect(1012, 'restart');

The "close" event contains an object lots of information about what happened fwiw:

"type": "close",
 "isTrusted": false,
 "_yaeti": true,
 "code": 1006,
 "reason": "Connection dropped by remote peer.",
 "wasClean": false,
  ... etc

Web3.js versions: 1.2.7, 1.2.7-rc.0

cgewecke avatar Apr 26 '20 03:04 cgewecke

Re-opening for visibility. (Not sure how widely used the provider.on('error') pattern is and .on('close') is a reasonable work-around if a patch is a needed asap).

cgewecke avatar Apr 27 '20 07:04 cgewecke

I got the same error on 1.2.7

maxprof avatar May 03 '20 09:05 maxprof

Fixed in 1.2.8

cgewecke avatar May 20 '20 21:05 cgewecke

this doesn't work again in 1.3.4 - 'end', 'error' and even 'close' never fire - only 'connect' does. Also, where is the documentation?? Available events description is absent altogether. It needs to be drastically improved. Including the available provider options. Right now there is just a usage example without any explanation of what e.g. "auto" or "keepalive" do and how they are (expected) to work exactly.

jamboj avatar Feb 27 '21 20:02 jamboj

issue persist in "web3": "^1.8.1"

emurmotol avatar Nov 28 '22 10:11 emurmotol