Skip to content

Transaction finished race condition #5222

@greghart

Description

@greghart

There is logic in a transaction that basically flags a transaction as finished, which is then checked against when querying against that transaction. However, there is a race condition as both .rollback and .commit don't set this flag until after the "ROLLBACK" and "COMMIT" queries have succeeded.

See https://github.com/sequelize/sequelize/blob/master/lib/transaction.js#L209 that the flag is set in the finally
See the guard at

if (options.transaction && options.transaction.finished) {

Basically something like below is happening:

transaction.rollback()
  queryInterface.rollbackTransaction()
...
some other code gets to run and does an update
...
  .finally is triggered

The below code will reproduce

database.transaction(function(t){
  // Forced, but original use case was something like Promise.all([...]).catch (err) -> t.rollback()
  t.rollback();
  database.query("UPDATE entities SET name='something or other'");
});

Observe something like

Executing (5f84266c-9e62-4c96-8040-531e8d765ede): START TRANSACTION;
...
Executing (5f84266c-9e62-4c96-8040-531e8d765ede): ROLLBACK;
...
Executing (5f84266c-9e62-4c96-8040-531e8d765ede): UPDATE entities SET name`='something or other'

The update goes out even though the transaction was just rolled back.
I believe the fix would be to just set the finished flag right after sending the ROLLBACK out, rather than in the .finally.

Minor overall, as easily avoided by just making sure all your parallel requests are done before running the rollback.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions