Skip to content

Commit 423c626

Browse files
authored
Improve cancellation handling in GetListofCallStacksAsync (#108)
* Check for operation cancelled within loop * Add test case for cancellation of GetListofCallStacksAsync
1 parent 78e99b4 commit 423c626

2 files changed

Lines changed: 18 additions & 10 deletions

File tree

Engine/StackResolver.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ public async Task<List<StackDetails>> GetListofCallStacksAsync(string inputCalls
441441
var validElementNames = new List<string>() { "HistogramTarget", "event" };
442442
this.StatusMessage = "WARNING: XML input was detected but it does not appear to be a known schema!";
443443
while (reader.Read()) {
444+
if (cts.IsCancellationRequested) return null;
444445
if (XmlNodeType.Element == reader.NodeType && validElementNames.Contains(reader.Name)) {
445446
this.StatusMessage = "Input seems to be relevant XML, attempting to process...";
446447
isXMLdoc = true; // assume with reasonable confidence that we have a valid XML doc

Tests/Tests.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -782,31 +782,38 @@ private string PrepareLargeXEventInput() {
782782
Assert.AreEqual(0, xelFieldsTask.Result.Item1.Count);
783783
Assert.AreEqual(0, xelFieldsTask.Result.Item2.Count);
784784

785-
using var cts3 = new CancellationTokenSource();
786785
Assert.IsTrue(csr.ProcessBaseAddresses(@"c:\mssql\binn\sqldk.dll 00000001`00400000"));
787786
var xeventInput = PrepareLargeXEventInput().ToString();
788-
var xeStacks = await csr.GetListofCallStacksAsync(xeventInput, false, cts3);
789-
var resolveStacksTask = csr.ResolveCallstacksAsync(xeStacks, @"..\..\..\Tests\TestCases\TestOrdinal", false, null, false, false, false, true, false, false, Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()), cts3);
787+
using var cts3 = new CancellationTokenSource();
788+
var xeStacksTask = csr.GetListofCallStacksAsync(xeventInput, false, cts3);
790789
while (true) {
791-
if (resolveStacksTask.Wait(StackResolver.OperationWaitIntervalMilliseconds)) break;
790+
if (xeStacksTask.Wait(StackResolver.OperationWaitIntervalMilliseconds)) break;
792791
cts3.Cancel();
793792
}
793+
Assert.AreEqual(null, xeStacksTask.Result);
794+
using var cts4 = new CancellationTokenSource();
795+
var xeStacks = await csr.GetListofCallStacksAsync(xeventInput, false, cts4);
796+
var resolveStacksTask = csr.ResolveCallstacksAsync(xeStacks, @"..\..\..\Tests\TestCases\TestOrdinal", false, null, false, false, false, true, false, false, Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()), cts4);
797+
while (true) {
798+
if (resolveStacksTask.Wait(StackResolver.OperationWaitIntervalMilliseconds)) break;
799+
cts4.Cancel();
800+
}
794801
Assert.AreEqual(StackResolver.OperationCanceled, resolveStacksTask.Result);
795802

796-
using var cts4 = new CancellationTokenSource();
797-
var parseModuleInfoXMLTask = ModuleInfoHelper.ParseModuleInfoXMLAsync(xeStacks, cts4);
803+
using var cts5 = new CancellationTokenSource();
804+
var parseModuleInfoXMLTask = ModuleInfoHelper.ParseModuleInfoXMLAsync(xeStacks, cts5);
798805
while (true) {
799-
cts4.Cancel(); // because this method is quick, we need to simulate a cancel right away
806+
cts5.Cancel(); // because this method is quick, we need to simulate a cancel right away
800807
if (parseModuleInfoXMLTask.Wait(StackResolver.OperationWaitIntervalMilliseconds)) break;
801808
}
802809
Assert.AreEqual(0, parseModuleInfoXMLTask.Result.Item1.Count);
803810
Assert.AreEqual(0, parseModuleInfoXMLTask.Result.Item2.Count);
804811

805-
using var cts5 = new CancellationTokenSource();
806-
var parseModuleInfoTask = ModuleInfoHelper.ParseModuleInfoAsync(xeStacks, cts5);
812+
using var cts6 = new CancellationTokenSource();
813+
var parseModuleInfoTask = ModuleInfoHelper.ParseModuleInfoAsync(xeStacks, cts6);
807814
while (true) {
808815
if (parseModuleInfoTask.Wait(StackResolver.OperationWaitIntervalMilliseconds)) break;
809-
cts5.Cancel();
816+
cts6.Cancel();
810817
}
811818
Assert.AreEqual(0, parseModuleInfoTask.Result.Count);
812819
Assert.AreEqual(0, parseModuleInfoTask.Result.Count);

0 commit comments

Comments
 (0)