From db7479777a206736659e446e29e4703206b6b124 Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Fri, 23 Jun 2023 15:00:53 -0300 Subject: [PATCH 1/3] support executionContextsCleared message --- .../BrowserDebugProxy/DevToolsHelper.cs | 21 ++++++++++++++++--- .../debugger/BrowserDebugProxy/MonoProxy.cs | 17 ++++++++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/mono/wasm/debugger/BrowserDebugProxy/DevToolsHelper.cs b/src/mono/wasm/debugger/BrowserDebugProxy/DevToolsHelper.cs index 8f4cfa9ca69b52..1f6ee86cebb6a6 100644 --- a/src/mono/wasm/debugger/BrowserDebugProxy/DevToolsHelper.cs +++ b/src/mono/wasm/debugger/BrowserDebugProxy/DevToolsHelper.cs @@ -515,14 +515,22 @@ public ExecutionContext GetCurrentContext(SessionId sessionId) ? context : throw new KeyNotFoundException($"No execution context found for session {sessionId}"); - public bool TryGetCurrentExecutionContextValue(SessionId id, out ExecutionContext executionContext) + public bool TryGetCurrentExecutionContextValue(SessionId id, out ExecutionContext executionContext, bool ignoreDestroyedContext = true) { executionContext = null; if (!contexts.TryGetValue(id, out ConcurrentBag contextBag)) return false; if (contextBag.IsEmpty) return false; - executionContext = contextBag.Where(context => context.Id == contextBag.Where(context => context.Destroyed == false).Max(context => context.Id)).FirstOrDefault(); + IEnumerable validContexts = null; + if (ignoreDestroyedContext) + validContexts = contextBag.Where(context => context.Destroyed == false); + else + validContexts = contextBag; + if (!validContexts.Any()) + return false; + int maxId = validContexts.Max(context => context.Id); + executionContext = contextBag.Where(context => context.Id == maxId).FirstOrDefault(); return executionContext != null; } @@ -540,7 +548,7 @@ public void OnDefaultContextUpdate(SessionId sessionId, ExecutionContext newCont public bool TryGetAndAddContext(SessionId sessionId, ExecutionContext newExecutionContext, out ExecutionContext previousExecutionContext) { - bool hasExisting = TryGetCurrentExecutionContextValue(sessionId, out previousExecutionContext); + bool hasExisting = TryGetCurrentExecutionContextValue(sessionId, out previousExecutionContext, false); ConcurrentBag bag = contexts.GetOrAdd(sessionId, _ => new ConcurrentBag()); bag.Add(newExecutionContext); return hasExisting; @@ -569,6 +577,13 @@ public void DestroyContext(SessionId sessionId, int id) foreach (ExecutionContext context in contextBag.Where(x => x.Id == id).ToList()) context.Destroyed = true; } + public void ClearContexts(SessionId sessionId) + { + if (!contexts.TryGetValue(sessionId, out ConcurrentBag contextBag)) + return; + foreach (ExecutionContext context in contextBag) + context.Destroyed = true; + } public bool ContainsKey(SessionId sessionId) => contexts.ContainsKey(sessionId); } } diff --git a/src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs b/src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs index 7f73ab33430d2a..3eb360ca2ad186 100644 --- a/src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs +++ b/src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs @@ -142,6 +142,11 @@ protected override async Task AcceptEvent(SessionId sessionId, JObject par Contexts.DestroyContext(sessionId, args["executionContextId"].Value()); return false; } + case "Runtime.executionContextsCleared": + { + Contexts.ClearContexts(sessionId); + return false; + } case "Debugger.paused": { @@ -1541,7 +1546,17 @@ internal virtual async Task OnSourceFileAdded(SessionId sessionId, SourceFile so { if (req.TryResolve(source)) { - await SetBreakpoint(sessionId, context.store, req, true, false, token); + try + { + await SetBreakpoint(sessionId, context.store, req, true, false, token); + } + catch (DebuggerAgentException e) + { + //it's not a wasm page then the command throws an error + if (!e.Message.Contains("getDotnetRuntime is not defined")) + logger.LogDebug($"Unexpected error on RuntimeReady {e}"); + return; + } } } } From c3873da423bf8ad0a7511b3a96a77932df732301 Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Wed, 28 Jun 2023 14:29:58 -0300 Subject: [PATCH 2/3] addressing @radical comments --- src/mono/wasm/debugger/BrowserDebugProxy/DevToolsHelper.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mono/wasm/debugger/BrowserDebugProxy/DevToolsHelper.cs b/src/mono/wasm/debugger/BrowserDebugProxy/DevToolsHelper.cs index 1f6ee86cebb6a6..1759da172cf258 100644 --- a/src/mono/wasm/debugger/BrowserDebugProxy/DevToolsHelper.cs +++ b/src/mono/wasm/debugger/BrowserDebugProxy/DevToolsHelper.cs @@ -530,7 +530,7 @@ public bool TryGetCurrentExecutionContextValue(SessionId id, out ExecutionContex if (!validContexts.Any()) return false; int maxId = validContexts.Max(context => context.Id); - executionContext = contextBag.Where(context => context.Id == maxId).FirstOrDefault(); + executionContext = contextBag.SingleOrDefault(context => context.Id == maxId); return executionContext != null; } @@ -548,7 +548,7 @@ public void OnDefaultContextUpdate(SessionId sessionId, ExecutionContext newCont public bool TryGetAndAddContext(SessionId sessionId, ExecutionContext newExecutionContext, out ExecutionContext previousExecutionContext) { - bool hasExisting = TryGetCurrentExecutionContextValue(sessionId, out previousExecutionContext, false); + bool hasExisting = TryGetCurrentExecutionContextValue(sessionId, out previousExecutionContext, ignoreDestroyedContext: false); ConcurrentBag bag = contexts.GetOrAdd(sessionId, _ => new ConcurrentBag()); bag.Add(newExecutionContext); return hasExisting; From 1097b3e8f9e12707517605052720bbca9bebcfad Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Thu, 29 Jun 2023 16:54:51 -0300 Subject: [PATCH 3/3] change singleordefault with firstordefault --- src/mono/wasm/debugger/BrowserDebugProxy/DevToolsHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/wasm/debugger/BrowserDebugProxy/DevToolsHelper.cs b/src/mono/wasm/debugger/BrowserDebugProxy/DevToolsHelper.cs index 1759da172cf258..7c356d31a918b4 100644 --- a/src/mono/wasm/debugger/BrowserDebugProxy/DevToolsHelper.cs +++ b/src/mono/wasm/debugger/BrowserDebugProxy/DevToolsHelper.cs @@ -530,7 +530,7 @@ public bool TryGetCurrentExecutionContextValue(SessionId id, out ExecutionContex if (!validContexts.Any()) return false; int maxId = validContexts.Max(context => context.Id); - executionContext = contextBag.SingleOrDefault(context => context.Id == maxId); + executionContext = contextBag.FirstOrDefault(context => context.Id == maxId); return executionContext != null; }