I have this function which I call once in a minute or so (I made it more readable):
async function mySend() {
let httpOptions = {
keepAlive: false,
timeout: 40000, // ms
}
let web3T1 = await new Web3(new Web3HttpProvider(myHttpUrl, httpOptions))
try {
let confirmedOrReverted = false
web3T1.eth.sendTransaction(txData)
.once('transactionHash', function(hash){logger.info(`recieved transaction hash: ${hash}`)})
.once('receipt', function(receipt){
logger.info(`Tx receipt => ${receipt.status === true? 'success' : 'failed'}!`)
confirmedOrReverted = true
})
.once('confirmation', async function(confirmationNumber, receipt){
logger.info(`recieved confirmation #${confirmationNumber}`);
})
.once('error', async function(error){
if (error.message.includes('Transaction has been reverted by the EVM')) {
logger.info(`Transaction has been reverted by the EVM.`)
} else {
logger.error(`TX exception: ${error.message} `)
}
confirmedOrReverted = true
})
while (!confirmedOrReverted) { await sleep(100) }
} catch(err) {
logger.error(`error: ${err}`)
}
if (web3T1 != null) {
//https://stackoverflow.com/questions/50632114/web3-websocket-connection-prevents-node-process-from-exiting
web3T1 = null
logger.debug(`provider closed.`)
}
}
On Windows 10 this works perfectly.
On Ubuntu 20.x (2 different installations, different nodejs versions) after half an hour or so, I see my CPU spikes up to 100% for my nodejs process. I have to kill the process.
Only difference to Windows: I use pm2 on the ubuntu machine to start the process.
I nailed everything down to the web3.eth.sendTransaction part. Even when I set the web3 object to null, it seems nodejs is keeping something alive which keeps on polling or so. I profiled the application and the only thing I saw is that C++ epoll_pwait takes 87% of CPU time. Which I guess is normal because I wait inbetween my function calls?
Anyway, any hint is appreciated. Maybe I do not handle the sendTransaction call well, and Windows does not mind so much?
EDIT: On Windows, I have this exception logged from time to time:
TX exception: Failed to check for transaction receipt: {}
I do not see this line logged on Ubuntu. Could this be the culprit? Somehow the event emitter on Windows throws this error, but not on Ubuntu and it keeps polling for that?
EDIT2: Using sockstat I can see that when the CPU usage is very high I also have a lot of open sockets with the same local port number:
But this list is gone after a few seconds, when the CPU usage goes down.
Why there are so many connections on the same local port? How is that possible, when I set my connection object to null? It seems that the server keeps the connection ESTABLISHED and somehow internal unix does that too instead of destroying the socket and closing the connection. How can I prevent that?