RegexMatchTimeoutException when trying to parse HTTP version #627

Closed
opened 2025-12-29 15:29:17 +01:00 by adam · 2 comments
Owner

Originally created by @APErebus on GitHub (Aug 22, 2024).

Originally assigned to: @StefH on GitHub.

Describe the bug

In our automated testing, we are seeing intermittent failures due to the HttpVersionParse.Parse() throwing a RegexMatchTimeoutException.

{
   "ClassName":"System.Text.RegularExpressions.RegexMatchTimeoutException",
   "Message":"The Regex engine has timed out while trying to match a pattern to an input string. This can occur for many reasons, including very large inputs or excessive backtracking caused by nested quantifiers, back-references and other factors.",

   "Data":null,
   "InnerException":null,
   "HelpURL":null,
   "StackTraceString":"
      at System.Text.RegularExpressions.RegexRunner.g__ThrowRegexTimeout|25_0()
         at Regex354_TryMatchAtCurrentPosition(RegexRunner, ReadOnlySpan`1)
         at Regex354_Scan(RegexRunner, ReadOnlySpan`1)
         at System.Text.RegularExpressions.Regex.RunSingleMatch(RegexRunnerMode mode, Int32 prevlen, String input, Int32 beginning, Int32 length, Int32 startat)
         at System.Text.RegularExpressions.Regex.Match(String input)
         at WireMock.Util.HttpVersionParser.Parse(String protocol)
         at WireMock.Owin.Mappers.OwinRequestMapper.MapAsync(HttpRequest request, IWireMockMiddlewareOptions options)
         at WireMock.Owin.WireMockMiddleware.InvokeInternalAsync(HttpContext ctx)
         at WireMock.Owin.GlobalExceptionMiddleware.InvokeInternalAsync(HttpContext ctx)",
   "RemoteStackTraceString":null,
   "RemoteStackIndex":0,
   "ExceptionMethod":null,
   "HResult":-2146233083,
   "Source":"System.Text.RegularExpressions",
   "WatsonBuckets":null,
   "regexInput":"HTTP/1.1",
   "regexPattern":"HTTP/(\\d+(\\.\\d+)?(?!\\.))",
   "timeoutTicks":1000000
}

Expected behavior:

We expect the regex to parse all the time with no exceptions

Test to reproduce

We don't have reliable reproduceable test scenarios as it's intermittent. 1-2 failures every 1000 runs.

Our suspicion is that the 100ms timeout is too short and test systems under load might not be able to parse fast enough, throwing the exception.

Can we make this default value higher (500ms) or make it configurable so we can set it to a higher value?

Originally created by @APErebus on GitHub (Aug 22, 2024). Originally assigned to: @StefH on GitHub. ### Describe the bug In our automated testing, we are seeing intermittent failures due to the `HttpVersionParse.Parse()` throwing a `RegexMatchTimeoutException`. ``` { "ClassName":"System.Text.RegularExpressions.RegexMatchTimeoutException", "Message":"The Regex engine has timed out while trying to match a pattern to an input string. This can occur for many reasons, including very large inputs or excessive backtracking caused by nested quantifiers, back-references and other factors.", "Data":null, "InnerException":null, "HelpURL":null, "StackTraceString":" at System.Text.RegularExpressions.RegexRunner.g__ThrowRegexTimeout|25_0() at Regex354_TryMatchAtCurrentPosition(RegexRunner, ReadOnlySpan`1) at Regex354_Scan(RegexRunner, ReadOnlySpan`1) at System.Text.RegularExpressions.Regex.RunSingleMatch(RegexRunnerMode mode, Int32 prevlen, String input, Int32 beginning, Int32 length, Int32 startat) at System.Text.RegularExpressions.Regex.Match(String input) at WireMock.Util.HttpVersionParser.Parse(String protocol) at WireMock.Owin.Mappers.OwinRequestMapper.MapAsync(HttpRequest request, IWireMockMiddlewareOptions options) at WireMock.Owin.WireMockMiddleware.InvokeInternalAsync(HttpContext ctx) at WireMock.Owin.GlobalExceptionMiddleware.InvokeInternalAsync(HttpContext ctx)", "RemoteStackTraceString":null, "RemoteStackIndex":0, "ExceptionMethod":null, "HResult":-2146233083, "Source":"System.Text.RegularExpressions", "WatsonBuckets":null, "regexInput":"HTTP/1.1", "regexPattern":"HTTP/(\\d+(\\.\\d+)?(?!\\.))", "timeoutTicks":1000000 } ``` ### Expected behavior: We expect the regex to parse all the time with no exceptions ### Test to reproduce We don't have reliable reproduceable test scenarios as it's intermittent. 1-2 failures every 1000 runs. ### Other related info Our suspicion is that the 100ms timeout is too short and test systems under load might not be able to parse fast enough, throwing the exception. Can we make this default value higher (500ms) or make it configurable so we can set it to a higher value?
adam added the bug label 2025-12-29 15:29:17 +01:00
adam closed this issue 2025-12-29 15:29:17 +01:00
Author
Owner

@StefH commented on GitHub (Aug 22, 2024):

Thanks for noticing.

I did add a timeout to most Regex to be compliant with SonarCloud warnings.

I'll need to check all Regex and probably will take a higher value, 10 seconds.

@StefH commented on GitHub (Aug 22, 2024): Thanks for noticing. I did add a timeout to most Regex to be compliant with SonarCloud warnings. I'll need to check all Regex and probably will take a higher value, 10 seconds.
Author
Owner

@StefH commented on GitHub (Aug 22, 2024):

https://github.com/WireMock-Net/WireMock.Net/pull/1160

@StefH commented on GitHub (Aug 22, 2024): https://github.com/WireMock-Net/WireMock.Net/pull/1160
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#627