44
55using DevProxy . Abstractions ;
66using Microsoft . VisualStudio . Threading ;
7+ using System . Collections . Concurrent ;
78using System . Diagnostics ;
89using System . Net ;
910using System . Security . Cryptography . X509Certificates ;
@@ -38,7 +39,7 @@ public class ProxyEngine(IProxyConfiguration config, ISet<UrlToWatch> urlsToWatc
3839 private readonly IProxyState _proxyState = proxyState ?? throw new ArgumentNullException ( nameof ( proxyState ) ) ;
3940 // Dictionary for plugins to store data between requests
4041 // the key is HashObject of the SessionEventArgs object
41- private readonly Dictionary < int , Dictionary < string , object > > _pluginData = [ ] ;
42+ private readonly ConcurrentDictionary < int , Dictionary < string , object > > _pluginData = [ ] ;
4243 private InactivityTimer ? _inactivityTimer ;
4344
4445 public static X509Certificate2 ? Certificate => _proxyServer ? . CertificateManager . RootCertificate ;
@@ -469,7 +470,10 @@ async Task OnRequestAsync(object sender, SessionEventArgs e)
469470 if ( IsProxiedHost ( e . HttpClient . Request . RequestUri . Host ) &&
470471 IsIncludedByHeaders ( e . HttpClient . Request . Headers ) )
471472 {
472- _pluginData . Add ( e . GetHashCode ( ) , [ ] ) ;
473+ if ( ! _pluginData . TryAdd ( e . GetHashCode ( ) , [ ] ) )
474+ {
475+ throw new Exception ( $ "Unable to initialize the plugin data storage for hash key { e . GetHashCode ( ) } ") ;
476+ }
473477 var responseState = new ResponseState ( ) ;
474478 var proxyRequestArgs = new ProxyRequestArgs ( e , responseState )
475479 {
@@ -607,7 +611,7 @@ async Task OnAfterResponseAsync(object sender, SessionEventArgs e)
607611 if ( ! proxyResponseArgs . HasRequestUrlMatch ( _urlsToWatch ) )
608612 {
609613 // clean up
610- _pluginData . Remove ( e . GetHashCode ( ) ) ;
614+ _pluginData . Remove ( e . GetHashCode ( ) , out _ ) ;
611615 return ;
612616 }
613617
@@ -623,7 +627,7 @@ async Task OnAfterResponseAsync(object sender, SessionEventArgs e)
623627 _logger . LogRequest ( message , MessageType . FinishedProcessingRequest , new LoggingContext ( e ) ) ;
624628
625629 // clean up
626- _pluginData . Remove ( e . GetHashCode ( ) ) ;
630+ _pluginData . Remove ( e . GetHashCode ( ) , out _ ) ;
627631 }
628632 }
629633
0 commit comments