Skip to content

[BUG]: Concurrency issue in ProxyEngine, ConcolFormatter, and MockResponsePlugin #1161

@bartizan

Description

@bartizan

Description

When running some simultaneous requests that have to be mocked cause unpredictable results

Expected behaviour

Expected that all requests:

  • should be processed and returned a mock response;
  • should be logged in console a block with begging and end;
  • no exception should occur in MockResponsePlugin plugin.

Actual behaviour

  • not all requests are properly processed, some were run against original endpoints;
  • some logging blocks could be missing or in a tangle;
  • an exception occur in MockResponsePlugin plugin.

Image

Image

Steps to reproduce

  1. run devproxy with provided configuration to mock responses of sample.com

devproxy -c .\devproxyrc.json

  1. run demo app to spawn a few request (in the description, it is 3 requests but the more the easier to reproduce. You might need to change that depending on your hardware).

dotnet run

Dev Proxy Version

0.27.0

Operating system (environment)

Windows

Shell

PowerShell

Configuration file

devproxyrc.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.27.0/rc.schema.json",
  "plugins": [
    {
      "name": "MockResponsePlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
      "configSection": "mockResponsePlugin"
    }
  ],
  "urlsToWatch": [
    "https://example.com/*"
  ],
  "mockResponsePlugin": {
    "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.27.0/mockresponseplugin.schema.json",
    "mocksFile": "mocks.json"
  },
  "logLevel": "trace",
  "newVersionNotification": "none",
  "showSkipMessages": true,
  "showTimestamps": true,
  "validateSchemas": false
}

mocks.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.27.0/mockresponseplugin.mocksfile.schema.json",
  "mocks": [
    {
      "request": {
        "url": "https://example.com/*",
        "method": "GET"
      },
      "response": {
        "statusCode": 201,
        "body": {
          "userId": 1,
          "id": 1,
          "title": "sunt aut facere repellat provident",
          "body": "quia et suscipit\nsuscipit recusandae consequunhitecto"
        },
        "headers": [
          {
            "name": "Date",
            "value": "Wed, 19 Feb 2025 09:03:37 GMT"
          },
          {
            "name": "Content-Type",
            "value": "application/json; charset=utf-8"
          },
          {
            "name": "Content-Length",
            "value": "292"
          }
        ]
      }
    }
  ]
}

Program.cs

using HttpClient client = new();
var urls = Enumerable.Range(0, 3)
    .Select(i => $"https://example.com?iteration={i:0#}");

await Parallel.ForEachAsync(
    source: urls,
    body: (url, cancellationToken) => GetAsync(client, url, cancellationToken));

static async ValueTask GetAsync(HttpClient client, string url, CancellationToken cancellationToken)
{
    using var response = await client.GetAsync(url, cancellationToken);
    Console.WriteLine($"URL: {url}, HTTP status code: {response.StatusCode} ({(int)response.StatusCode})");
}

Additional Info

P.S. Some invesigation reveals that the listed concurrency issues are just the beginning, there could be more.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions