Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/commands/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ class Query extends Command {
stream.on('end', () => {
stream.emit('close');
});
stream.on('error', () => {
this._connection && this._connection.destroy();
});
return stream;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const process = require('node:process');
const { test, skip } = require('poku');
const common = require('../../common.test.cjs');

if (process.env.MYSQL_USE_TLS === '1') skip('Skipping for SSL=1');

test('Ensure stream ends in case of error', async () => {
const connection = common.createConnection();

connection.query(
[
'CREATE TEMPORARY TABLE `items` (',
'`id` int(11) NOT NULL AUTO_INCREMENT,',
'`text` varchar(255) DEFAULT NULL,',
'PRIMARY KEY (`id`)',
') ENGINE=InnoDB DEFAULT CHARSET=utf8',
].join('\n'),
(err) => {
if (err) {
throw err;
}
}
);

for (let i = 0; i < 100; i++) {
connection.execute('INSERT INTO items(text) VALUES(?)', ['test'], (err) => {
if (err) {
throw err;
}
});
}

const rows = connection.query('SELECT * FROM items').stream();

// eslint-disable-next-line no-unused-vars
for await (const _ of rows) break;

setTimeout(() => {
throw new Error('Connection remains open after stream error');
}, 1000).unref();

connection.end();
});
Loading