[CSHARP-2339] Disposing client session after transaction commit throws exception Created: 25/Jul/18  Updated: 28/Oct/23  Resolved: 26/Jul/18

Status: Closed
Project: C# Driver
Component/s: Session Management, Write Operations
Affects Version/s: 2.7.0
Fix Version/s: 2.7.1

Type: Bug Priority: Trivial - P5
Reporter: Wouter De Keersmaecker Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 10, C#, server version 4.0



 Description   

The following snippet of code throws an InvalidOperationException with message "Cannot call AbortTransaction after calling CommitTransaction."

 

using (var session = await database.Client.StartSessionAsync().ConfigureAwait(false))
{
	try
	{
		session.StartTransaction();		IMongoCollection<TDatabaseType> scheduleCollection = iface.database.GetCollection<TDatabaseType>(CollectionID);
		await scheduleCollection.InsertOneAsync(session, newObject).ConfigureAwait(false);		await session.CommitTransactionAsync().ConfigureAwait(false);
	}
	catch
	{
	    await session.AbortTransactionAsync();
	    throw;
	}
}

The exception is thrown on disposal of the client session, and upon closer inspection I found the following:

Dispose() of CoreSession:

if (this._currentTransaction != null)
{
	try
	{
		this.AbortTransaction(CancellationToken.None);
	}
	catch
	{
	}
}

In the Dispose function, a check is made to see if there is any transaction and if so it is aborted. However, after creating and commiting a transaction, _currentTransaction is not null but its state is "Committed". Could it be that a check for the state of _currentTransaction is missing, or am I using it wrong?

 

 



 Comments   
Comment by Githook User [ 06/Nov/18 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-2339: Only call AbortTransaction from Dispose when appropriate.
Branch: v2.7.x
https://github.com/mongodb/mongo-csharp-driver/commit/ad5aaf392d8d4b9b5a18d638960d120d1904f5b7

Comment by Githook User [ 26/Jul/18 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-2339: Only call AbortTransaction from Dispose when appropriate.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/4ea3f9c5fe7bd7c2e49bfdd112b79626e19cead5

Comment by Wouter De Keersmaecker [ 25/Jul/18 ]

Ah yes, I forgot I have first chance exceptions enabled in Visual Studio.

Comment by Robert Stam [ 25/Jul/18 ]

I can reproduce this, but it turns out this exception is being ignored (as you can see in the code snippet you included from CoreSession).

Still, we should not be throwing this exception at all.

Generated at Wed Feb 07 21:42:17 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.