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

Connection errors are not propagated to the query stream #1847

Open
dsech opened this issue Feb 16, 2023 · 5 comments · May be fixed by #1899
Open

Connection errors are not propagated to the query stream #1847

dsech opened this issue Feb 16, 2023 · 5 comments · May be fixed by #1899

Comments

@dsech
Copy link

dsech commented Feb 16, 2023

Sample code:

import { createPool } from "mysql2";

const connection = createPool({
    host: 'localhost',
    user: 'user',
    password: 'pass',
  });
try {
  for await (const row of connection.query("SELECT * FROM big_table").stream()) {
    // handle row here
  }
} catch (e) {
  // connection errors are not caught here
  console.log('caught error', e);
}

If any connection errors occur during the command execution, they will not be caught while iterating over the rows.

After investigation I found the issue to be in Connection.notifyError here

if (this._command && this._command.onResult) {
this._command.onResult(err);
this._command = null;
// connection handshake is special because we allow it to be implicit
// if error happened during handshake, but there are others commands in queue
// then bubble error to other commands and not to connection
} else if (

From what I see, the error will be passed to the command only if the command was executed with a callback.

A solution that seems to fix the issue is to also emit the error to the command like this:

if (this._command) {
  this._command.emit('error', err);
}

With the above change the error will be emitted to the command, and will also be thrown when iterating over the stream.

Can be reproduced by running a slow query like SELECT SLEEP(30) and then issue a KILL "connection id"

@dsech dsech changed the title Connection errors are not emitted to the query stream Connection errors are not propagated to the query stream Feb 16, 2023
@mrx8
Copy link

mrx8 commented Mar 13, 2023

I have the same problem here. Obviously nobody is using streaming. But this is far more efficient with larger results.
Can you please take a look at @dsech finding ?
BTW: this is not only for connection-errors, but obviously for every error which is send during a query from the server.

@sidorares
Copy link
Owner

@mrx8 I agree, this part of the codebase needs some more attention

If you have some capacity any help would be helpful, especially defining an issue ( ideally a failing unit test per issue )

@dsech
Copy link
Author

dsech commented Mar 14, 2023

@sidorares I've tried to create a failing unit test for my scenario, but it seems my issue is occurring only when I'm running the code in ECMAScript modules (mjs files).

Are mjs files supported in mysql2 test runner?

@sidorares
Copy link
Owner

Are mjs files supported in mysql2 test runner?

currently CI matrix includes node 16.x and 18.x so in theory should be ok. I'm not sure if any tweak to path name pattern required, it might be that only *.js files are searched ( we use https://www.npmjs.com/package/urun . I do plan to upgrade to something more modern, probably a built in node test runner )

dsech pushed a commit to dsech/node-mysql2 that referenced this issue Mar 15, 2023
@mrx8
Copy link

mrx8 commented May 22, 2023

@sidorares Have you already had time to look at the PR from @dsech, which reproduces this issue ?

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 a pull request may close this issue.

3 participants