Skip to content

Fixes concurrency issue in ProxyEngine, ConcolFormatter, and MockResponsePlugin #1161 #1162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions dev-proxy-plugins/Mocks/MockResponsePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using DevProxy.Plugins.Behavior;
using System.Text;
using Microsoft.Extensions.Logging;
using System.Collections.Concurrent;

namespace DevProxy.Plugins.Mocks;

Expand Down Expand Up @@ -42,7 +43,7 @@ public class MockResponsePlugin(IPluginEvents pluginEvents, IProxyContext contex
private IProxyConfiguration? _proxyConfiguration;
// tracks the number of times a mock has been applied
// used in combination with mocks that have an Nth property
private readonly Dictionary<string, int> _appliedMocks = [];
private readonly ConcurrentDictionary<string, int> _appliedMocks = [];

public override Option[] GetOptions()
{
Expand Down Expand Up @@ -243,12 +244,7 @@ _configuration.Mocks is null ||

if (mockResponse is not null && mockResponse.Request is not null)
{
if (!_appliedMocks.TryGetValue(mockResponse.Request.Url, out int value))
{
value = 0;
_appliedMocks.Add(mockResponse.Request.Url, value);
}
_appliedMocks[mockResponse.Request.Url] = ++value;
_appliedMocks.AddOrUpdate(mockResponse.Request.Url, 1, (_, value) => ++value);
}

return mockResponse;
Expand Down
20 changes: 9 additions & 11 deletions dev-proxy/Logging/ProxyConsoleFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Concurrent;
using System.Text;
using DevProxy.Abstractions;
using Microsoft.Extensions.Logging.Abstractions;
Expand All @@ -17,7 +18,7 @@ public class ProxyConsoleFormatter : ConsoleFormatter
private const string _boxBottomLeft = "\u2570 ";
// used to align single-line messages
private const string _boxSpacing = " ";
private readonly Dictionary<int, List<RequestLog>> _requestLogs = [];
private readonly ConcurrentDictionary<int, List<RequestLog>> _requestLogs = [];
private readonly ProxyConsoleFormatterOptions _options;
const string labelSpacing = " ";
// label length + 2
Expand Down Expand Up @@ -62,24 +63,21 @@ private void LogRequest(RequestLog requestLog, string category, IExternalScopePr
{
if (messageType == MessageType.FinishedProcessingRequest)
{
var lastMessage = _requestLogs[requestId.Value].Last();
// log all request logs for the request
foreach (var log in _requestLogs[requestId.Value])
var currentRequestLogs = _requestLogs[requestId.Value];
var lastIndex = currentRequestLogs.Count - 1;
for (var i = 0; i < currentRequestLogs.Count; ++i)
{
WriteLogMessageBoxedWithInvertedLabels(log, scopeProvider, textWriter, log == lastMessage);
var log = currentRequestLogs[i];
WriteLogMessageBoxedWithInvertedLabels(log, scopeProvider, textWriter, i == lastIndex);
}
_requestLogs.Remove(requestId.Value);
_requestLogs.Remove(requestId.Value, out _);
}
else
{
// buffer request logs until the request is finished processing
if (!_requestLogs.TryGetValue(requestId.Value, out List<RequestLog>? value))
{
value = ([]);
_requestLogs[requestId.Value] = value;
}

requestLog.PluginName = category == DefaultCategoryName ? null : category;
var value = _requestLogs.GetOrAdd(requestId.Value, []);
value.Add(requestLog);
}
}
Expand Down
12 changes: 8 additions & 4 deletions dev-proxy/ProxyEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using DevProxy.Abstractions;
using Microsoft.VisualStudio.Threading;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Net;
using System.Security.Cryptography.X509Certificates;
Expand Down Expand Up @@ -38,7 +39,7 @@ public class ProxyEngine(IProxyConfiguration config, ISet<UrlToWatch> urlsToWatc
private readonly IProxyState _proxyState = proxyState ?? throw new ArgumentNullException(nameof(proxyState));
// Dictionary for plugins to store data between requests
// the key is HashObject of the SessionEventArgs object
private readonly Dictionary<int, Dictionary<string, object>> _pluginData = [];
private readonly ConcurrentDictionary<int, Dictionary<string, object>> _pluginData = [];
private InactivityTimer? _inactivityTimer;

public static X509Certificate2? Certificate => _proxyServer?.CertificateManager.RootCertificate;
Expand Down Expand Up @@ -469,7 +470,10 @@ async Task OnRequestAsync(object sender, SessionEventArgs e)
if (IsProxiedHost(e.HttpClient.Request.RequestUri.Host) &&
IsIncludedByHeaders(e.HttpClient.Request.Headers))
{
_pluginData.Add(e.GetHashCode(), []);
if (!_pluginData.TryAdd(e.GetHashCode(), []))
{
throw new Exception($"Unable to initialize the plugin data storage for hash key {e.GetHashCode()}");
}
var responseState = new ResponseState();
var proxyRequestArgs = new ProxyRequestArgs(e, responseState)
{
Expand Down Expand Up @@ -607,7 +611,7 @@ async Task OnAfterResponseAsync(object sender, SessionEventArgs e)
if (!proxyResponseArgs.HasRequestUrlMatch(_urlsToWatch))
{
// clean up
_pluginData.Remove(e.GetHashCode());
_pluginData.Remove(e.GetHashCode(), out _);
return;
}

Expand All @@ -623,7 +627,7 @@ async Task OnAfterResponseAsync(object sender, SessionEventArgs e)
_logger.LogRequest(message, MessageType.FinishedProcessingRequest, new LoggingContext(e));

// clean up
_pluginData.Remove(e.GetHashCode());
_pluginData.Remove(e.GetHashCode(), out _);
}
}

Expand Down