Skip to content

Unhandled 'error' event with pooled connections #2049

@yashgt

Description

@yashgt

My application starts fine and runs for a few hours. Then all of a sudden I see the error:

events.js:165
throw er; // Unhandled 'error' event
^

> Error: Connection lost: The server closed the connection.

at Protocol.end (/home/ec2-user/production/code/admin/node_modules/mysql/lib/protocol/Protocol.js:113:13)
at Socket.<anonymous> (/home/ec2-user/production/code/admin/node_modules/mysql/lib/Connection.js:109:28)
at Socket.emit (events.js:185:15)
at endReadableNT (_stream_readable.js:1106:12)
at process._tickCallback (internal/process/next_tick.js:114:19)

Emitted 'error' event at:
at Connection._handleProtocolError (/home/ec2-user/production/code/admin/node_modules/mysql/lib/Connection.js:433:8)
at Protocol.emit (events.js:180:13)
at Protocol._delegateError (/home/ec2-user/production/code/admin/node_modules/mysql/lib/protocol/Protocol.js:392:10)
at Protocol.end (/home/ec2-user/production/code/admin/node_modules/mysql/lib/protocol/Protocol.js:117:8)
at Socket. (/home/ec2-user/production/code/admin/node_modules/mysql/lib/Connection.js:109:28)
[... lines matching original stack trace ...]

This error comes when no user is doing any action on the application. The system is idle.

I have the following code:

exports.createPool = function createPool(dbConfig)
{
        logger.debug('Creating pool');
        pool = mysql.createPool(dbConfig);
        pool.on('connection', function (connection) {
                logger.info('Setting error handler for {0}',connection);
                connection.on('error', function(err) {
                        logger.error(err); // 'ER_BAD_DB_ERROR'
                });
        });
        pool.on('release', function(connection) {
                logger.error('Releasing {0}', connection); //
        });
};

If a connection goes in Error, I expect the handler to be called. It does get called and that is the reason we see:
Error: Connection lost: The server closed the connection.
I also expect the connection to get released and the pool to generate another connection.

Even though I have applied an error handler, it says the 'error' even is not handled and the application crashes.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions