Skip to content

debug is logging undefined as the error code #8756

Closed

Description

@guyellis

What are you doing?

Tracing the debug statements with:

DEBUG=sequelize:connection:mysql,sequelize:pool

What do you expect to happen?

I expected the error code to have a value.

What is actually happening?

The error code does not have a value (see second "connection error" in logs below):

2017-12-06T00:23:53.229+00:00: 2017-12-06T00:23:53.229Z sequelize:connection:mysql connection acquired
2017-12-06T00:25:53.163+00:00: 2017-12-06T00:25:53.161Z sequelize:connection:mysql connection disconnected
2017-12-06T00:25:53.165+00:00: 2017-12-06T00:25:53.164Z sequelize:connection:mysql connection error EPIPE
2017-12-06T00:25:53.166+00:00: 2017-12-06T00:25:53.165Z sequelize:pool connection destroy
2017-12-06T00:25:53.283+00:00: 2017-12-06T00:25:53.283Z sequelize:connection:mysql connection acquired
2017-12-06T00:26:58.236+00:00: 2017-12-06T00:26:58.235Z sequelize:connection:mysql connection error undefined
2017-12-06T00:26:58.354+00:00: 2017-12-06T00:26:58.354Z sequelize:connection:mysql connection acquired
2017-12-06T00:26:58.369+00:00: 2017-12-06T00:26:58.369Z sequelize:pool connection acquired
2017-12-06T00:26:58.390+00:00: 2017-12-06T00:26:58.389Z sequelize:pool connection released
2017-12-06T00:26:58.486+00:00: 2017-12-06T00:26:58.486Z sequelize:pool connection acquired
2017-12-06T00:26:58.491+00:00: 2017-12-06T00:26:58.491Z sequelize:pool connection released

In the logs above the error is undefined on one of the lines. This comes from this line which is probably going to move at some point in the future so I'll paste it here:

debug(`connection error ${e.code}`);

Dialect: mysql2
Dialect version: 1.5.1
Database version: don't know
Sequelize version: 4.26.0
Tested with latest release: Yes 4.26.0

Under what circumstance would e.code be undefined?

Activity

  1. sushantdhiman commented on Dec 6, 2017

    @sushantdhiman
    Contributor

    Have you tried inspecting whole error, Please share that output.

    On a side note, on any github code page you can press y and it will generate a commit based permalink which wont change, like this

  2. guyellis commented on Dec 6, 2017

    @guyellis
    ContributorAuthor

    @sushantdhiman I did not know that about the y - that is an awesome side note - thank you!

    How do I inspect the "whole error" - Is there an easy way to access that or do I need to modify that line to print the whole error?

  3. sushantdhiman commented on Dec 6, 2017

    @sushantdhiman
    Contributor

    do I need to modify that line to print the whole error?

    Yes

  4. guyellis commented on Dec 6, 2017

    @guyellis
    ContributorAuthor

    @sushantdhiman I updated that debug line to:

    if (!e.code) {
    debug(`connection undefined error ${JSON.stringify(e)}`);
    } else {
    debug(`connection error ${e.code}`);
    }

    and this is what I'm seeing:

    ...:43:57.081Z sequelize:connection:mysql connection undefined error {"fatal":true}
  5. sushantdhiman commented on Dec 6, 2017

    @sushantdhiman
    Contributor

    can you retry with

    if (!e.code) {
       console.trace(e);
    } else {
       debug(`connection error ${e.code}`);
    }
  6. guyellis commented on Dec 6, 2017

    @guyellis
    ContributorAuthor

    @sushantdhiman here it is:

    2017-12-06T17:17:35.977+00:00: Trace: { Error: Can't add new command when connection is in closed state
        at Connection._addCommandClosedState (/home/myapp/node_modules/mysql2/lib/connection.js:158:13)
        at Connection.end (/home/myapp/node_modules/mysql2/lib/connection.js:948:22)
        at Utils.Promise (/home/myapp/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:167:18)
        at Promise._execute (/home/myapp/node_modules/sequelize/node_modules/bluebird/js/release/debuggability.js:303:9)
        at Promise._resolveFromExecutor (/home/myapp/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:483:18)
        at new Promise (/home/myapp/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:79:10)
        at ConnectionManager.disconnect (/home/myapp/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:166:12)
        at ConnectionManager._disconnect (/home/myapp/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:339:43)
        at Object.destroy (/home/myapp/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:114:23)
        at Pool._destroy (/home/myapp/node_modules/generic-pool/lib/Pool.js:129:42)
        at _trackOperation.then (/home/myapp/node_modules/generic-pool/lib/Pool.js:163:14)
        at bound (domain.js:280:14)
        at runBound (domain.js:293:12)
        at tryCatcher (/home/myapp/node_modules/sequelize/node_modules/bluebird/js/release/util.js:16:23)
        at Promise._settlePromiseFromHandler (/home/myapp/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:512:31)
        at Promise._settlePromise (/home/myapp/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:569:18)
        at Promise._settlePromise0 (/home/myapp/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:614:10)
        at Promise._settlePromises (/home/myapp/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:693:18)
        at Async._drainQueue (/home/myapp/node_modules/sequelize/node_modules/bluebird/js/release/async.js:133:16)
        at Async._drainQueues (/home/myapp/node_modules/sequelize/node_modules/bluebird/js/release/async.js:143:10)
        at Immediate.Async.drainQueues (/home/myapp/node_modules/sequelize/node_modules/bluebird/js/release/async.js:17:14)
        at Immediate.wrapped (/home/myapp/node_modules/newrelic/lib/transaction/tracer/index.js:184:28)
        at runCallback (timers.js:672:20)
        at tryOnImmediate (timers.js:645:5)
        at processImmediate [as _immediateCallback] (timers.js:617:5) fatal: true }
        at Connection.errorHandler (/home/myapp/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:102:21)
        at emitOne (events.js:96:13)
        at Connection.emit (events.js:188:7)
        at Connection._addCommandClosedState (/home/myapp/node_modules/mysql2/lib/connection.js:165:10)
        at Connection.end (/home/myapp/node_modules/mysql2/lib/connection.js:948:22)
        at Utils.Promise (/home/myapp/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:167:18)
        at Promise._execute (/home/myapp/node_modules/sequelize/node_modules/bluebird/js/release/debuggability.js:303:9)
        at Promise._resolveFromExecutor (/home/myapp/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:483:18)
        at new Promise (/home/myapp/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:79:10)
        at ConnectionManager.disconnect (/home/myapp/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:166:12)
        at ConnectionManager._disconnect (/home/myapp/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:339:43)
        at Object.destroy (/home/myapp/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:114:23)
        at Pool._destroy (/home/myapp/node_modules/generic-pool/lib/Pool.js:129:42)
        at _trackOperation.then (/home/myapp/node_modules/generic-pool/lib/Pool.js:163:14)
        at bound (domain.js:280:14)
        at runBound (domain.js:293:12)
        at tryCatcher (/home/myapp/node_modules/sequelize/node_modules/bluebird/js/release/util.js:16:23)
        at Promise._settlePromiseFromHandler (/home/myapp/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:512:31)
        at Promise._settlePromise (/home/myapp/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:569:18)
        at Promise._settlePromise0 (/home/myapp/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:614:10)
        at Promise._settlePromises (/home/myapp/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:693:18)
        at Async._drainQueue (/home/myapp/node_modules/sequelize/node_modules/bluebird/js/release/async.js:133:16)
        at Async._drainQueues (/home/myapp/node_modules/sequelize/node_modules/bluebird/js/release/async.js:143:10)
        at Immediate.Async.drainQueues (/home/myapp/node_modules/sequelize/node_modules/bluebird/js/release/async.js:17:14)
        at Immediate.wrapped (/home/myapp/node_modules/newrelic/lib/transaction/tracer/index.js:184:28)
        at runCallback (timers.js:672:20)
        at tryOnImmediate (timers.js:645:5)
        at processImmediate [as _immediateCallback] (timers.js:617:5)
    
  7. sushantdhiman commented on Dec 7, 2017

    @sushantdhiman
    Contributor

    Initial idea from logs and code, I think we are trying to close a already closed connection OR a closed connection received packets in between destroy and end phase.

  8. guyellis commented on Dec 7, 2017

    @guyellis
    ContributorAuthor

    @sushantdhiman would that point more towards a problem with the way that I'm using this module or a possible improvement that can be made in this module?

    Also, I was reading down through that file and came across this line and was wondering if instead of

    !connection.stream.destroyed;
    

    it should read

    connection.stream && !connection.stream.destroyed;
    

    or something similar to prevent a throw in the event that stream is undefined. (Unless of course it's impossible for stream to be undefined.) I mention it here and not in a separate issue in case it's related to what we're talking about in this issue - but it's probably not.

  9. guyellis commented on Dec 12, 2017

    @guyellis
    ContributorAuthor

    @sushantdhiman just checking in on this. Not sure if you read my previous comment. I'm trying to determine if you think that this is something that I can work around in my code or something that we need to fix in sequelize?

  10. sushantdhiman commented on Dec 13, 2017

    @sushantdhiman
    Contributor

    @guyellis You can experiment with that by passing custom validate function to pooling config (http://docs.sequelizejs.com/class/lib/sequelize.js~Sequelize.html#instance-constructor-constructor)

    But I doubt that its the actual issue, connection.stream.destroyed trying to access destroyed on undefined stream will give a TypeError

    I not sure about this but seems related to mysql2 OR way we handle connections OR may be pooling. Only conclusion I can draw from this error is that a closed connection is receiving packets. Will need more investigation to reach to a solid conclusion

  11. guyellis commented on Dec 13, 2017

    @guyellis
    ContributorAuthor

    @sushantdhiman thank you! How else can I help? Given that I am getting this error every now and then is there any other logging or data that I can gather that will help us get to the bottom of this? For example at that point in the code where we're seeing this (the console.trace() above) is there any other state information (pool/connections) that I can dump that would help us?

  12. sushantdhiman commented on Dec 13, 2017

    @sushantdhiman
    Contributor

    Most related issue for mysql2 sidorares/node-mysql2#447

    Its a guess (I dont have any test setup) that connection released to pool are dying (or killed by server) and sequelize then eventual got handed a closed connection.

    To debug this issue I suggest enabling mysql2 debug log as well and see if connection was closed in an idle pool (by idle pool I meant that we saturate pool by doing some db ops, then let those connection eventually close) and observe if that connection was then used by sequelize.

    To me this is most probable cause, as soon as sequelize detect connection is stale it request for new one and operation continues.

    One cause behind this could be high evict time with low idle time as noted in documentation for pool.idle

    The maximum time, in milliseconds, that a connection can be idle before being released. Use with combination of evict for proper working, for more details read coopernurse/node-pool#178 (comment)

  13. guyellis commented on Jan 3, 2018

    @guyellis
    ContributorAuthor

    Thanks @sushantdhiman - I'm hoping to circle back to this in a few weeks and do some more testing and digging...

  14. stale commented on Apr 3, 2018

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

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