From c67155398d949f7231383001b69139475f3e65f5 Mon Sep 17 00:00:00 2001 From: Yan Ivan Evdokimov Date: Tue, 30 Jan 2024 14:31:58 +0200 Subject: [PATCH 1/3] Fix premature close --- lib/commands/query.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/commands/query.js b/lib/commands/query.js index 1b3725d73f..67386bbfba 100644 --- a/lib/commands/query.js +++ b/lib/commands/query.js @@ -30,7 +30,7 @@ class Query extends Command { this._receivedFieldsCount = 0; this._resultIndex = 0; this._localStream = null; - this._unpipeStream = function() {}; + this._unpipeStream = function () { }; this._streamFactory = options.infileStreamFactory; this._connection = null; } @@ -155,7 +155,7 @@ class Query extends Command { const onPause = () => { this._localStream.pause(); }; - const onData = function(data) { + const onData = function (data) { const dataWithHeader = Buffer.allocUnsafe(data.length + 4); data.copy(dataWithHeader, 4); connection.writePacket( @@ -227,7 +227,7 @@ class Query extends Command { } /* eslint no-unused-vars: ["error", { "argsIgnorePattern": "^_" }] */ - row(packet, _connection) { + row(packet, _connection) { if (packet.isEOF()) { const status = packet.eofStatusFlags(); const moreResults = status & ServerStatus.SERVER_MORE_RESULTS_EXISTS; @@ -279,11 +279,13 @@ class Query extends Command { }); this.on('end', () => { stream.push(null); // pushing null, indicating EOF - setImmediate(() => stream.emit('close')); // notify readers that query has completed }); this.on('fields', fields => { stream.emit('fields', fields); // replicate old emitter }); + stream.on('end', () => { + stream.emit('close'); + }); return stream; } @@ -302,7 +304,7 @@ class Query extends Command { Timers.clearTimeout(this.queryTimeout); this.queryTimeout = null; } - + const err = new Error('Query inactivity timeout'); err.errorno = 'PROTOCOL_SEQUENCE_TIMEOUT'; err.code = 'PROTOCOL_SEQUENCE_TIMEOUT'; From f2d2d3a6d011e633fd31b518e61d6ec649c235bf Mon Sep 17 00:00:00 2001 From: Yan Ivan Evdokimov Date: Wed, 31 Jan 2024 10:26:09 +0200 Subject: [PATCH 2/3] Add test for premature close of the stream when it is paused --- package-lock.json | 6 ++---- test/integration/connection/test-stream.js | 7 +++++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 14b017af01..e1d44afbcf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3443,8 +3443,7 @@ "version": "5.3.2", "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz", "integrity": "sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==", - "dev": true, - "requires": {} + "dev": true }, "ajv": { "version": "6.12.6", @@ -3868,8 +3867,7 @@ "version": "9.1.0", "resolved": "https://registry.npmjs.org/eslint-config-prettier/-/eslint-config-prettier-9.1.0.tgz", "integrity": "sha512-NSWl5BFQWEPi1j4TjVNItzYV7dZXZ+wP6I6ZhrBGpChQhZRUaElihE9uRRkcbRnNb76UMKDF3r+WTmNcGPKsqw==", - "dev": true, - "requires": {} + "dev": true }, "eslint-plugin-async-await": { "version": "0.0.0", diff --git a/test/integration/connection/test-stream.js b/test/integration/connection/test-stream.js index 57ac6dd5ba..3a2b32368f 100644 --- a/test/integration/connection/test-stream.js +++ b/test/integration/connection/test-stream.js @@ -8,6 +8,7 @@ let rows; const rows1 = []; const rows2 = []; const rows3 = []; +const rows4 = []; connection.query( [ @@ -65,6 +66,11 @@ connection.execute('SELECT * FROM announcements', async (err, _rows) => { for await (const row of s3) { rows3.push(row); } + const s4 = connection.query('SELECT * FROM announcements').stream(); + for await (const row of s4) { + await new Promise(resolve => setTimeout(resolve, 1000)); + rows4.push(row); + } }); process.on('exit', () => { @@ -72,4 +78,5 @@ process.on('exit', () => { assert.deepEqual(rows, rows1); assert.deepEqual(rows, rows2); assert.deepEqual(rows, rows3); + assert.deepEqual(rows, rows4); }); From 357fa4ef9103c13b6cec0c882940cd0bd0e21b90 Mon Sep 17 00:00:00 2001 From: Yan Ivan Evdokimov Date: Wed, 31 Jan 2024 10:43:31 +0200 Subject: [PATCH 3/3] Restore package-lock.json --- package-lock.json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index e1d44afbcf..14b017af01 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3443,7 +3443,8 @@ "version": "5.3.2", "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz", "integrity": "sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==", - "dev": true + "dev": true, + "requires": {} }, "ajv": { "version": "6.12.6", @@ -3867,7 +3868,8 @@ "version": "9.1.0", "resolved": "https://registry.npmjs.org/eslint-config-prettier/-/eslint-config-prettier-9.1.0.tgz", "integrity": "sha512-NSWl5BFQWEPi1j4TjVNItzYV7dZXZ+wP6I6ZhrBGpChQhZRUaElihE9uRRkcbRnNb76UMKDF3r+WTmNcGPKsqw==", - "dev": true + "dev": true, + "requires": {} }, "eslint-plugin-async-await": { "version": "0.0.0",