Skip to content

Conversation

@erossignon
Copy link
Contributor

fixes #1166

This commit addresses a critical thread-safety issue in the GetSoapAction
method when parsing the Content-Type header on .NET 8 and later.

The previous implementation could lead to a race condition where a buffer rented
from `ArrayPool<Range>.Shared` was returned more than once.
This double-return could cause memory corruption and unpredictable behavior under
high concurrent loads.

The fix consists of the following changes:

- A `try-finally` block is now used to ensure the rented buffer is returned to the
  pool exactly once, regardless of the execution path.
- The loops for parsing the header now `break` as soon as the `soapAction`
  is found, improving efficiency.
- Added boundary checks to prevent potential `IndexOutOfRangeException`
  when processing malformed headers.

Additionally, a new concurrency stress test, `GetSoapAction_IsThreadSafe`, has been added
to `SoapCore.Tests`.

This test uses `Parallel.For` to execute a high volume of concurrent calls to `GetSoapAction`,
verifying that the fix is robust and effectively prevents the race condition.

Replaced the use of `asyncMessageFilters.Reverse()` with a direct backward
for-loop iteration to prevent unnecessary array allocations, addressing
potential performance issues when processing response message filters.
…gDes#1166

This commit addresses a critical thread-safety issue in the `GetSoapAction`
method when parsing the `Content-Type` header on .NET 8 and later.

The previous implementation could lead to a race condition where a buffer rented
from `ArrayPool<Range>.Shared` was returned more than once.
This double-return could cause memory corruption and unpredictable behavior under
high concurrent loads.

The fix consists of the following changes:

- A `try-finally` block is now used to ensure the rented buffer is returned to the
  pool exactly once, regardless of the execution path.
- The loops for parsing the header now `break` as soon as the `soapAction`
  is found, improving efficiency.
- Added boundary checks to prevent potential `IndexOutOfRangeException`
  when processing malformed headers.

Additionally, a new concurrency stress test, `GetSoapAction_IsThreadSafe`, has been added
to `SoapCore.Tests`.

This test uses `Parallel.For` to execute a high volume of concurrent calls to `GetSoapAction`,
verifying that the fix is robust and effectively prevents the race condition.

relates to issue DigDes#1166
@andersjonsson
Copy link
Collaborator

Nice, thank you!

@andersjonsson andersjonsson merged commit 55bbb2d into DigDes:develop Dec 11, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

thread-safety issue in HeadersHelper.GetSoapAction due to double-returning of ArrayPool buffer

2 participants