Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recognize fatal mysql2 connection errors #2175

Conversation

novemberborn
Copy link
Contributor

Connections created by MySQL2 driver transition to broken state when
network errors or timeouts occur. This condition is not visible to the
connection pool in Knex.

If Knex detects a timeout and tries to close a connection, this causes
an uncaught exception from MySQL2. Similarly the connection pool never
realizes the connection is in a broken state, and thus will never
replace it.

Unfortunately the MySQL2 connection does not expose a state, but it can
be inferred by checking for connection._fatalError.

Fixes #1798


This is an alternative to #1853. I believe the issue is specific to the mysql2dialect, however (see #1798 (comment) which shows the mysql2 client being used). The workaround proposed in #1853 likely fights only the symptoms, but does not solve the underlying problem.

Ideally https://github.com/sidorares/node-mysql2 would expose the connection state, but it's probably worth fixing the problem in Knex even for current releases of mysql2.

It's not clear to me if there are tests for this dialect. To be quite frank though I'm planning to switch my (business) client's project to the mysql dialect once mysqljs/mysql#1776 lands there, so unfortunately I can't justify spending a lot of effort on fixing issues with mysql2.

Connections created by MySQL2 driver transition to broken state when
network errors or timeouts occur. This condition is not visible to the
connection pool in Knex.

If Knex detects a timeout and tries to close a connection, this causes
an uncaught exception from MySQL2. Similarly the connection pool never
realizes the connection is in a broken state, and thus will never
replace it.

Unfortunately the MySQL2 connection does not expose a state, but it can
be inferred by checking for `connection._fatalError`.

Fixes knex#1798
@sidorares
Copy link

Ideally https://github.com/sidorares/node-mysql2 would expose the connection state

do you know what's knex api for this? happy to add support on node-mysql2 side

@novemberborn
Copy link
Contributor Author

@sidorares I think validateConnection corresponds to the validate call in generic-pool. See the documentation.

What's needed then is an official way to check whether a connection is still valid. This PR assumes that connections are valid unless they have a _fatalError. It also assumes that connections that do not have a _fatalError can be ended. If that's not the case then there should be a proper way of checking that before calling end().

validateConnection() {
return true
validateConnection(connection) {
return !connection._fatalError
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is that ._fatalError attribute part of public API of mysql2 driver?

@elhigu
Copy link
Member

elhigu commented Aug 15, 2017

I suppose this is using private API of mysql2 driver, so it is very important to have some kind of test for this. Is ._fatalError just undefided or null before error happens?

@novemberborn
Copy link
Contributor Author

novemberborn commented Aug 15, 2017

I suppose this is using private API of mysql2 driver, so it is very important to have some kind of test for this. Is ._fatalError just undefided or null before error happens?

I think it's private, yes. It defaults to null: https://github.com/sidorares/node-mysql2/blob/b8f0b98e61e865736f4cc6917ce5fe70e6beff96/lib/connection.js#L84

@elhigu
Copy link
Member

elhigu commented Aug 16, 2017

@sidorares is there or could some public API be added to mysql2 driver to be able to get this?

@sidorares
Copy link

@elhigu I'm happy to add anything that would work for you, ideally in a way compatible with mysqljs/mysql

what if I wrap https://github.com/sidorares/node-mysql2/blob/b8f0b98e61e865736f4cc6917ce5fe70e6beff96/lib/connection.js#L84 into a getter function and "bless" it to be public api?

@elhigu
Copy link
Member

elhigu commented Aug 16, 2017

@sidorares sounds good 👍

@I00000
Copy link

I00000 commented Aug 24, 2017

👍

@elhigu
Copy link
Member

elhigu commented Oct 31, 2017

@sidorares was that checking of fatal error brought to public API?

@novemberborn if not, then just a testcase for making sure that fatal error attribute exists (to prevent regression) and rebasing the branch would be enough to get this in.

@novemberborn
Copy link
Contributor Author

This PR arose out of work I was doing for a client, and that contract has since ended (and they switched to Postgres). Unfortunately I won't have the time to pick this up from where I left off, so anybody reading this please feel free to take the commit and add a test case etc as necessary.

@elhigu
Copy link
Member

elhigu commented Nov 5, 2017

Will be ropened if someone takes over finishing this.

@elhigu elhigu closed this Nov 5, 2017
@fluxsauce
Copy link

I understand that this isn't currently being worked on, but this is still an open bug IMHO. Hit the exact same problem, and our solution is going to be to switch to mysql, not mysql2

@elhigu
Copy link
Member

elhigu commented Nov 29, 2017

@fluxsauce I couldn't tell from your comment if it was critique that this PR should not have been closed or just a clarification for people finding this issue that the bug still exist :)

Yes it is still open bug, related issue has not been closed. This PR was closed, because it is not been worked on, there is no benefit on leaving unfinished PRs open to bitrot.

@fluxsauce
Copy link

@elhigu ah, thanks for the clarification, I missed #1798 . Sorry about that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unhandled Exception in connection.end() for MySQL Dialect
5 participants