tedious: ConnectionError: Connection lost - write ECONNRESET when inserting long string

I have a table on an Azure SQL Database.

CREATE TABLE [dbo].[Owner](
	[OwnerId] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
	[Name] [varchar](50) NOT NULL,
	[Signature] [varchar](max) NULL
)

I tried to insert very long string to the Signature column. If the packetSize is not set to 16384 or higher, I got the following exception:

ConnectionError: Connection lost - write ECONNRESET
    at ConnectionError (C:\work\GitRepos\tedious-test\node_modules\tedious\lib\errors.js:13:12)
    at Connection.socketError (C:\work\GitRepos\tedious-test\node_modules\tedious\lib\connection.js:1187:26)
    at Socket.<anonymous> (C:\work\GitRepos\tedious-test\node_modules\tedious\lib\connection.js:1032:14)
    at Socket.emit (events.js:205:15)
    at errorOrDestroy (internal/streams/destroy.js:107:12)
    at onwriteError (_stream_writable.js:438:5)
    at onwrite (_stream_writable.js:459:5)
    at internal/streams/destroy.js:49:7
    at Socket._destroy (net.js:593:3)
    at Socket.destroy (internal/streams/destroy.js:37:8)
Emitted 'error' event at:
    at Connection.socketError (C:\work\GitRepos\tedious-test\node_modules\tedious\lib\connection.js:1187:12)
    at Socket.<anonymous> (C:\work\GitRepos\tedious-test\node_modules\tedious\lib\connection.js:1032:14)
    [... lines matching original stack trace ...]
    at Socket.destroy (internal/streams/destroy.js:37:8)
    at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:84:12) {
  message: 'Connection lost - write ECONNRESET',
  code: 'ESOCKET'
}

Below is my simple test program. I use the latest tedious. Also the read operation seems to work fine for long string no matter what the packet size is. Any idea what I did wrong?

var Connection = require('tedious').Connection;

var config = {
  server: "myserver.database.windows.net",
  options: {
    encrypt: true,
    database: "<mydb>",
    packetSize: 4096,
  },
  authentication: {
    type: "default",
    options: {  
      userName: "<myuser>",
      password: "<mypwd>",
    }
  }
};

var connection = new Connection(config);

connection.on('connect', function(err) {
    // executeStatement();
    executeInsert();
  }
);


var Request = require('tedious').Request;

function executeStatement() {
  /* Read a long string, work fine */
  request = new Request("select OwnerId, Signature From dbo.Owner Where OwnerId = 36", function(err, rowCount) {
    if (err) {
      console.log(err);
    } else {
      console.log(rowCount + ' rows');
    }
  });

  request.on('row', function(columns) {
    columns.forEach(function(column) {
      console.log(column.value);
    });
  });

  connection.execSql(request);
}


function executeInsert() {
  /************************************************
   * Insert a long string, not working when
   *     packetSize = 4096
   * If
   *     packetSize = 16384
   * or higher, insertion works fine.
   *****************************************************/
  let s = '0123456789'.repeat(100000);
  request = new Request("Insert into dbo.Owner VALUES ('Rick', '" + s + "')", function(err, rowCount) {
    if (err) {
      console.log(err);
    } else {
      console.log(rowCount + ' rows');
    }
  });

  connection.execSql(request);
}

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 32 (8 by maintainers)

Most upvoted comments

Got Connection lost - read ECONNRESET when run a long query. With encrypt: true and packetSize: 32768, everything works fine.

node version: v12.6.0

We run into a similar issue. when trying to insert a long string. After a bit of testing it turns out, that for us it is related to the used nodejs version in our case dockerized. node:10-slim -> works fine node:12.3.1 -> works fine starting node:12.4 results in either socket hangup or socket closed error.

🎉 This issue has been resolved in version 6.2.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

For whoever is still struggling with any of the following issues Connection lost - read ECONNRESET or Connection lost - write ECONNRESET while dealing with large reads or writes and using Node.js v12.4+, setting these options in the configuration fixed it for me:

encrypt:  true,
packetSize: 16368

node version: v12.2.0 works without packetSize option node version: v.12.6.0 required me to use packetSize: 8192 to avoid the Connection lost - read ECONNRESET error