tedious icon indicating copy to clipboard operation
tedious copied to clipboard

Socket Hang-up in tedious

Open chaitanya1094 opened this issue 3 years ago • 6 comments

Hi Team, We are using tedious "6.6.5" and Node "10.15.1" to connect to Azure SQL. Frequently we are facing below error and app crashes,

/opt/app/node_modules/tedious/lib/errors.js:13
return new ConnectionError(message, code);
       ^
ConnectionError: Connection lost - socket hang up
at connectionError (/opt/app/node_modules/tedious/lib/errors.js:13:12)
at connection.socketError (/opt/app/node_modules/tedious/lib/connection.js:1289:54)
at Connection.socketEnd (/opt/app/node_modules/tedious/lib/connection.js:1307:12)
at Socket.socket.on (/opt/app/node_modules/tedious/lib/connection.js:1126:14)
at socket.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1145:12)
at process. tickcallback (internal/process/next_tick.js:63:19)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: "node index`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!

Below is the code we are using to connect to DB.

const {  Connection,  Request} = require('tedious');
var config = {
    server: '<server>',
    authentication: {
      type: 'azure-active-directory-password',
      options: {
        userName: 'test',
        password: 'test'
      }
    },
    options: {
      encrypt: true,
      conenctionTimeout:30000
    }
  };
const queryDB= (query,  done) => {

    let result = "";
    let ERROR_HANDLER = null;
    const connection = new Connection(config);

    connection.on("connect", err => {

        if (err) {
            ERROR_HANDLER = err
            ERROR_HANDLER.status = 500;
            done(ERROR_HANDLER);
            return;
        }
        const request = new Request(query, (err, rowCount) => {
            if (err) {
                ERROR_HANDLER = err
                ERROR_HANDLER.status = 500;
            } else {
                console.log(`${rowCount} rows returned`);
            }
        })

        request.on("row", columns => {
            result += columns[0].value;
        });

        request.on('requestCompleted', () => {

            connection.close();
            if (ERROR_HANDLER) {

                done(ERROR_HANDLER);

            } else {
                done(null, result);
            }
        })

        request.on('error', (err) => {

            connection.close();
            ERROR_HANDLER = err;

            ERROR_HANDLER.status = 500;
            done(err);
        })
        connection.execSql(request);
    })
}

module.exports={queryDB}

chaitanya1094 avatar Jul 01 '21 06:07 chaitanya1094

I don't see anything specific about your use case that would cause these hangups. Have you tried upgrading to more recent versions of tedious and checked if the same error still happens there as well? 🤔

arthurschreiber avatar Jul 01 '21 08:07 arthurschreiber

Thanks for the response, we are in production currently.. so little hesitant to upgrade...this error is occurring if a huge load on the app is present. Is this error because we are creating a connection for each DB call? If we want to upgrade what version you suggest?

chaitanya1094 avatar Jul 01 '21 09:07 chaitanya1094

Thanks for the response, we are in production currently.. so little hesitant to upgrade...

I'd suggest doing the updates in small steps, so you can resolve any deprecation warnings. That might help you to be more confident about the upgrade.

this error is occurring if a huge load on the app is present. Is this error because we are creating a connection for each DB call?

That's hard to say, really. 😞 Maybe you're hitting some sort of timeout when a lot of connections / requests are being processed? The error is very generic and basically just means that the connection was closed by the server, but it's not clear why. You could try to see if you can also access the server's logs and if this helps you to pinpoint where the issue is coming from.

arthurschreiber avatar Jul 01 '21 19:07 arthurschreiber

I'm seeing this same issue with Azure SQL. Several times per day this error occurs resetting the app. 80% of the time everything works as expected, then these errors pop up causing the app to restart.

MaddyTP avatar Oct 18 '22 00:10 MaddyTP

We are also facing the same issue, is there any fix?

aryamohanan avatar Feb 23 '24 08:02 aryamohanan