node-mssql: Exception on ConnectionPool release

I’m getting a uncaught exception since I upgraded to v4. I couldn’t pinpoint from the stack trace (below) when/where exactly it’s happening.

Except for the v3 to v4 migration, I didn’t change anything else in my code.

TypeError: Cannot read property 'release' of null
    at ConnectionPool.release (/app/node_modules/mssql/lib/base.js:199:14)
    at Request.tds.Request.err [as userCallback] (/app/node_modules/mssql/lib/tedious.js:608:25)
    at Request._this.callback (/app/node_modules/tedious/lib/request.js:47:27)
    at Connection.message (/app/node_modules/tedious/lib/connection.js:1401:27)
    at Connection.dispatchEvent (/app/node_modules/tedious/lib/connection.js:687:45)
    at MessageIO.<anonymous> (/app/node_modules/tedious/lib/connection.js:602:18)
    at emitNone (events.js:86:13)
    at MessageIO.emit (events.js:186:7)
    at ReadablePacketStream.<anonymous> (/app/node_modules/tedious/lib/message-io.js:102:16)
    at emitOne (events.js:96:13)
    at ReadablePacketStream.emit (events.js:189:7)

Hope you’ll be able to help, thanks!

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 18

Most upvoted comments

I am having the same issue.

TypeError: Cannot read property 'release' of null
    at ConnectionPool.release (/var/app/current/node_modules/mssql/lib/base.js:199:14)
    at Request.tds.Request.err [as userCallback] (/var/app/current/node_modules/mssql/lib/tedious.js:608:25)
    at Request._this.callback (/var/app/current/node_modules/tedious/lib/request.js:47:27)
    at Connection.message (/var/app/current/node_modules/tedious/lib/connection.js:1401:27)
    at Connection.dispatchEvent (/var/app/current/node_modules/tedious/lib/connection.js:687:45)
    at MessageIO.<anonymous> (/var/app/current/node_modules/tedious/lib/connection.js:602:18)
    at emitNone (events.js:86:13)
    at MessageIO.emit (events.js:185:7)
    at ReadablePacketStream.<anonymous> (/var/app/current/node_modules/tedious/lib/message-io.js:102:16)
    at emitOne (events.js:96:13)
    at ReadablePacketStream.emit (events.js:188:7)

I’m testing the following to bypass the error, so far so good:

const connection = new mssql.ConnectionPool(...);

// Patch for https://github.com/patriksimek/node-mssql/issues/467
connection._throwingClose = connection._close;
connection._close = function(callback) {
  const close = connection._throwingClose.bind(this, callback);
  if (this.pool) {
    return this.pool.drain().then(close);
  }
  else {
    return close();
  }
};

The error might come from the _close method:

https://github.com/patriksimek/node-mssql/blob/85c14ff5b01b1ae0575591c7e4f1c7327cf82e51/lib/base.js#L299-L311

this.pool is set to null just after calling this.pool.drain(), so drain() isn’t done yet. I suspect drain() to cause requests to error, reaching the following:

https://github.com/patriksimek/node-mssql/blob/85c14ff5b01b1ae0575591c7e4f1c7327cf82e51/lib/tedious.js#L455 which calls https://github.com/patriksimek/node-mssql/blob/85c14ff5b01b1ae0575591c7e4f1c7327cf82e51/lib/base.js#L197

but at this point this.pool has been set to null.

Moving this.pool = null inside drain 's callback might have unwanted side-effects, so I won’t open a PR for it but @patriksimek might enlighten us.