Skip to content

Commit

Permalink
Fix crash in SimpleTextInsights (#3853)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreww-msft committed Sep 13, 2024
1 parent 3130870 commit 6f52ad2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>

<TextBox Text="{x:Bind Text, Mode=OneWay}" AutomationProperties.AutomationId="ContentTextBock" VerticalAlignment="Center" Style="{StaticResource ReadOnlyTextBox}" />
<TextBox Text="{x:Bind Text, Mode=OneWay}" AutomationProperties.AutomationId="ContentTextBock" VerticalAlignment="Center" Style="{StaticResource ReadOnlyTextBox}" TextWrapping="Wrap"/>
<local:ElevatedButtonControl Text="{x:Bind ElevationButtonTextString}" Grid.Column="1" Margin="20 0 0 0" Command="{x:Bind Command, Mode=OneWay}" AutomationProperties.Name="{x:Bind ElevationButtonTextString}" >
<ToolTipService.ToolTip>
<ToolTip Content="{x:Bind ElevationButtonToolTipString}" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using DevHome.DevDiagnostics.Models;
using Microsoft.UI.Dispatching;
using Serilog;

namespace DevHome.DevDiagnostics.Helpers;
Expand All @@ -31,28 +33,47 @@ internal sealed partial class InsightsHelper
[GeneratedRegex(@"0xc0000005", RegexOptions.IgnoreCase, "en-US")]
private static partial Regex MemoryErrorRegex();

private static readonly List<InsightRegex> RegexList = [];
private static readonly List<InsightRegex> _regexList = [];

static InsightsHelper()
{
RegexList.Add(new InsightRegex(InsightType.LockedFile, LockedFileErrorRegex()));
RegexList.Add(new InsightRegex(InsightType.Security, BufferOverflowErrorRegex()));
RegexList.Add(new InsightRegex(InsightType.MemoryViolation, MemoryErrorRegex()));
_regexList.Add(new InsightRegex(InsightType.LockedFile, LockedFileErrorRegex()));
_regexList.Add(new InsightRegex(InsightType.Security, BufferOverflowErrorRegex()));
_regexList.Add(new InsightRegex(InsightType.MemoryViolation, MemoryErrorRegex()));
}

internal static Insight? FindPattern(string errorText)
internal static async Task<Insight?> FindPattern(string errorText, DispatcherQueue dispatcher)
{
SimpleTextInsight? newInsight = null;

foreach (var insightRegex in RegexList)
foreach (var insightRegex in _regexList)
{
var match = insightRegex.Regex.Match(errorText);
if (match.Success)
{
newInsight = new SimpleTextInsight
var tcs = new TaskCompletionSource<bool>();
dispatcher.TryEnqueue(() =>
{
InsightType = insightRegex.InsightType,
};
try
{
newInsight = new SimpleTextInsight
{
InsightType = insightRegex.InsightType,
};
tcs.SetResult(true);
}
catch (Exception ex)
{
tcs.SetException(ex);
}
});

await tcs.Task;

if (newInsight is null)
{
return null;
}

// Once we flesh out our error database, we should have a more structured way to
// handle different types of insights, rather than a switch statement.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,15 @@
<toolkit:DataGridTemplateColumn x:Uid="LogMessageColumn" Width="*" ClipboardContentBinding="{Binding Message}" >
<toolkit:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Message}" SelectedText="{Binding SelectedText, Mode=TwoWay}" IsReadOnly="True"/>
<TextBox
Text="{Binding Message}"
SelectedText="{Binding SelectedText, Mode=TwoWay}"
IsReadOnly="True"
TextWrapping="Wrap"
MaxHeight="100"
VerticalAlignment="Top"
AcceptsReturn="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"/>
</DataTemplate>
</toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,7 @@ Dump File creation time: {1}</value>
<comment>Title of an insight informing the user of a reason why a process exited</comment>
</data>
<data name="InsightProcessExitMissingDependencies" xml:space="preserve">
<value>Process {0} (pid: {1,6}) exited with error code {2}. Enabling loader snaps and trying to launch the process again can help diagnose why the app exited.</value>
<value>Process {0} (pid: {1,6}) exited with error code {2}. To help diagnose this issue, enable loader snaps and try to launch the process again.</value>
<comment>{Locked="{0}, "{1,6}", "{2}"} Formatted string informing the user the process name {0}, the Process ID {1}, the exit code {2} of a process.</comment>
</data>
<data name="InsightProcessExitMissingDependenciesIdentifiedTitle" xml:space="preserve">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,17 @@ private void WinLogsOutput_CollectionChanged(object? sender, NotifyCollectionCha

private void FindPattern(string message)
{
var newInsight = InsightsHelper.FindPattern(message);
if (newInsight is not null)
var newInsightTask = InsightsHelper.FindPattern(message, _dispatcher);
if (newInsightTask is not null)
{
_dispatcher.TryEnqueue(() =>
var newInsight = newInsightTask.Result;
if (newInsight is not null)
{
_insightsService.AddInsight(newInsight);
});
_dispatcher.TryEnqueue(() =>
{
_insightsService.AddInsight(newInsight);
});
}
}
}

Expand Down

0 comments on commit 6f52ad2

Please sign in to comment.