node-twitter-api-v2 icon indicating copy to clipboard operation
node-twitter-api-v2 copied to clipboard

[bug] "Phantom" TCP socket upon some disconnects from stream (filtered stream)

Open NoUJoe opened this issue 3 years ago • 1 comments

I've been running a test project to get familiar. I was briefly disonnected from my wifi when moving my laptop, which lead to this. When my app attempted to reconnect, I received a "TooManyConnections" error. And again on the next attempt, and again and so forth. I stopped and started my app, same issue. I restarted my laptop, same issue. Only after between 15 - 20 minutes can I reconnect successfully. This outlines the problem.

Testing further, terminating the app via ctrl-c and starting it again doesn't yeild this issue. Sometimes, the first attempt to connect will throw this error, but resolves within 10-15 seconds and the second attempt will be a success. But usually connects first time.

So currently, it seems the issue is specific to the kind of disconnect that occurs. I haven't tested whether unplugging my router from the phoneline yields the same issue, but I expect it would.

I am by NO MEANS that knowledgable on TCP/UDP sockets, but my thoughts are that it is something to do with the Twitter servers keeping their end of the connection "alive" (and only checking every 15 minutes?) Because when terminating my app whilst connected to the internet, I'm guessing that my side closes the socket and tells Twitter to close too. Whereas when I lose connection to the internet, my side can't tell Twitter to close the socket.

But I'm purely speculating when it comes to the cause. All I know is that this happens.

I have a test script that I've thrown together to reproduce the issue. You just need to put your own bearer token in and make sure you have filtered stream rules in place because this code does not do that.

To reproduce, just run the script, wait for the stream to connect and:

  1. Terminate the script via ctrl-c, wait a few seconds and re-run it. It should connect fine (maybe 2nd try)

  2. Disonnect your machine from the internet and then reconnect. The script will attempt to reconnect every 20 seconds (which is within API weight limits of 50 requests per 15 min window). It should keep throwing the TooManyConnections error for about 15-20 minutes until it finally reconnects.

https://gist.github.com/NoUJoe/ac22f1db17bc5bac145d967615fea849

Expected behavior Sockets closed cleanly

Version

  • Node.js version 16.13.1
  • Lib version 1.12.3
  • OS (especially if you use Windows) WIndows 10

NoUJoe avatar Jul 15 '22 02:07 NoUJoe

Hello, Thank you for the detailed report.

Unfortunately, I couldn't manage to reproduce (with macOS). By looking at the code, everything is closed correctly (on Node.js side) and I don't know what to do more. It might be related to https://github.com/PLhery/node-twitter-api-v2/issues/326.

If you have time to, you can look at the code and make a PR? If the problem is on system side/Twitter side, we cannot do more and this is not a place for a such issue.

alkihis avatar Jul 28 '22 12:07 alkihis