Repository navigation
Description
Update: below is the initial trouble report, which is remotely related. The true issue is in this comment.
When rollback-ing a distributed transaction, it appears the transaction changes are anyway persisted in the database.
This is reproduced with NHibernate rollback tests in its DtcFailuresFixture when targeting PostgreSql. The default Npgsql driver is v2.2.7. I have seen #122 which was looking promising, but unfortunately, upgrading my desktop to v3.2.2 for testing yields the same result: most distributed rollback tests failings. Those not failing are special cases where NHibernate does not even send any changes to the db.
The general pattern for those tests is:
- open a transaction scope
- acquire a connection
- perform some changes (insert delete or update)
- promote to distributed by enlisting a custom test durable ressource
- rollback
- query the db for checking the changes were not done.
Tests fail at point 6, the changes are found in database.
Notes:
- NHibernate may flush the changes to db from the prepare phase of its session, enlisted as volatile, using
EnlistmentOptions.EnlistDuringPrepareRequired. Or it flushes them directly from inside the scope. Tests covers both cases and fails on both. - The rollback may be triggered by not completing the scope, or by calling
ForceRollbackfrom the prepare phase of custom test durable ressource. Tests cover both cases and fail on both. - The checking query is done in all tests with a newly acquired connection. (Maybe the pool yields the previously used one.)
- In all cases, NHibernate enlists a volatile ressource in the transaction (the session).
The same rollback tests succeed with Sql-Server, Oracle and SQLite. (They fail similarly with Firebird 2.5.)
The links to NHibernate sources are on a forked branch, used for this PR about NHibernate own troubles with DTC. For more details about them, there is NH-4011.
On my own desktop (Windows 10 Home), I use PostgreSql 9.6.3 for tests, and Npgsql driver 3.2.2. NHibernate Teamcity builds uses PostgreSql 9.0.3 (Windows Server 2012 R2) and Npgsql driver 2.2.7. Same results.
Activity
fredericDelaporte commentedon May 24, 2017
Maybe the trouble is automatic enlistment is not enabled by default by the npgsql driver, contrary to what we expect from ado connections.
Going to test that.
fredericDelaporte commentedon May 24, 2017
Ok found #1483
fredericDelaporte commentedon May 24, 2017
And now the other non rollback tests start failing.
fredericDelaporte commentedon May 24, 2017
The remaining failing tests are flaky and illustrates in my opinion a common trouble with most 2PC implementations: the way they are coded, they fail to respect the transaction scope contract which specifies it leaves its dispose method only once the transaction is actually committed or rollback.
So for some database provider (including Npgsql now) we regularly encounter a race condition in the test causing the assert which tries to check the transaction outcome after scope disposal to fail. Probably because the transaction is not yet actually completed on the data provider side.
Just a wild guess, by I suspect this occurs when using a volatile ressource enlistment for the database connection instead of using a durable ressource enlistment. If that is it, yes this fact clearly lacks good documentation on Microsoft side.With Npgsql driver, this has two symptoms:
Npgsql.NpgsqlOperationInProgressException : The connection is already in state 'Executing')roji commentedon May 24, 2017
Thanks for this helpful discussion. I've already made enlisting on by default for 3.3 (#1483).
Regarding your other comment on the race condition, I'm curious.
TransactionScope.Dispose()is of course not implemented by specific providers - it's a System.Transactions method which calls commit (or rollback) on individual providers' enlisted resource managers. In Npgsql's case (starting from version 3.2), the resource manager is VolatileResourceManager, which AFAIK has no asynchronous element to it which could explain the race condition (i.e.Commit()blocks until the transaction is either committed or failed) - although your NpgsqlOperationInProgressException definitely points at some sort of Npgsql issue.What would help is a simple, clear code sample which reproduces your issues, preferably without any Hibernate layer above it - see the Npgsql tests in SystemTransactionTests.cs. You're also welcome to look into VolatileResourceManager yourself, it's not very complex.
As a side note, it's very unlikely that this is related to volatile vs. durable resource management - that distinction only affects what happens when your app crashes while the transaction is still in progress.
[-]Rollback not done on distributed transaction[/-][+]Race conditions with distributed transactions[/+]roji commentedon May 24, 2017
Am reopening and renaming this issue to track your issues.
fredericDelaporte commentedon May 24, 2017
Thanks for the pointers. I was envisioning writing some tests, and had already forked your repository. But of course my priority is NHibernate own troubles, so maybe I will not do that soon.
The concurrency troubles happen specifically when the transaction is promoted to distributed. We have witnessed in our own tests that the second phase of the 2PC can happen after the scope disposal, at least for volatile ressources we have enlisted. I am currently handling that in a PR with some synchronization logic between our volatile ressource and the NHibernate session, in order to block session usage till the second phase is actually ended. I start the lock at the end of the prepare phase, and release it at the end of the commit/rollback/in-doubt phase.
Of course I cannot apply such a solution to the second phase of third parties connections.
This facts contradicts msdn documentation. And since we do not have that occurring with
SqlConnectionorOdbcConnection, I think there is some undocumented pattern to respect when implementing the 2PC of a connection for avoiding those concurrency issues.Currently the sole thing I am thinking about is using a durable ressource enlistment instead of a volatile one. But as I have said, this is just a wild guess (or swag if you prefer).fredericDelaporte commentedon May 24, 2017
Some more backing about my swag: volatile ressources, as stated by msdn documentation, are usually meant to be non persisting ressources such as caches.
So here is my guess: even if such volatile resources have not finished their own commit, the transaction can still be considered already committed if all the durable ressources have committed. So performing their commit phase after letting the scope leave its disposal could made sens, if they have made the assumption that only durable ressource commit/rollback phase actually matter for considering the transaction persisted.roji commentedon May 24, 2017
@fredericDelaporte read up a bit more about volatile vs. durable. The only difference between the two is what happens after a crash, i.e. if it is possible to recover the transaction. There's not supposed to be any difference between the two if a crash doesn't occur, i.e. in the concurrency/race condition aspects of successful distributed transactions.
fredericDelaporte commentedon May 24, 2017
Still we have many times reproduced cases where the second phase of a volatile ressource was executed after the transaction scope disposal, and not yet done that with a durable ressource. (But not have tried either.) So either the msdn documentation about transaction scope being block until transaction commit or rollback is plain wrong, or maybe there are some other undocumented differences in how MSDTC handles volatile ressources vs durable ressources.
Update: Added tests in #1583 showcase the trouble with a durable ressource too.
roji commentedon May 24, 2017
If you can show that the problem only occurs with a volatile RM but never with a durable one, that would be good - otherwise I find it to be an unlikely direction...
Note that it's not inconceivable that there's asynchronous element causing the race condition on the PostgreSQL side (I have absolutely no basis for saying this - just mentioning it as a possibility).
Regardless, am very interested if you find more info on this.
49 remaining items
roji commentedon Jun 19, 2017
See PR with fixes: #1610
2PC async fixes to prevent race conditions
Another distributed race condition, npgsql#1571
Cleaning-up assertions according to MS DTC design, add a test for rol…
Raising the sleep on flaky test for npgsql#1571.
Another distributed race condition, #1571
Cleaning-up assertions according to MS DTC design, add a test for rol…
Raising the sleep on flaky test for #1571.
asehrli commentedon Aug 9, 2023
worth a dime