Skip to content

Commit af47148

Browse files
authored
fix(stream): premature close when using for await (#2389)
* Fix node 18 (based on #711) * Add tests to query stream * fix async error * fix indentation
1 parent e6a9306 commit af47148

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lib/commands/query.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ class Query extends Command {
279279
});
280280
this.on('end', () => {
281281
stream.push(null); // pushing null, indicating EOF
282-
stream.emit('close'); // notify readers that query has completed
282+
setImmediate(() => stream.emit('close')); // notify readers that query has completed
283283
});
284284
this.on('fields', fields => {
285285
stream.emit('fields', fields); // replicate old emitter

test/integration/connection/test-stream.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const assert = require('assert');
77
let rows;
88
const rows1 = [];
99
const rows2 = [];
10+
const rows3 = [];
1011

1112
connection.query(
1213
[
@@ -45,7 +46,7 @@ connection.execute(
4546
}
4647
}
4748
);
48-
connection.execute('SELECT * FROM announcements', (err, _rows) => {
49+
connection.execute('SELECT * FROM announcements', async (err, _rows) => {
4950
rows = _rows;
5051
const s1 = connection.query('SELECT * FROM announcements').stream();
5152
s1.on('data', row => {
@@ -60,10 +61,15 @@ connection.execute('SELECT * FROM announcements', (err, _rows) => {
6061
connection.end();
6162
});
6263
});
64+
const s3 = connection.query('SELECT * FROM announcements').stream();
65+
for await (const row of s3) {
66+
rows3.push(row);
67+
}
6368
});
6469

6570
process.on('exit', () => {
6671
assert.deepEqual(rows.length, 2);
6772
assert.deepEqual(rows, rows1);
6873
assert.deepEqual(rows, rows2);
74+
assert.deepEqual(rows, rows3);
6975
});

0 commit comments

Comments
 (0)