Skip to content

Commit fac66ca

Browse files
author
fremag
committed
Work thread: add some logs when exception is caught
Fixes #143
1 parent f89e043 commit fac66ca

File tree

6 files changed

+45
-12
lines changed

6 files changed

+45
-12
lines changed

MemoScope/Core/ClrDump.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,16 @@ public ClrDump(DataTarget target, string dumpPath, MessageBus msgBus)
6565
DumpPath = dumpPath;
6666
MessageBus = msgBus;
6767
worker = new SingleThreadWorker(dumpPath);
68-
worker.Run(InitRuntime);
68+
worker.Run(InitRuntime, OnError);
6969

7070
ClrDumpInfo = ClrDumpInfo.Load(dumpPath);
7171
}
7272

73+
private void OnError(Exception ex)
74+
{
75+
MessageBus.Log(this, "Failed to initRuntime: " + DumpPath, ex);
76+
}
77+
7378
public void InitCache(CancellationToken token)
7479
{
7580
cache = new ClrDumpCache(this);
@@ -105,7 +110,7 @@ internal void Dispose()
105110
logger.Debug("Cache dispose");
106111
cache.Dispose();
107112
logger.Debug("Runtime.DataTarget.Dispose");
108-
Run( () => Runtime.DataTarget.Dispose());
113+
Run( () => Runtime?.DataTarget?.Dispose());
109114
logger.Debug("Worker.Dispose");
110115
worker.Dispose();
111116
}

MemoScope/Core/ProcessInfo/ClrDumpInfo.cs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Diagnostics;
66
using System.Collections.Generic;
77
using System.Linq;
8-
using System;
98

109
namespace MemoScope.Core.ProcessInfo
1110
{

MemoScope/Core/SimpleTask.cs

+13-3
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,32 @@ public class SimpleTask
77
{
88
public Action Work { get; }
99
public Action Callback { get; }
10+
public Action<Exception> OnError { get; }
11+
1012
public TaskScheduler Scheduler { get; }
1113

12-
public SimpleTask(Action work, Action callback) : this(work, callback, null)
14+
public SimpleTask(Action work, Action callback) : this(work, callback,null, null)
1315
{
1416
}
1517

1618
public SimpleTask(Action work) : this(work, null)
1719
{
18-
1920
}
2021

21-
public SimpleTask(Action work, Action callback, TaskScheduler sched)
22+
public SimpleTask(Action work, Action callback, TaskScheduler sched) : this(work, callback, null, sched)
23+
{
24+
}
25+
26+
public SimpleTask(Action work, Action callback, Action<Exception> onError) : this(work, callback, onError, null)
27+
{
28+
}
29+
30+
public SimpleTask(Action work, Action callback, Action<Exception> onError, TaskScheduler sched)
2231
{
2332
Work = work;
2433
Callback = callback;
2534
Scheduler = sched;
35+
OnError = onError;
2636
}
2737
}
2838
}

MemoScope/Core/SingleThreadWorker.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ private void Run()
4040
{
4141
task.Work();
4242
}
43-
catch (Exception)
43+
catch (Exception e)
4444
{
45+
task.OnError?.Invoke(e);
4546
}
4647
if (task.Callback != null)
4748
{
@@ -51,8 +52,9 @@ private void Run()
5152
{
5253
task.Callback();
5354
}
54-
catch (Exception)
55+
catch (Exception ex)
5556
{
57+
task.OnError?.Invoke(ex);
5658
}
5759
};
5860
if (task.Scheduler == null)
@@ -83,6 +85,12 @@ public void Run(Action work)
8385
queue.Add(new SimpleTask(work, () => myEvent.Set()));
8486
myEvent.WaitOne();
8587
}
88+
public void Run(Action work, Action<Exception> onError=null)
89+
{
90+
ManualResetEvent myEvent = new ManualResetEvent(false);
91+
queue.Add(new SimpleTask(work, () => myEvent.Set(), onError));
92+
myEvent.WaitOne();
93+
}
8694

8795
public T Eval<T>(Func<T> func)
8896
{

MemoScope/Modules/TypeStats/TypeStatModule.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,15 @@ public void Setup(ClrDump clrDump)
4343
public override void Init()
4444
{
4545
Log("Computing type statistics...", WinFwk.UITools.Log.LogLevelType.Info);
46-
typeStats = ClrDump.GetTypeStats();
47-
Summary = $"{typeStats.Count:###,###,###,##0} types";
46+
if (ClrDump.Runtime != null)
47+
{
48+
typeStats = ClrDump.GetTypeStats();
49+
Summary = $"{typeStats.Count:###,###,###,##0} types";
50+
}
51+
else
52+
{
53+
Summary = $"Error. Dump file not loaded !";
54+
}
4855
Log("Type statistics computed.", WinFwk.UITools.Log.LogLevelType.Info);
4956
}
5057

MemoScope/Services/DumpLoaderService.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using WinFwk.UIMessages;
88
using WinFwk.UIServices;
99
using System.Threading;
10+
using WinFwk.UITools.Log;
1011

1112
namespace MemoScope.Services
1213
{
@@ -50,9 +51,12 @@ public void HandleMessage(OpenDumpRequest openDumpRequest)
5051
EndTask("File loaded: " + fileInfo.FullName);
5152
}
5253
}
53-
catch
54+
catch(Exception ex)
5455
{
55-
target.Dispose();
56+
string msg = "Failed to load dump file: " + fileInfo.FullName;
57+
EndTask(msg);
58+
MessageBus.Log(this, msg, ex);
59+
target?.Dispose();
5660
throw;
5761
}
5862
});

0 commit comments

Comments
 (0)