@@ -20,7 +20,7 @@ internal class MonoProxy : DevToolsProxy
2020 {
2121 private IList < string > urlSymbolServerList ;
2222 private HashSet < SessionId > sessions = new HashSet < SessionId > ( ) ;
23- protected Dictionary < SessionId , ExecutionContext > contexts = new Dictionary < SessionId , ExecutionContext > ( ) ;
23+ protected Dictionary < SessionId , List < ExecutionContext > > contexts = new Dictionary < SessionId , List < ExecutionContext > > ( ) ;
2424
2525 public static HttpClient HttpClient => new HttpClient ( ) ;
2626
@@ -41,24 +41,37 @@ public MonoProxy(ILogger logger, IList<string> urlSymbolServerList, int runtimeI
4141
4242 internal ExecutionContext GetContext ( SessionId sessionId )
4343 {
44- if ( contexts . TryGetValue ( sessionId , out ExecutionContext context ) )
44+ if ( TryGetCurrentExecutionContextValue ( sessionId , out ExecutionContext context ) )
4545 return context ;
4646
4747 throw new ArgumentException ( $ "Invalid Session: \" { sessionId } \" ", nameof ( sessionId ) ) ;
4848 }
4949
5050 private bool UpdateContext ( SessionId sessionId , ExecutionContext executionContext , out ExecutionContext previousExecutionContext )
5151 {
52- bool previous = contexts . TryGetValue ( sessionId , out previousExecutionContext ) ;
53- contexts [ sessionId ] = executionContext ;
52+ bool previous = TryGetCurrentExecutionContextValue ( sessionId , out previousExecutionContext ) ;
53+ if ( ! previous )
54+ contexts [ sessionId ] = new ( ) ;
55+ contexts [ sessionId ] . Add ( executionContext ) ;
5456 return previous ;
5557 }
5658
59+ internal bool TryGetCurrentExecutionContextValue ( SessionId id , out ExecutionContext executionContext )
60+ {
61+ executionContext = null ;
62+ if ( ! contexts . TryGetValue ( id , out List < ExecutionContext > contextList ) )
63+ return false ;
64+ if ( contextList . Count == 0 )
65+ return false ;
66+ executionContext = contextList . Last < ExecutionContext > ( ) ;
67+ return true ;
68+ }
69+
5770 internal virtual Task < Result > SendMonoCommand ( SessionId id , MonoCommands cmd , CancellationToken token ) => SendCommand ( id , "Runtime.evaluate" , JObject . FromObject ( cmd ) , token ) ;
5871
5972 internal void SendLog ( SessionId sessionId , string message , CancellationToken token , string type = "warning" )
6073 {
61- if ( ! contexts . TryGetValue ( sessionId , out ExecutionContext context ) )
74+ if ( ! TryGetCurrentExecutionContextValue ( sessionId , out ExecutionContext context ) )
6275 return ;
6376 /*var o = JObject.FromObject(new
6477 {
@@ -93,7 +106,7 @@ protected override async Task<bool> AcceptEvent(SessionId sessionId, JObject par
93106 case "Runtime.consoleAPICalled" :
94107 {
95108 // Don't process events from sessions we aren't tracking
96- if ( ! contexts . TryGetValue ( sessionId , out ExecutionContext context ) )
109+ if ( ! TryGetCurrentExecutionContextValue ( sessionId , out ExecutionContext context ) )
97110 return false ;
98111 string type = args [ "type" ] ? . ToString ( ) ;
99112 if ( type == "debug" )
@@ -169,6 +182,17 @@ protected override async Task<bool> AcceptEvent(SessionId sessionId, JObject par
169182 return true ;
170183 }
171184
185+ case "Runtime.executionContextDestroyed" :
186+ {
187+ int id = args [ "executionContextId" ] . Value < int > ( ) ;
188+ if ( ! contexts . TryGetValue ( sessionId , out var contextList ) )
189+ return false ;
190+ contextList . RemoveAll ( x => x . Id == id ) ;
191+ if ( contextList . Count == 0 )
192+ contexts . Remove ( sessionId ) ;
193+ return false ;
194+ }
195+
172196 case "Debugger.paused" :
173197 {
174198 // Don't process events from sessions we aren't tracking
@@ -254,7 +278,7 @@ protected virtual async Task SendResume(SessionId id, CancellationToken token)
254278 }
255279 protected async Task < bool > IsRuntimeAlreadyReadyAlready ( SessionId sessionId , CancellationToken token )
256280 {
257- if ( contexts . TryGetValue ( sessionId , out ExecutionContext context ) && context . IsRuntimeReady )
281+ if ( TryGetCurrentExecutionContextValue ( sessionId , out ExecutionContext context ) && context . IsRuntimeReady )
258282 return true ;
259283
260284 Result res = await SendMonoCommand ( sessionId , MonoCommands . IsRuntimeReady ( RuntimeId ) , token ) ;
@@ -277,7 +301,7 @@ protected override async Task<bool> AcceptCommand(MessageId id, JObject parms, C
277301 if ( id == SessionId . Null )
278302 await AttachToTarget ( id , token ) ;
279303
280- if ( ! contexts . TryGetValue ( id , out ExecutionContext context ) )
304+ if ( ! TryGetCurrentExecutionContextValue ( id , out ExecutionContext context ) )
281305 {
282306 if ( method == "Debugger.setPauseOnExceptions" )
283307 {
0 commit comments