Skip to content

Transactions do not reuse the connector on dispose - #3266

Merged
roji merged 14 commits into
npgsql:mainfrom
vonzshik:transaction-dispose-connector-reuse-fix
Nov 1, 2020
Merged

roji merged 14 commits into
npgsql:mainfrom
vonzshik:transaction-dispose-connector-reuse-fix

Conversation

@vonzshik

Copy link
Copy Markdown
Contributor

I went with a simple way of creating a new transaction.

@roji

roji commented Oct 25, 2020

Copy link
Copy Markdown
Member

I think I have another solution which doesn't cause allocations for every new transaction, without having to go through #3260... The problem is the that binding scope is ended as soon as we see the ReadyForQuery protocol message with an idle transaction status. We can delay that and do it inside NpgsqlTransaction.Commit (and Rollback) instead - this would give us the opportunity to terminate the user action before we return the connection to the pool (just like you did for other operations in #3254).

(if we do this, then it should become unsupported to do COMMIT or ROLLBACK in raw SQL when multiplexing is on, just like it's unsupported to start a new transaction with BEGIN).

Do you want to give this a try?

@vonzshik

Copy link
Copy Markdown
Contributor Author

I think I have another solution which doesn't cause allocations for every new transaction, without having to go through #3260... The problem is the that binding scope is ended as soon as we see the ReadyForQuery protocol message with an idle transaction status. We can delay that and do it inside NpgsqlTransaction.Commit (and Rollback) instead - this would give us the opportunity to terminate the user action before we return the connection to the pool (just like you did for other operations in #3254).

(if we do this, then it should become unsupported to do COMMIT or ROLLBACK in raw SQL when multiplexing is on, just like it's unsupported to start a new transaction with BEGIN).

Do you want to give this a try?

I think I know what you're talking about. This could be done similar to how COPY operations deal with the same problem.

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As your fixes to the tests show, if anyone isn't disposing their transactions, this change will start being potentially problematic. I know that DbTransaction is disposable so people are supposed to dispose, but the reality is that making a type suddenly care about disposal is risky.

Is there any reason not to just do this earlier in commit/rollback? I think it may even simplify the code a bit.

Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
@vonzshik

Copy link
Copy Markdown
Contributor Author

Is there any reason not to just do this earlier in commit/rollback? I think it may even simplify the code a bit.

Dispose. On dispose, it can rollback the transaction on the connector. If we return the connector to the pool on transaction's commit/rollback, we should never dispose after that.
The only other solution I see is to not rollback on dispose.

@roji

roji commented Oct 25, 2020

Copy link
Copy Markdown
Member

On dispose, it can rollback the transaction on the connector. If we return the connector to the pool on transaction's commit/rollback, we should never dispose after that.

Dispose can rollback, but only if rollback wasn't already called before. So if we always just end the scope on commit/rollback, most of the times it will happen early (when the user calls commit/rollback), and sometimes it will happen when disposing (when the user disposes the transaction without calling commit/rollback). Where do you see an issue?

@vonzshik

Copy link
Copy Markdown
Contributor Author

On dispose, it can rollback the transaction on the connector. If we return the connector to the pool on transaction's commit/rollback, we should never dispose after that.

Dispose can rollback, but only if rollback wasn't already called before. So if we always just end the scope on commit/rollback, most of the times it will happen early (when the user calls commit/rollback), and sometimes it will happen when disposing (when the user disposes the transaction without calling commit/rollback). Where do you see an issue?

You have only one NpgsqlTransaction per connector, instance of which you get while calling BeginTransaction. After the transaction is commited/rollbacked, you still have the reference to the connector, which is already in the pool. And if you attempt to dispose your NpgsqlTransaction instance after that, it can rollback something on the connector, which is already taken by some other connection.

@roji

roji commented Oct 25, 2020

Copy link
Copy Markdown
Member

I must be missing something... Let's assume we move ending the scope to Commit/Rollback as I suggest.

If the user calls Rollback explicitly, the scope is ended at that point and the connector is returned to the pool. If the user disposes the transaction after that, IsCompleted is true and we do nothing.

If the user doesn't call Rollback and then calls Dispose, IsCompleted is false and we call Rollback. At that point again, the scope is ended and the connector is returned to the pool.

Where's the issue?

@vonzshik

Copy link
Copy Markdown
Contributor Author

I must be missing something... Let's assume we move ending the scope to Commit/Rollback as I suggest.

If the user calls Rollback explicitly, the scope is ended at that point and the connector is returned to the pool. If the user disposes the transaction after that, IsCompleted is true and we do nothing.

Yeah, the connector is in the pool. After that, another connection is open and it takes that exact connector and opens another transaction. Which you will rollback in dispose.

@roji

roji commented Oct 25, 2020

Copy link
Copy Markdown
Member

You're right.

In that case, we may want to think a bit more about existing code which does not dispose transactions. Right now, it's totally OK to begin a transaction, commit/rollback, and then begin another transaction on the same connection (without disposing the first). This is because our "transaction already in progress" error checks the connector's transaction status, and it is idle by that point (i.e. it doesn't care if the previous transaction has been disposed or not).

One thing we could do is to be stricter, and start throwing if an undisposed transaction exists (we could do this only in multiplexing, where it matters, or in general). But don't underestimate the amount of existing code out there (as well as possibly ORMs and other layers) which may hit this.

Another possibility is to simply make this work. In other words, if a new transaction is started, and the previous one is undisposed, we simply "take over" the connector (it never gets returned to the pool).

What do you think?

@vonzshik

Copy link
Copy Markdown
Contributor Author

Another possibility is to simply make this work. In other words, if a new transaction is started, and the previous one is undisposed, we simply "take over" the connector (it never gets returned to the pool).

This would work as long as dispose wouldn't rollback.

@roji

roji commented Oct 25, 2020

Copy link
Copy Markdown
Member

This would work as long as dispose wouldn't rollback.

Sure. If the user disposes the transaction and rolls back, it's perfectly fine to close the binding scope etc. My main point is about maintaining compatibility for existing code which does not dispose.

@vonzshik
vonzshik requested a review from roji October 26, 2020 15:45

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@vonzshik looks great to me - except for one question about double EndBindingScope.

Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlTransaction.cs Resolved
@roji

roji commented Oct 29, 2020

Copy link
Copy Markdown
Member

@vonzshik good points made offline. Some thoughts:

  • We can't return the connector when committing/rolling back, because the user may call dispose later - at which point we no longer own the transaction instance (as it's recycled)
  • We can require the user the always dispose, but that's quite the breaking change for code which doesn't do it. Besides, I think all ADO.NET connectors allow you to begin a transaction, and then close the connection without doing anything to it - the transaction is implicitly rolled back. This rollback-on-close behavior is important as there's a lot of code out there where an exception may be thrown, causing the connection to be disposed immediately (without taking care of the transaction).
  • As you suggest, one way to solve this is to stop recycling NpgsqlTransaction instances. I'd rather avoid this if possible (for perf).
  • However, as I wrote above, we already do rollback-on-close; the same logic can apply to the NpgsqlTransaction object - if it hasn't been disposed, dispose it at that point (which would return it to the pool). This would extend the logic that we already have, effectively adding a new transaction state to the connector (completed but undisposed).

What do you think, do you see any problems?

@vonzshik
vonzshik requested a review from roji October 30, 2020 05:38
@vonzshik

Copy link
Copy Markdown
Contributor Author

Okay @roji, now it should work more or less how we've discussed.

Comment thread src/Npgsql/NpgsqlConnection.cs Resolved
@vonzshik
vonzshik requested a review from roji October 30, 2020 21:30

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, looks good!

@roji
roji merged commit f4c3da7 into npgsql:main Nov 1, 2020
@vonzshik
vonzshik deleted the transaction-dispose-connector-reuse-fix branch November 1, 2020 09:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants