-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Transaction finished race condition #5222
Description
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
Line 777 in 2a168da
| 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.