Skip to content

Fix flaky RegularExpressionAttribute timeout test#127247

Open
danmoseley wants to merge 1 commit intodotnet:mainfrom
danmoseley:fix-regex-timeout-test
Open

Fix flaky RegularExpressionAttribute timeout test#127247
danmoseley wants to merge 1 commit intodotnet:mainfrom
danmoseley:fix-regex-timeout-test

Conversation

@danmoseley
Copy link
Copy Markdown
Member

Fix flaky RegularExpressionAttribute timeout test

Rare CI failure in RegularExpressionAttributeTests.Validate_MatchingTimesOut_ThrowsRegexMatchTimeoutException:

Assert.Throws() Failure: Exception type was not an exact match
Expected: typeof(System.Text.RegularExpressions.RegexMatchTimeoutException)
Actual:   typeof(System.ComponentModel.DataAnnotations.ValidationException)
---- System.ComponentModel.DataAnnotations.ValidationException : The field Object must match the regular expression '(a[ab]+)+$'.

The test uses pattern (a[ab]+)+$ with a 1ms timeout, expecting catastrophic backtracking to cause a RegexMatchTimeoutException. The input was a hardcoded 28-character string ("aaaaaaaaaaaaaaaaaaaaaaaaaaaa>"). With regex engine improvements, the backtracking sometimes completes within the 1ms timeout window before the timeout check fires. When that happens, IsValid() returns false (no match) and Validate() wraps it as a ValidationException instead of the expected RegexMatchTimeoutException.

The fix increases the input length from 28 to 100 characters (new string('a', 100) + ">"), making the exponential backtracking work (O(2^n)) large enough that the 1ms timeout is reliably exceeded.

Note

This change was AI-generated with GitHub Copilot.

The test used a 28-character input with a 1ms timeout, but
regex engine improvements allowed completion before the timeout
check fired, causing IsValid to return false instead of throwing
RegexMatchTimeoutException. Use 100 characters to ensure the
exponential backtracking exceeds the timeout.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a rare CI flake in RegularExpressionAttributeTests.Validate_MatchingTimesOut_ThrowsRegexMatchTimeoutException by making the test input reliably trigger a RegexMatchTimeoutException even with recent regex engine performance improvements.

Changes:

  • Increase the input length used in the timeout test from a fixed short literal to new string('a', 100) + ">" to more consistently exceed a 1ms regex timeout.
Show a summary per file
File Description
src/libraries/System.ComponentModel.Annotations/tests/System/ComponentModel/DataAnnotations/RegularExpressionAttributeTests.cs Strengthens the timeout test input to reduce flakiness and keep the expected exception stable.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 0

@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-componentmodel-dataannotations
See info in area-owners.md if you want to be subscribed.

@danmoseley
Copy link
Copy Markdown
Member Author

@danmoseley danmoseley requested review from tarekgh and removed request for tarekgh April 22, 2026 01:32
Copy link
Copy Markdown
Member

@tarekgh tarekgh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Thanks!

Copy link
Copy Markdown
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you @danmoseley !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants