NodeJS : Error: read ECONNRESET at TCP.onStreamRead (internal/stream_base_commons.js:111:27)

Shabhana picture Shabhana · Jan 4, 2019 · Viewed 36.7k times · Source

Using Polling like below to check if the content of the file is changed then, other two functions are called

var poll_max_date=AsyncPolling(function (end,err) {   if(err)   {
       console.error(err);   }   var stmp_node_id=fs.readFileSync(path.join(__dirname,'node_id'),"utf8"); 
   console.log("--------loaded node : "+stmp_node_id);  
   if(druid_stmp_node_id!=stmp_node_id)   {
         // MAX DATA CUT-OFF DRUID QUERY
         druid_exe.max_date_query_fire();

         // // DRUID QUERY FOR GLOBAL DATA
         druid_exe.global_druid_query_fire();

         druid_stmp_node_id=stmp_node_id;   }

     end(); }, 1800000).run();//30 mins

Its working fine for sometime, but then getting below error like after 4 - 5hours :

events.js:167 throw er; // Unhandled 'error' event ^

Error: read ECONNRESET at TCP.onStreamRead (internal/stream_base_commons.js:111:27) Emitted 'error' event at: at emitErrorNT (internal/streams/destroy.js:82:8) at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)

tried using fs.watch to monitor the changes in the file instead of polling like below :

let md5Previous = null; let fsWait = false;

fs.watch(dataSourceLogFile, (event, filename) => { if (filename) {

if (fsWait) return;
fsWait = setTimeout(() => {
  fsWait = false;
}, 1000);

const md5Current = md5(fs.readFileSync(dataSourceLogFile));
if (md5Current === md5Previous) {
  return;
}

md5Previous = md5Current;

console.log(`${filename} file Changed`);

// MAX DATA CUT-OFF DRUID QUERY
druid_exe.max_date_query_fire();
// DRUID QUERY FOR GLOBAL DATA
druid_exe.global_druid_query_fire();   } });

Its is also working fine for sometime, but then getting same error like after 4 - 5hours :

events.js:167 throw er; // Unhandled 'error' event ^

Error: read ECONNRESET at TCP.onStreamRead (internal/stream_base_commons.js:111:27) Emitted 'error' event at: at emitErrorNT (internal/streams/destroy.js:82:8) at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)

But when run in Local Machine, its working fine. the error occurs only when run in remote Linux Machine.

somebody can help me how I can fix that problem?

Answer

Ashok Mandal picture Ashok Mandal · Jan 4, 2019

Use fs.watchFile once , because fs.watch is not consistent across platforms,

https://nodejs.org/docs/latest/api/fs.html#fs_fs_watchfile_filename_options_listener

Change your code according to the requirement.