Skip to content

Conversation

@peombwa
Copy link
Member

@peombwa peombwa commented Jun 15, 2023

Fixes #2064 by addressing thread safety intermittent bug in .NET framework when copying request content header.

Changes proposed in this pull request

  • Updates how request content headers are copied when cloning a request.

@jberezanski-mdg
Copy link

jberezanski-mdg commented Jun 16, 2023

I'm not sure this will fix the problem. The update from

 foreach (var contentHeader in originalRequest.Content.Headers)

to

originalRequest.Content.Headers?.ToList()

does not change the behavior much - ToList() still iterates the Dictionary contained inside HttpHeaders. It might execute slightly faster, so the time window in which some other thread could cause the headers dictionary to be modified is narrower and the chance of encountering the InvalidOperationException is smaller, but I believe it is still nonzero.

Looking at the decompiled sources of HttpHeaders and Dictionary in .NET 4.8.1, the only reliable (but ugly) workaround I can see right now is to wrap originalRequest.Content.Headers?.ToList() in a try...catch(InvalidOperationException) and retry if the exception happens.

@peombwa
Copy link
Member Author

peombwa commented Jun 19, 2023

Thanks @jberezanski-mdg for the suggestion. That's right! Adding a retry once on InvalidOperationException appears to be the only viable workaround for .NET Framework. This shouldn't be an issue for customers running on PowerShell 7.x (.NET 7).

@peombwa peombwa self-assigned this Jun 19, 2023
@peombwa peombwa marked this pull request as ready for review June 19, 2023 22:47
@peombwa peombwa merged commit 8d07218 into features/2.0 Jun 29, 2023
@peombwa peombwa deleted the bugfixes/UpdateCloneMethod branch June 29, 2023 15:23
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.

4 participants