ttezel / twit

Twitter API Client for node (REST & Streaming API)
4.32k stars 573 forks source link

Error: aborted #558

Open crivasr opened 3 years ago

crivasr commented 3 years ago

I'm using this module for a couple month and since yesterday I think, or maybe even before that, I'm getting this error after a couple minutes of running the program:

node:events:356
      throw er; // Unhandled 'error' event
      ^

Error: aborted
    at connResetException (node:internal/errors:642:14)
    at TLSSocket.socketCloseListener (node:_http_client:446:27)
    at TLSSocket.emit (node:events:391:22)
    at TLSSocket.EventEmitter.emit (node:domain:470:12)
    at node:net:667:12
    at TCP.done (node:_tls_wrap:573:7)
Emitted 'error' event on StreamingAPIConnection instance at:
    at IncomingMessage.<anonymous> (/home/pi/DiscordBot/node_modules/twit/lib/streaming-api-connection.js:126:14)
    at IncomingMessage.emit (node:events:379:20)
    at IncomingMessage.EventEmitter.emit (node:domain:470:12)
    at TLSSocket.socketCloseListener (node:_http_client:446:13)
    at TLSSocket.emit (node:events:391:22)
    [... lines matching original stack trace ...]
    at TCP.done (node:_tls_wrap:573:7) {
  code: 'ECONNRESET'
}

here is my code:

var T = new Twit({
    consumer_key:         "xxxxx",
    consumer_secret:      "xxxxx",
    access_token:         "xxxxx",
    access_token_secret:  "xxxxx",
    timeout_ms:           60*1000,  // optional HTTP request timeout to apply to all requests.
    strictSSL:            true,     // optional - requires SSL certificates to be valid.
});

var stream = T.stream('statuses/filter', { follow: "xxxxx" });

stream.on('tweet', tweet => {
    if (tweet.retweeted_status
        || tweet.in_reply_to_status_id
        || tweet.in_reply_to_status_id_str
        || tweet.in_reply_to_user_id
        || tweet.in_reply_to_user_id_str
        || tweet.in_reply_to_screen_name) return;
    // do something with tweet
    })
});
remperazKevin commented 3 years ago

Same problem here.

I have been using the API for a good month (or so) but from the past couple of days in every five minutes (starting from launching my script) I get the same Error: aborted from my console logs. After some searching off the internet, I found that this error is thrown when there had been a problem with its TCP peer connection (a connection error basically).

My current understanding of this is that the API server itself is loaded to the brim (meaning there are too many requests happening at once and to save itself the server shuts off your connection to it). Given that its latest version was published three years ago, it is somewhat understandable that there would be many developers using it and problems with the API package itself.

Currently, what I did to mitigate but not completely solve it was to put the stream listener inside a try catch statement and have the script run using PM2 (if the try catch statement isn't enough at least I don't have to worry about manually re-launching the script). Sadly there is no error event emitter from the API (although there is disconnect and warning emitters. I don't really think the emitters can do much help with this error, plus, I haven't really used those emitters).

Hopefully, this would get fixed, or at least they would add an error event emitter so that it doesn't abruptly end the process running.

ray-bun commented 3 years ago

Same issue here, try catch did do the trick

NunoSempere commented 2 years ago

Here is some wrapper code with try/catch+a recursive function which I think solves this problem.

const sleep = (ms) => {
  return new Promise((resolve) => setTimeout(resolve, ms));
};

async function startRecursiveStream(i){
  if(i>0){
    console.log("Starting stream...")
    try{
      let stream = T.stream('statuses/filter', { track: '@metaforecast' })
      stream.on('tweet', answerWithMetaforecastScreenshot)
    }catch(error){
      sleep(1000) // Be a good citizen
      console.log("Most ot the time, the below will be a harmless reconnection error")
      console.log(error)
      startRecursiveStream(i-1)
    }  
  }else{
    console.log("Too many errors. Shutting down.")
    throw(new Error("Continued failure to connect to stream.")) // Heroku application should restart after fatal error.
  }
}

startRecursiveStream(10)
Blqckcore commented 2 years ago

Hello, I have this issue and wrapping it in a try catch block didn't change anything.