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

WebsocketProvider 'error', 'end', 'close' do not fire on disconnect

Open jamboj opened this issue 5 years ago • 12 comments

Expected behavior

Client application needs to know: a) when it potentially starts losing blocks i.e. when the network goes down/socket disconnects b) when a reconnect is attempted

What are the events that are supposed to fire on a) and b) above?

Where is the documentation describing available WebsocketProvider events? Also, where is the documentation describing WebsocketProvider options such as keepalive, auto, onTimeout etc?

I would expect at least one of 'end', 'error' or 'close' to fire but they do not with web3 1.3.4.

Only 'connect' fires AFTER successful reconnect is made, but this is not enough.

Actual behavior

The only even that fires in the process of losing and reestablishing connection is 'connect' (and also 'error' but only after max reconnect attempts reached

Steps to reproduce the behavior

Run the code below, disconnect wifi after first block, reconnect wifi, wait for another block then disconnect wifi

const Web3 = require('web3');

let endpoint 	= 'wss://mainnet.infura.io/ws/v3/<...>' ;

let options = {
	timeout: 10000, // ms

	clientConfig: 
	{
	  keepalive: true,
	  keepaliveInterval: 20000 // ms
	},

	reconnect: {
		auto: true,
		delay: 10000, // ms
		maxAttempts: 3,
		onTimeout: false
	}
};

var ep = new Web3.providers.WebsocketProvider(endpoint, options);

ep.on('connect', function () 
{ 
	console.log('! provider connected'); // <- fires after successful connection
});

ep.on('error', function (err) 
{ 
	console.log("~ on-error:", err); // <- never fires
});

ep.on('end', async (err) => 
{ 
	console.log("~ on-end:", err); // <- never fires
});

ep.on('close', (event) => 
{ 
	console.log("~ on-close:", event); // <- never fires
});


var web3;

connect();
 

async function newBlock(error, blockHeader)
{ 	
	console.log("block:", blockHeader.number);	
};


async function connect()
{	
	console.log("--> connecting..");

	try
	{	
		web3 = await new Web3(ep);
	
	//block part
		blockSubscr = await web3.eth.subscribe('newBlockHeaders', newBlock);
		console.log("+ subscribe blocks");
	}
	catch(err) 
	{ 
        console.log('|* connection error:', err); 
    }
}

Logs

$ node trywsrecon3.js 
--> connecting..
web3-shh package will be deprecated in version 1.3.5 and will no longer be supported.
web3-bzz package will be deprecated in version 1.3.5 and will no longer be supported.
+ subscribe blocks
! provider connected
block: 11947407
-
! provider connected
block: 11947411
~ on-error: Error: Maximum number of reconnect attempts reached!

Environment

web3.js 1.3.4 node 15.10.0 Mac OS X 10.15.7

jamboj avatar Feb 28 '21 18:02 jamboj

We'll take a look at this thanks for the detailed report.

frankiebee avatar Mar 01 '21 21:03 frankiebee

We'll take a look at this thanks for the detailed report.

Thank you! Please let me know if you need any additional info. This websocket provider reconnects is an amazing feature that seems to be almost complete - I agree it can't be fully stateful and client will indeed lose block sometimes, but as long as the client can keep clear tabs on what's going on it would be a much much easier/cleaner task, especially with different providers e.g. infura and alchemy and now bsc and others taking different approaches to handling idle clients etc.

jamboj avatar Mar 01 '21 22:03 jamboj

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment.

github-actions[bot] avatar Jul 12 '21 00:07 github-actions[bot]

what's up with this? I can confirm the issue still exists. 😢

JimLynchCodes avatar Dec 21 '21 16:12 JimLynchCodes

still exists in "web3": "^1.8.1"

emurmotol avatar Nov 29 '22 15:11 emurmotol

still exists @spacesailor24

It happens to me when subscribing to a BSC node (binance smart chain), after some time (random time) there is no more data coming from the node, as if it was disconnected but no event fire.

lodembeep avatar Apr 09 '23 16:04 lodembeep

UPDATE : I found a work around :

reconnect: {
        auto: true,
        delay: 5000, // ms
        maxAttempts: 5,
        onTimeout: false
    }

The issue is that after some time, it reached the maxAttempts.

Related to this issue is :

  • Returning empty blocks on new block header sub (fixed by Nethereum) : https://github.com/bnb-chain/bsc/issues/913
  • They have issues with their websocket closing connections : https://github.com/bnb-chain/bsc/issues/1105

I tried also to tinker with web3-providers-ws to get some data out of the events but counld't get any. Let me know if you need steps to reproduce.

lodembeep avatar Apr 10 '23 12:04 lodembeep

Need to investigate and reproduce

mconnelly8 avatar May 16 '23 16:05 mconnelly8

Hi! I think the issue reported by OP is a client rather than a server one. Regarding the issue: https://github.com/bnb-chain/bsc/issues/1105, this is currently under investigation, but I think those two are totally unrelated. Nevertheless, I will post an update here after we will have a resolution on the bsc issue.

bnb-alexlucaci avatar May 25 '23 14:05 bnb-alexlucaci

For reconnect event in 1.x you can use this code:

websocketProvider.on('reconnect', function () {
        console.log('EVENT reconnect');
});

avkos avatar Jun 08 '23 15:06 avkos

Here is an example of listening events from was provider (web3js - 1.x)

test-ws-1 x mov

As I see connect and reconnect events work fine. Also, I've caught the max attempt error in

websocketProvider.on('error', function (err) {
        console.log('EVENT error', err);
});

after max reconnection attempts are reached provider does not try to reconnect. So it works as expected

avkos avatar Jun 09 '23 08:06 avkos

closing this issue as @avkos was not able to reproduce, reopen if this issue is still persisting.

Web3.js 4.x has been released so its highly encouraged to use latest web3.js. https://docs.web3js.org/ https://docs.web3js.org/guides/web3_upgrade_guide/x/ https://www.npmjs.com/package/web3/v/4.0.1 https://github.com/web3/web3.js/releases/tag/v4.0.1

jdevcs avatar Jun 14 '23 19:06 jdevcs