Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
using System;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis.ExpressionEvaluator;
using Microsoft.CodeAnalysis.CSharp.ExpressionEvaluator;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.ExpressionEvaluator;
using Microsoft.VisualStudio.Debugger;
using Microsoft.VisualStudio.Debugger.Clr;
using Microsoft.VisualStudio.Debugger.ComponentInterfaces;
using Microsoft.VisualStudio.Debugger.Evaluation;
using Roslyn.Test.Utilities;
using Xunit;
Expand Down Expand Up @@ -2183,5 +2186,85 @@ public void NullableValue_Error()
EvalFailedResult("o.P", "Function evaluation timed out", "int?", "o.P"));
}
}

/// <summary>
/// Get many items synchronously.
/// </summary>
[Fact]
public void ManyItemsSync()
{
const int n = 10000;
var value = CreateDkmClrValue(Enumerable.Range(0, n).ToArray());
var evalResult = FormatResult("a", value);
IDkmClrResultProvider resultProvider = new CSharpResultProvider();
var workList = new DkmWorkList();

// GetChildren
var getChildrenResult = default(DkmGetChildrenAsyncResult);
resultProvider.GetChildren(evalResult, workList, n, DefaultInspectionContext, r => getChildrenResult = r);
Assert.Equal(workList.Length, 0);
Assert.Equal(getChildrenResult.InitialChildren.Length, n);

// GetItems
var getItemsResult = default(DkmEvaluationEnumAsyncResult);
resultProvider.GetItems(getChildrenResult.EnumContext, workList, 0, n, r => getItemsResult = r);
Assert.Equal(workList.Length, 0);
Assert.Equal(getItemsResult.Items.Length, n);
}

/// <summary>
/// Multiple items, some completed asynchronously.
/// </summary>
[Fact]
public void MultipleItemsAsync()
{
var source =
@"using System.Diagnostics;
[DebuggerDisplay(""{F}"")]
class C
{
object F;
}";
var runtime = new DkmClrRuntimeInstance(ReflectionUtilities.GetMscorlib(GetAssembly(source)));
using (runtime.Load())
{
int n = 10;
var type = runtime.GetType("C");
// C[] with alternating null and non-null values.
var value = CreateDkmClrValue(Enumerable.Range(0, n).Select(i => (i % 2) == 0 ? type.Instantiate() : null).ToArray());
var evalResult = FormatResult("a", value);

IDkmClrResultProvider resultProvider = new CSharpResultProvider();
var workList = new DkmWorkList();

// GetChildren
var getChildrenResult = default(DkmGetChildrenAsyncResult);
resultProvider.GetChildren(evalResult, workList, n, DefaultInspectionContext, r => getChildrenResult = r);
Assert.Equal(workList.Length, 1);
workList.Execute();
Assert.Equal(getChildrenResult.InitialChildren.Length, n);

// GetItems
var getItemsResult = default(DkmEvaluationEnumAsyncResult);
resultProvider.GetItems(getChildrenResult.EnumContext, workList, 0, n, r => getItemsResult = r);
Assert.Equal(workList.Length, 1);
workList.Execute();
Assert.Equal(getItemsResult.Items.Length, n);
}
}

[Fact]
public void MultipleItemsSyncAndException()
{
// Test exception after M items.
Assert.False(true);
}

[Fact]
public void MultipleItemsAsyncAndException()
{
// Test exception after M items.
Assert.False(true);
}
}
}
120 changes: 84 additions & 36 deletions src/ExpressionEvaluator/Core/Source/ResultProvider/ResultProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Threading;
using Microsoft.CodeAnalysis.Collections;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.VisualStudio.Debugger;
Expand Down Expand Up @@ -43,10 +42,15 @@ internal ResultProvider(Formatter formatter)
void IDkmClrResultProvider.GetResult(DkmClrValue value, DkmWorkList workList, DkmClrType declaredType, DkmInspectionContext inspectionContext, ReadOnlyCollection<string> formatSpecifiers, string resultName, string resultFullName, DkmCompletionRoutine<DkmEvaluationAsyncResult> completionRoutine)
{
// TODO: Use full name
GetRootResultAndContinue(value, workList, declaredType, inspectionContext, resultName,
result => ContinueWithExceptionHandling(
() => completionRoutine(new DkmEvaluationAsyncResult(result)),
e => completionRoutine(DkmEvaluationAsyncResult.CreateErrorResult(e))));
var wl = new WorkList(workList, e => completionRoutine(DkmEvaluationAsyncResult.CreateErrorResult(e)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nitpick: "wl" -> "workList"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately the parameter is named 'workList'.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah...my bad.

GetRootResultAndContinue(
value,
wl,
declaredType,
inspectionContext,
resultName,
result => wl.ContinueWith(_ => completionRoutine(new DkmEvaluationAsyncResult(result))));
wl.Execute();
}

DkmClrValue IDkmClrResultProvider.GetClrValue(DkmSuccessEvaluationResult evaluationResult)
Expand Down Expand Up @@ -113,7 +117,7 @@ string IDkmClrResultProvider.GetUnderlyingString(DkmEvaluationResult result)
}
}

private void CreateEvaluationResultAndContinue(EvalResultDataItem dataItem, DkmWorkList workList, DkmInspectionContext inspectionContext, DkmStackWalkFrame stackFrame, CompletionRoutine<DkmEvaluationResult> completionRoutine)
private void CreateEvaluationResultAndContinue(EvalResultDataItem dataItem, WorkList workList, DkmInspectionContext inspectionContext, DkmStackWalkFrame stackFrame, CompletionRoutine<DkmEvaluationResult> completionRoutine)
{
switch (dataItem.Kind)
{
Expand Down Expand Up @@ -360,7 +364,7 @@ internal EvalResultDataItem CreateDataItem(
inspectionContext: inspectionContext);
}

private void GetRootResultAndContinue(DkmClrValue value, DkmWorkList workList, DkmClrType declaredType, DkmInspectionContext inspectionContext, string resultName, CompletionRoutine<DkmEvaluationResult> completionRoutine)
private void GetRootResultAndContinue(DkmClrValue value, WorkList workList, DkmClrType declaredType, DkmInspectionContext inspectionContext, string resultName, CompletionRoutine<DkmEvaluationResult> completionRoutine)
{
var type = value.Type.GetLmrType();
if (type.IsTypeVariables())
Expand Down Expand Up @@ -440,7 +444,7 @@ private void GetRootResultAndContinue(DkmClrValue value, DkmWorkList workList, D
}
}

internal void GetResultAndContinue(EvalResultDataItem dataItem, DkmWorkList workList, DkmClrType declaredType, DkmInspectionContext inspectionContext, EvalResultDataItem parent, CompletionRoutine<DkmEvaluationResult> completionRoutine)
private void GetResultAndContinue(EvalResultDataItem dataItem, WorkList workList, DkmClrType declaredType, DkmInspectionContext inspectionContext, EvalResultDataItem parent, CompletionRoutine<DkmEvaluationResult> completionRoutine)
{
var value = dataItem.Value; // Value may have been replaced (specifically, for Nullable<T>).
DebuggerDisplayInfo displayInfo;
Expand All @@ -451,13 +455,17 @@ internal void GetResultAndContinue(EvalResultDataItem dataItem, DkmWorkList work
CompletionRoutine<Exception> onException =
e => completionRoutine(CreateEvaluationResultFromException(e, dataItem, inspectionContext));

value.EvaluateDebuggerDisplayStringAndContinue(workList, inspectionContext, targetType, attribute.Name,
value.EvaluateDebuggerDisplayStringAndContinue(workList.InnerWorkList, inspectionContext, targetType, attribute.Name,
displayName => ContinueWithExceptionHandling(
() => value.EvaluateDebuggerDisplayStringAndContinue(workList, inspectionContext, targetType, attribute.Value,
() => value.EvaluateDebuggerDisplayStringAndContinue(workList.InnerWorkList, inspectionContext, targetType, attribute.Value,
displayValue => ContinueWithExceptionHandling(
() => value.EvaluateDebuggerDisplayStringAndContinue(workList, inspectionContext, targetType, attribute.TypeName,
() => value.EvaluateDebuggerDisplayStringAndContinue(workList.InnerWorkList, inspectionContext, targetType, attribute.TypeName,
displayType => ContinueWithExceptionHandling(
() => completionRoutine(GetResult(inspectionContext, dataItem, declaredType, displayName.Result, displayValue.Result, displayType.Result, parent)),
() =>
{
completionRoutine(GetResult(inspectionContext, dataItem, declaredType, displayName.Result, displayValue.Result, displayType.Result, parent));
workList.Execute();
},
onException)),
onException)),
onException));
Expand Down Expand Up @@ -536,15 +544,16 @@ private void GetChildrenAndContinue(EvalResultDataItem dataItem, DkmWorkList wor
Debug.Assert(index >= numRows);
Debug.Assert(initialRequestSize >= numRows);
var initialChildren = new DkmEvaluationResult[numRows];
GetEvaluationResultsAndContinue(rows, initialChildren, 0, numRows, workList, inspectionContext, stackFrame,
() => ContinueWithExceptionHandling(
() =>
{
var enumContext = DkmEvaluationResultEnumContext.Create(index, stackFrame, inspectionContext, new EnumContextDataItem(dataItem));
completionRoutine(new DkmGetChildrenAsyncResult(initialChildren, enumContext));
rows.Free();
},
e => completionRoutine(DkmGetChildrenAsyncResult.CreateErrorResult(e))));
var wl = new WorkList(workList, e => completionRoutine(DkmGetChildrenAsyncResult.CreateErrorResult(e)));
GetEvaluationResultsAndContinue(rows, initialChildren, 0, numRows, wl, inspectionContext, stackFrame,
_1 => wl.ContinueWith(
_2 =>
{
var enumContext = DkmEvaluationResultEnumContext.Create(index, stackFrame, inspectionContext, new EnumContextDataItem(dataItem));
completionRoutine(new DkmGetChildrenAsyncResult(initialChildren, enumContext));
rows.Free();
}));
wl.Execute();
}

private void GetItemsAndContinue(EvalResultDataItem dataItem, DkmWorkList workList, int startIndex, int count, DkmInspectionContext inspectionContext, DkmCompletionRoutine<DkmEvaluationEnumAsyncResult> completionRoutine)
Expand All @@ -560,35 +569,36 @@ private void GetItemsAndContinue(EvalResultDataItem dataItem, DkmWorkList workLi
var numRows = rows.Count;
Debug.Assert(count >= numRows);
var results = new DkmEvaluationResult[numRows];
GetEvaluationResultsAndContinue(rows, results, 0, numRows, workList, inspectionContext, value.StackFrame,
() => ContinueWithExceptionHandling(
() =>
{
completionRoutine(new DkmEvaluationEnumAsyncResult(results));
rows.Free();
},
e => completionRoutine(DkmEvaluationEnumAsyncResult.CreateErrorResult(e))));
var wl = new WorkList(workList, e => completionRoutine(DkmEvaluationEnumAsyncResult.CreateErrorResult(e)));
GetEvaluationResultsAndContinue(rows, results, 0, numRows, wl, inspectionContext, value.StackFrame,
_1 => wl.ContinueWith(
_2 =>
{
completionRoutine(new DkmEvaluationEnumAsyncResult(results));
rows.Free();
}));
wl.Execute();
}

private void StoreResultAndContinue(DkmEvaluationResult result, ArrayBuilder<EvalResultDataItem> rows, DkmEvaluationResult[] results, int index, int numRows, DkmWorkList workList, DkmInspectionContext inspectionContext, DkmStackWalkFrame stackFrame, CompletionRoutine completionRoutine)
private void StoreResultAndContinue(DkmEvaluationResult result, ArrayBuilder<EvalResultDataItem> rows, DkmEvaluationResult[] results, int index, int numRows, WorkList workList, DkmInspectionContext inspectionContext, DkmStackWalkFrame stackFrame, CompletionRoutine<WorkList> completionRoutine)
{
results[index] = result;
index++;

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.

Personally, I think it would be clearer to pass (index + 1) to the next call since no one else will consume the mutated state.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That seems cleaner. Changed.

if (index < numRows)
{
GetEvaluationResultsAndContinue(rows, results, index, numRows, workList, inspectionContext, stackFrame, completionRoutine);
workList.ContinueWith(wl => GetEvaluationResultsAndContinue(rows, results, index, numRows, wl, inspectionContext, stackFrame, completionRoutine));
}
else
{
completionRoutine();
completionRoutine(workList);
}

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.

I think this completionRoutine just adds something to the work list, so I don't believe it can throw. If that's the case, then you probably don't need exception handling in the caller.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed try/catch.

}

private void GetEvaluationResultsAndContinue(ArrayBuilder<EvalResultDataItem> rows, DkmEvaluationResult[] results, int index, int numRows, DkmWorkList workList, DkmInspectionContext inspectionContext, DkmStackWalkFrame stackFrame, CompletionRoutine completionRoutine)
private void GetEvaluationResultsAndContinue(ArrayBuilder<EvalResultDataItem> rows, DkmEvaluationResult[] results, int index, int numRows, WorkList workList, DkmInspectionContext inspectionContext, DkmStackWalkFrame stackFrame, CompletionRoutine<WorkList> completionRoutine)
{
if (index >= numRows)

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.

It seems strange to use opposite conditions here and in StoreResultAndContinue.

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.

It seems like this condition can only fail when index == numRows == 0. Maybe we shouldn't call this method in that case. (Rationale: why does our [convoluted] loop have two range checks?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will leave the check as is for now since we're calling this method from GetChildren and GetItems. But StoreResultAndContinue has been merged in and the condition reversed.

{
completionRoutine();
completionRoutine(workList);
}
else
{
Expand All @@ -603,7 +613,7 @@ private void GetEvaluationResultsAndContinue(ArrayBuilder<EvalResultDataItem> ro
// experience is less than ideal (there's no real indication that something went wrong),
// however, it seems like it's better to enumerate/display some rows than none. We will
// receive a non-fatal Watson report in this case, so the problem won't go unnoticed.
completionRoutine();
completionRoutine(workList);
}));
}
}
Expand Down Expand Up @@ -668,7 +678,7 @@ private static DkmEvaluationResult CreateEvaluationResultFromException(Exception
DataItem: null);
}

internal static void ContinueWithExceptionHandling(CompletionRoutine onCompleted, CompletionRoutine<Exception> onException)
private static void ContinueWithExceptionHandling(CompletionRoutine onCompleted, CompletionRoutine<Exception> onException)
{
try
{
Expand All @@ -679,5 +689,43 @@ internal static void ContinueWithExceptionHandling(CompletionRoutine onCompleted
onException(e);
}
}

private delegate void WorkListCompletionRoutine(WorkList workList);

private sealed class WorkList

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would it make sense for WorkList to be IDisposable and assert that _completionRoutine is null (or, if not Dispose, then some other sort of validation that Execute always gets called when it needs to)? It worries me a little that we might forget to call Execute on a WorkList that has a continuation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It would be good to assert that _completionRoutine is null although it's not clear when we would assert since the place to call Dispose is also a reasonable point to call Execute.

{
internal readonly DkmWorkList InnerWorkList;
private readonly CompletionRoutine<Exception> _onException;
private WorkListCompletionRoutine _completionRoutine;

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 far as I can tell, there are three kinds of completion routines floating around, WorkListCompletionRoutine, CompletionRoutine, and DkmCompletionRoutine. However, we seem to call locals of all three types "completionRoutine". I think the code would be much easier to follow if we gave them distinct names.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've removed WorkListCompletionRoutine.


internal WorkList(DkmWorkList workList, CompletionRoutine<Exception> onException)
{
InnerWorkList = workList;
_onException = onException;
}

internal void ContinueWith(WorkListCompletionRoutine completionRoutine)
{
Debug.Assert(_completionRoutine == null);
_completionRoutine = completionRoutine;
}

internal void Execute()
{
while (_completionRoutine != null)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we ever expect _completionRoutine to be null when Execute is called (before entering the while)? If not, perhaps Assert?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

_completionRoutine will be null if the item in progress has a [DebuggerDisplay] attribute since _completionRoutine will be set asynchronously.

{
var completionRoutine = _completionRoutine;
_completionRoutine = null;
try
{
completionRoutine(this);
}
catch (Exception e) when (ExpressionEvaluatorFatalError.ReportNonFatalException(e, DkmComponentManager.ReportCurrentNonFatalException))
{
_onException(e);
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ internal void AddWork(Action item)
_workList.Enqueue(item);
}

internal int Length

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If this isn't part of the real type, can we add a doc comment?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider using => body for trivial properties.

{
get { return _workList.Count; }
}

public void Execute()
{
while (_workList.Count > 0)
Expand Down