Skip to content

Commit cc34a83

Browse files
fix: destroy connection when stream errors (#3769)
* Destroy connection when stream is destroyed * Only destroy connection on stream error * Add test case * Shorten timeout * Update test/integration/connection/test-stream-error-destroy-connection.test.cjs Co-authored-by: Weslley Araújo <[email protected]> --------- Co-authored-by: Weslley Araújo <[email protected]>
1 parent 9c51eb9 commit cc34a83

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

lib/commands/query.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,9 @@ class Query extends Command {
295295
stream.on('end', () => {
296296
stream.emit('close');
297297
});
298+
stream.on('error', () => {
299+
this._connection && this._connection.destroy();
300+
});
298301
return stream;
299302
}
300303

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
const process = require('node:process');
4+
const { test, skip } = require('poku');
5+
const common = require('../../common.test.cjs');
6+
7+
if (process.env.MYSQL_USE_TLS === '1') skip('Skipping for SSL=1');
8+
9+
test('Ensure stream ends in case of error', async () => {
10+
const connection = common.createConnection();
11+
12+
connection.query(
13+
[
14+
'CREATE TEMPORARY TABLE `items` (',
15+
'`id` int(11) NOT NULL AUTO_INCREMENT,',
16+
'`text` varchar(255) DEFAULT NULL,',
17+
'PRIMARY KEY (`id`)',
18+
') ENGINE=InnoDB DEFAULT CHARSET=utf8',
19+
].join('\n'),
20+
(err) => {
21+
if (err) {
22+
throw err;
23+
}
24+
}
25+
);
26+
27+
for (let i = 0; i < 100; i++) {
28+
connection.execute('INSERT INTO items(text) VALUES(?)', ['test'], (err) => {
29+
if (err) {
30+
throw err;
31+
}
32+
});
33+
}
34+
35+
const rows = connection.query('SELECT * FROM items').stream();
36+
37+
// eslint-disable-next-line no-unused-vars
38+
for await (const _ of rows) break;
39+
40+
setTimeout(() => {
41+
throw new Error('Connection remains open after stream error');
42+
}, 1000).unref();
43+
44+
connection.end();
45+
});

0 commit comments

Comments
 (0)