Skip to content

RangeError: Maximum call stack size exceeded #1206

Closed

Description

@arpitgupta2405

Hi,

I'm using tedious npm module to connect with MSSQL. I have a customer facing application using which the customer can query his/her MSSQL databases.

I'm getting the below errors in my server log and server gets restarted every time this error happens:
Callstack 1
RangeError: Maximum call stack size exceeded at validateNumber (internal/validators.js:115:24) at Buffer.readUInt16LE (internal/buffer.js:225:3) at /srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:187:32 at Parser.awaitData (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:145:7) at Parser.readUInt16LE (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:186:10) at /srv/www/integrator_staging/node_modules/tedious/lib/metadata-parser.js:81:12 at /srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:221:7 at Parser.awaitData (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:145:7) at Parser.readUInt32LE (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:218:10) at metadataParse (/srv/www/integrator_staging/node_modules/tedious/lib/metadata-parser.js:80:76) at readColumn (/srv/www/integrator_staging/node_modules/tedious/lib/token/colmetadata-token-parser.js:60:31) at next (/srv/www/integrator_staging/node_modules/tedious/lib/token/colmetadata-token-parser.js:91:7) at /srv/www/integrator_staging/node_modules/tedious/lib/token/colmetadata-token-parser.js:94:9 at /srv/www/integrator_staging/node_modules/tedious/lib/token/colmetadata-token-parser.js:63:9 at /srv/www/integrator_staging/node_modules/tedious/lib/token/colmetadata-token-parser.js:54:7 at /srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:379:9 at /srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:371:7 at Parser.awaitData (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:145:7) at Parser.readBuffer (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:368:10) at /srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:378:12 at /srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:165:7 at Parser.awaitData (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:145:7)

Callstack 2
buffer.js:1071Buffer.prototype.slice = function slice(start, end) {^RangeError: Maximum call stack size exceededat Buffer.slice (buffer.js:1071:40)at /srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:369:32at Parser.awaitData (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:145:7)at Parser.readBuffer (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:368:10)at /srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:378:12at /srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:165:7at Parser.awaitData (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:145:7)at Parser.readUInt8 (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:162:10)at Parser.readBVarChar (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:377:10)at readColumnName (/srv/www/integrator_staging/node_modules/tedious/lib/token/colmetadata-token-parser.js:46:10)at /srv/www/integrator_staging/node_modules/tedious/lib/token/colmetadata-token-parser.js:62:7at readTableName (/srv/www/integrator_staging/node_modules/tedious/lib/token/colmetadata-token-parser.js:41:5)at /srv/www/integrator_staging/node_modules/tedious/lib/token/colmetadata-token-parser.js:61:5at /srv/www/integrator_staging/node_modules/tedious/lib/metadata-parser.js:122:15at /srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:165:7at Parser.awaitData (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:145:7)at Parser.readUInt8 (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:162:10)at /srv/www/integrator_staging/node_modules/tedious/lib/metadata-parser.js:121:27at /srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:165:7at Parser.awaitData (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:145:7)at Parser.readUInt8 (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:162:10)at /srv/www/integrator_staging/node_modules/tedious/lib/metadata-parser.js:82:14

Callstack 3
RangeError: Maximum call stack size exceeded at Buffer.get length [as length] () at Parser.awaitData (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:144:47) at Parser.readUInt8 (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:162:10) at /srv/www/integrator_staging/node_modules/tedious/lib/metadata-parser.js:121:27 at /srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:165:7 at Parser.awaitData (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:145:7) at Parser.readUInt8 (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:162:10) at /srv/www/integrator_staging/node_modules/tedious/lib/metadata-parser.js:82:14 at /srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:189:7 at Parser.awaitData (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:145:7) at Parser.readUInt16LE (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:186:10) at /srv/www/integrator_staging/node_modules/tedious/lib/metadata-parser.js:81:12 at /srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:221:7 at Parser.awaitData (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:145:7) at Parser.readUInt32LE (/srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:218:10) at metadataParse (/srv/www/integrator_staging/node_modules/tedious/lib/metadata-parser.js:80:76) at readColumn (/srv/www/integrator_staging/node_modules/tedious/lib/token/colmetadata-token-parser.js:60:31) at next (/srv/www/integrator_staging/node_modules/tedious/lib/token/colmetadata-token-parser.js:91:7) at /srv/www/integrator_staging/node_modules/tedious/lib/token/colmetadata-token-parser.js:94:9 at /srv/www/integrator_staging/node_modules/tedious/lib/token/colmetadata-token-parser.js:63:9 at /srv/www/integrator_staging/node_modules/tedious/lib/token/colmetadata-token-parser.js:54:7 at /srv/www/integrator_staging/node_modules/tedious/lib/token/stream-parser.js:379:9

Could you please help me in identifying the root cause of this issue.

My code is as below:
const { Request, Connection } = require('tedious')

const connectionConfig = {
authentication: {
type: 'default',
options: {
userName: '',
password: ''
}
},
server: '',
options: {
database: '',
encrypt: true,
port: 1433,
packetSize: 32768
}
}

const connection = new Connection(connectionConfig)

function rowHandler () {
// save the result
}

function errorHandler () {
// handle the error
}

let queryToRun = 'sql query'

let request = new Request(queryToRun, (err, rowCount) => {
if (err) {
// handle error
}
// cleaning up the connection
})

request.on('error', errorHandler)

request.on('row', rowHandler)

connection.on('error', errorHandler)

connection.execSql(request)

Node version: v12.15.0
tedious version: v8.0.1

Activity

  1. arthurschreiber commented on Jan 19, 2021

    @arthurschreiber
    Collaborator

    The most likely cause is that the response rows to the query are so large that parsing it completely blows up the stack.

    You could try decreasing the packetSize option as a first step. Actually fixing this in tedious will be a bit more involved. The problem is that parsing the response works via heavily nested callback chains, which are not called asynchronously (but synchronously) for as long as enough data is buffered. Only once the end of the buffered data is reached, awaitData calls will wait for more data to come in asynchronously, which will free up the stack.

    A simple, short term fix could be replacing this line:

    with

    process.nextTick(next, done);

    Also, on another note, both the Node.js version as well as the tedious version you're running on are quite old. 😓 You might want to upgrade, especially for security reasons.

  2. arpitgupta2405 commented on Jan 21, 2021

    @arpitgupta2405
    Author

    @arthurschreiber , thanks for the prompt reply.

    We are planning for the tedious upgrade in the next coming months.

    Meanwhile, I tried the fix that you suggested and it doesn't seem to work. Also please note, that packetSize is something that the end user wants to configure, so is there any way that we can mitigate this issue or can catch this uncaughtException so that our server doesn't restart on every occurrence of this issue.

  3. arthurschreiber commented on Jan 21, 2021

    @arthurschreiber
    Collaborator

    @arpitgupta2405 Thanks for trying. I will try to reproduce this issue and then look for a proper fix! 🙇‍♂️

  4. arthurschreiber commented on Jan 21, 2021

    @arthurschreiber
    Collaborator

    @arpitgupta2405 In the mean time, could you try the same fix at this code location:

    I just noticed that the stack traces you posted above point to colmetadata-token-parser.ts.

  5. arpitgupta2405 commented on Jan 22, 2021

    @arpitgupta2405
    Author

    @arthurschreiber I tried the fix and got the below error:

    Uncaught Error: Unknown type: 0
    at Parser.parseTokens (node_modules/tedious/lib/token/stream-parser.js:133:28)
    at Parser._transform (node_modules/tedious/lib/token/stream-parser.js:109:12)
    at Parser.Transform._read (node_modules/tedious/node_modules/readable-stream/lib/_stream_transform.js:177:10)
    at Parser.Transform._write (node_modules/tedious/node_modules/readable-stream/lib/_stream_transform.js:164:83)
    at doWrite (node_modules/tedious/node_modules/readable-stream/lib/_stream_writable.js:409:139)
    at writeOrBuffer (node_modules/tedious/node_modules/readable-stream/lib/_stream_writable.js:398:5)
    at Parser.Writable.write (node_modules/tedious/node_modules/readable-stream/lib/_stream_writable.js:307:11)
    at Parser.addBuffer (node_modules/tedious/lib/token/token-stream-parser.js:47:24)
    at Connection.sendDataToTokenStreamParser (node_modules/tedious/lib/connection.js:1428:35)
    at Connection. (node_modules/tedious/lib/connection.js:2380:26)
    at Connection.data (node_modules/tedious/lib/connection.js:2369:25)
    at Connection.dispatchEvent (node_modules/tedious/lib/connection.js:1277:15)
    at MessageIO. (node_modules/tedious/lib/connection.js:1134:14)
    at MessageIO.EventEmitter.emit (domain.js:475:20)
    at Message. (node_modules/tedious/lib/message-io.js:43:14)
    at Message.EventEmitter.emit (domain.js:475:20)
    at addChunk (node_modules/tedious/node_modules/readable-stream/lib/_stream_readable.js:298:12)
    at readableAddChunk (node_modules/tedious/node_modules/readable-stream/lib/_stream_readable.js:280:11)
    at Message.Readable.push (node_modules/tedious/node_modules/readable-stream/lib/_stream_readable.js:241:10)
    at Message.Transform.push (node_modules/tedious/node_modules/readable-stream/lib/_stream_transform.js:139:32)
    at Message.afterTransform (node_modules/tedious/node_modules/readable-stream/lib/_stream_transform.js:88:10)
    at Message.PassThrough._transform (node_modules/tedious/node_modules/readable-stream/lib/_stream_passthrough.js:38:3)
    at Message.Transform._read (node_modules/tedious/node_modules/readable-stream/lib/_stream_transform.js:177:10)
    at Message.Transform._write (node_modules/tedious/node_modules/readable-stream/lib/_stream_transform.js:164:83)
    at doWrite (node_modules/tedious/node_modules/readable-stream/lib/_stream_writable.js:409:139)
    at writeOrBuffer (node_modules/tedious/node_modules/readable-stream/lib/_stream_writable.js:398:5)
    at Message.Writable.write (node_modules/tedious/node_modules/readable-stream/lib/_stream_writable.js:307:11)
    at IncomingMessageStream.processBufferedData (adaptors/database/rdbms/mssql/mssqlUtil.js:463:27)
    at IncomingMessageStream._transform (node_modules/tedious/lib/incoming-message-stream.js:107:10)
    at IncomingMessageStream.Transform._read (node_modules/tedious/node_modules/readable-stream/lib/_stream_transform.js:177:10)
    at IncomingMessageStream.Transform._write (node_modules/tedious/node_modules/readable-stream/lib/_stream_transform.js:164:83)
    at doWrite (node_modules/tedious/node_modules/readable-stream/lib/_stream_writable.js:409:139)
    at writeOrBuffer (node_modules/tedious/node_modules/readable-stream/lib/_stream_writable.js:398:5)
    at IncomingMessageStream.Writable.write (node_modules/tedious/node_modules/readable-stream/lib/_stream_writable.js:307:11)
    at TLSSocket.ondata (_stream_readable.js:728:22)
    at TLSSocket.EventEmitter.emit (domain.js:475:20)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:290:11)
    at TLSSocket.Readable.push (_stream_readable.js:224:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:181:23)
    at DuplexSocket.ondata (internal/js_stream_socket.js:68:22)
    at DuplexSocket.EventEmitter.emit (domain.js:475:20)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:290:11)
    at DuplexSocket.Readable.push (_stream_readable.js:224:10)
    at DuplexSocket._write (node_modules/native-duplexpair/index.js:24:22)
    at doWrite (_stream_writable.js:435:12)
    at writeOrBuffer (_stream_writable.js:419:5)
    at DuplexSocket.Writable.write (_stream_writable.js:309:11)
    at Socket.ondata (_stream_readable.js:728:22)
    at Socket.EventEmitter.emit (domain.js:475:20)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:290:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:181:23)
    at TCP. (node_modules/async-listener/glue.js:188:31)

  6. arpitgupta2405 commented on Jan 22, 2021

    @arpitgupta2405
    Author

    @arthurschreiber , please note that I'm trying to do a select in a table having ~500 columns (all of varchar type)

  7. arpitgupta2405 commented on Feb 9, 2021

    @arpitgupta2405
    Author

    @arthurschreiber , any update on this?

  8. arpitgupta2405 commented on Feb 23, 2021

    @arpitgupta2405
    Author

    @arthurschreiber , gentle reminder.

  9. arthurschreiber commented on Mar 17, 2021

    @arthurschreiber
    Collaborator

    @arpitgupta2405 @IanChokS and I opened #1240, which I think should fix the issue you've been running into. Would you mind checking that out and see if that fixes your problem? 🙇‍♂️

  10. github-actions commented on Mar 25, 2021

    @github-actions
    Contributor

    🎉 This issue has been resolved in version 11.0.7 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

  11. arthurschreiber commented on Mar 25, 2021

    @arthurschreiber
    Collaborator

    We just released [email protected] to the tedious@next release channel. We'd be happy to hear back from you if this fixes the issue you're describing here.

    If no issues come up, this will be promoted to the tedious@latest release channel automatically in a week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions