Skip to content

Commit

Permalink
fix(stream): premature close when using for await (#2389)
Browse files Browse the repository at this point in the history
* Fix node 18 (based on #711)

* Add tests to query stream

* fix async error

* fix indentation
  • Loading branch information
nofarham committed Jan 22, 2024
1 parent e6a9306 commit af47148
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/commands/query.js
Expand Up @@ -279,7 +279,7 @@ class Query extends Command {
});
this.on('end', () => {
stream.push(null); // pushing null, indicating EOF
stream.emit('close'); // notify readers that query has completed
setImmediate(() => stream.emit('close')); // notify readers that query has completed
});
this.on('fields', fields => {
stream.emit('fields', fields); // replicate old emitter
Expand Down
8 changes: 7 additions & 1 deletion test/integration/connection/test-stream.js
Expand Up @@ -7,6 +7,7 @@ const assert = require('assert');
let rows;
const rows1 = [];
const rows2 = [];
const rows3 = [];

connection.query(
[
Expand Down Expand Up @@ -45,7 +46,7 @@ connection.execute(
}
}
);
connection.execute('SELECT * FROM announcements', (err, _rows) => {
connection.execute('SELECT * FROM announcements', async (err, _rows) => {
rows = _rows;
const s1 = connection.query('SELECT * FROM announcements').stream();
s1.on('data', row => {
Expand All @@ -60,10 +61,15 @@ connection.execute('SELECT * FROM announcements', (err, _rows) => {
connection.end();
});
});
const s3 = connection.query('SELECT * FROM announcements').stream();
for await (const row of s3) {
rows3.push(row);
}
});

process.on('exit', () => {
assert.deepEqual(rows.length, 2);
assert.deepEqual(rows, rows1);
assert.deepEqual(rows, rows2);
assert.deepEqual(rows, rows3);
});

0 comments on commit af47148

Please sign in to comment.