runtime icon indicating copy to clipboard operation
runtime copied to clipboard

TransactionScope Debugger behavior

Open wilmanwar opened this issue 10 years ago • 13 comments

I found a strange behavior when debugging code that involved TransactionScope:

var sqlCommand = new SqlCommand(); using (var ts = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted })) { sqlCommand.CommandText = "INSERT INTO TestTable1..."; sqlCommand.ExecuteNonQuery(); sqlCommand.CommandText = "INSERT INTO TestTable2..."; sqlCommand.ExecuteNonQuery(); ts.Complete(); }

by placing a break point where the TransactionScope instance is created in the using statement and hovering mouse pointer over the word 'new' I get a debugger Tooltip with information about a TransactionScope which seems to create an actual instance of TransactionScope. Once I attempt to step through the code and the program attempts to create the TransactionScope an Exception is thrown with the following text:

The transaction specified for TransactionScope has a different IsolationLevel than the value requested for the scope. Parameter name: transactionOptions.IsolationLevel

wilmanwar avatar Aug 30 '16 20:08 wilmanwar

It may also seem that the Debugger Tooltip might cause unintended behavior if it is creating instances of objects during debugging when a developer might not expect it to.

wilmanwar avatar Aug 30 '16 20:08 wilmanwar

cc: @gregg-miskelly

Gregg, is it expected that hovering over "new" in the debugger will force an allocation of the associated object and any side effects that ctor may have? Is the expectation that the ctor would use Debugger.NotifyOfCrossThreadDependency() to prevent the execution in the debugger?

stephentoub avatar Sep 01 '16 12:09 stephentoub

cc: @KevinH-MS @Pnelsonmsft

@stephentoub to me it seems like 'new ' shouldn't be evaluated by data tips. Patrick/Kevin what do you think? Should I open a Roslyn bug for that?

Yes, the NotifyOfCrossThreadDependency behavior is definitely broken here. I opened VS bug 259670 to track this.

gregg-miskelly avatar Sep 01 '16 16:09 gregg-miskelly

Yes, the NotifyOfCrossThreadDependency behavior is definitely broken here

Sorry, just to clarify, TransactionScope is not currently using NotifyOfCrossThreadDependency. I was asking if that is the recommended way to fix this if the VS behavior doesn't change, whether NotifyOfCrossThreadDependency should be put into such ctors. Seems though like it'd be better to change the VS behavior rather than putting NOCTD into any ctor that may have side effects.

stephentoub avatar Sep 01 '16 16:09 stephentoub

@gregg-miskelly Yeah, it seems to me like we should block evaluation of constructor calls in the DataTip language service.

halverson-dev avatar Sep 01 '16 16:09 halverson-dev

@KevinH-MS we could certainly block in in the language service too, but it seems like the EE should be treating constructor calls like other non-property method calls and returning PotentialSideEffect.

gregg-miskelly avatar Sep 01 '16 16:09 gregg-miskelly

I opened https://github.com/dotnet/roslyn/issues/13526 for this.

@stephentoub I don't think there is any need to use NotifyOfCrossThreadDependency unless you think for some reason people might accidentally explicitly evaluate new TransactionScope(...) in the watch window. The data tip case should be blocked.

gregg-miskelly avatar Sep 01 '16 17:09 gregg-miskelly

@gregg-miskelly I think that's arguably correct (that PotentialSideEffect could be set), but I wonder if it would get annoying in the Watch window case...

CC @cston for thoughts...

halverson-dev avatar Sep 01 '16 17:09 halverson-dev

Where do I find information about "PotentialSideEffect"? Searching for that string in the repo code isn't finding anything.

jimcarley avatar Oct 12 '16 23:10 jimcarley

@jimcarley this isn't a CoreFX concept. This is how the C# expression compiler tells the debugger that a given expression shouldn't be evaluated in situations that are more sensitive (data tips, automatic re-evaluation of properties). Ex: here is where the C# expression compiler sets this for assignment statements -- https://github.com/dotnet/roslyn/blob/614299ff83da9959fa07131c6d0ffbc58873b6ae/src/ExpressionEvaluator/CSharp/Source/ExpressionCompiler/CompilationContext.cs#L233

gregg-miskelly avatar Oct 13 '16 00:10 gregg-miskelly

I think you can resolve this issue as external since it is tracked with roslyn#13526.

gregg-miskelly avatar Oct 13 '16 00:10 gregg-miskelly

Okay. While I don't completely understand how PotentialSideEffect would be used in this TransactionScope scenario, it does seem to me to be external to System.Transactions. I am going to go ahead and resolve this issue.

jimcarley avatar Oct 13 '16 00:10 jimcarley

External issue. Please see https://github.com/dotnet/roslyn/issues/13526

jimcarley avatar Oct 13 '16 00:10 jimcarley