Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use connection pause/resume to prevent kill race condition #7

Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions lib/dialects/mysql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ class Client_MySQL extends Client {
return;
}
if (!queryToKill.__query.ended()) {
connectionToKill.pause();
const conn = await this.acquireRawConnection();
try {
return await this._query(conn, {
Expand All @@ -175,6 +176,7 @@ class Client_MySQL extends Client {
options: {},
});
} finally {
connectionToKill.resume();
await this.destroyRawConnection(conn);
if (conn.__knex__disposed) {
this.logger.warn(`Connection Error: ${conn.__knex__disposed}`);
Expand Down
62 changes: 62 additions & 0 deletions test/integration/query/additional.js
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,68 @@ module.exports = function (knex) {
});
});

it('.timeout(ms, {cancel: true}) should not cancel subsequent queries in a transaction if timeouted query completes while being cancelled', async function () {
Copy link
Owner Author

@martinmacko47 martinmacko47 Apr 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test will fail without connectionToKill.pause()/resume() in Client_MySQL.cancelQuery() method. The test simulates a race-condition when the timeouted query manages to complete while we are trying to kill it. It is simulated by slowing down Client_MySQL.acquireRawConnection() method.

The current behaviour without connectionToKill.pause()/resume():

current

Fixed behaviour without the race condition with connectionToKill.pause()/resume():

expected

Btw, note that query B has no timeout, to make sure it won't timeout while waiting in the queue.

if (!isMysql(knex)) {
return this.skip();
}

const getProcessesForDriver = {
pg: async () => {
const results = await knex.raw('SELECT * from pg_stat_activity');
return _.map(_.filter(results.rows, {state: 'active'}), 'query');
},
mysql: async () => {
const results = await knex.raw('SHOW PROCESSLIST');
return _.map(results[0], 'Info');
},
mysql2: async () => {
const results = await knex.raw('SHOW PROCESSLIST');
return _.map(results[0], 'Info');
},
};

const driverName = knex.client.driverName;
if (!Object.prototype.hasOwnProperty.call(getProcessesForDriver, driverName)) {
throw new Error('Missing test query for driverName: ' + driverName);
}

const getProcesses = getProcessesForDriver[driverName];

return knex.transaction(async (trx) => {
const queryA = trx.raw('SELECT SLEEP(1), "A"');
const queryB = trx.raw('SELECT SLEEP(1), "B"');

const promise = Promise.all([
queryA.timeout(500, { cancel: true }),
queryB,
])

await delay(100);
const processesBeforeTimeout = await getProcesses();
expect(processesBeforeTimeout).to.include(queryA.toString());
expect(processesBeforeTimeout).to.not.include(queryB.toString());

const origAcquireRawConnection = trx.client.acquireRawConnection.bind(trx.client);
trx.client.acquireRawConnection = async () => {
await delay(600);
return origAcquireRawConnection();
}

await expect(promise).to.eventually.be.rejected.and.to.deep.include({
timeout: 500,
name: 'KnexTimeoutError',
message: 'Defined query timeout of 500ms exceeded when running query.',
});

trx.client.acquireRawConnection = origAcquireRawConnection;

await delay(100);
const processesAfterTimeout = await getProcesses();
expect(processesAfterTimeout).to.not.include(queryA.toString());
expect(processesAfterTimeout).to.include(queryB.toString());
});
});

it('.timeout(ms, {cancel: true}) should cancel slow query even if connection pool is exhausted', async function () {
// Only mysql/postgres query cancelling supported for now
if (!isMysql(knex) && !isPostgreSQL(knex)) {
Expand Down