Fix StackOverflowException when serializing complex exceptions#3305
Draft
Fix StackOverflowException when serializing complex exceptions#3305
Conversation
Modified ErrorSerializerSettingsFactory to only serialize safe, well-known exception properties (Message, StackTrace, Source, HelpLink, HResult, Data, InnerException). This prevents serialization of complex properties from derived exception types like CsvHelper.FieldValidationException that can cause infinite recursion and StackOverflowException during JSON serialization. Also added MaxDepth=64 as an additional safety measure. Fixes #550 Co-authored-by: YunchuWang <12449837+YunchuWang@users.noreply.github.com>
Extracted CreateThrownException helper method to reduce code duplication in ErrorSerializerSettingsFactoryTests. Co-authored-by: YunchuWang <12449837+YunchuWang@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix CsvHelper FieldValidationException and StackOverflowException
Fix StackOverflowException when serializing complex exceptions
Jan 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
What changed?
ErrorSerializerSettingsFactory.ExceptionResolverto use an allowlist approach for exception property serializationMessage,StackTrace,Source,HelpLink,HResult,Data,InnerExceptionMaxDepth=64as an additional safety measureWhy is this change needed?
Exceptions with complex/deeply nested properties (e.g., CsvHelper's
FieldValidationExceptioncontainingReadingContextwith the entire CSV reader state) cause infinite recursion during JSON serialization.ReferenceLoopHandling.Ignoreonly handles true circular references (same object instance), not deep object graphs.StackOverflowExceptioncannot be caught, crashing the process.Issues / work items
Project checklist
WebJobs.Extensions.DurableTaskpackageEventSourcelogsv2.xbranchAI-assisted code disclosure (required)
Was an AI tool used? (select one)
If AI was used:
ErrorSerializerSettingsFactory.cs,ErrorSerializerSettingsFactoryTests.csAI verification (required if AI was used):
Testing
Automated tests
ErrorSerializerSettingsFactoryTests)Manual validation (only if runtime/behavior changed)
Notes for reviewers
System.Exceptionproperties needed for debugging/diagnostics.Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
azfunc.pkgs.visualstudio.com/opt/hostedtoolcache/CodeQL/2.23.8/x64/codeql/csharp/tools/linux64/Semmle.Autobuild.CSharp /opt/hostedtoolcache/CodeQL/2.23.8/x64/codeql/csharp/tools/linux64/Semmle.Autobuild.CSharp(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
This section details on the original issue you should resolve
<issue_title>CsvHelper FieldValidationException & Orchestrator throws StackOverflowException</issue_title>
<issue_description>I'm parsing a csv file with CsvHelper with bad data in it.
I have a StackOverflowException
You have a simple project attached to reproduce the issue.
soe.zip
</issue_description>
<agent_instructions>check if this issue still exists, if so fix it</agent_instructions>
Comments on the Issue (you are @copilot in this section)
@cgillum Thanks for this report and for the reference project. I was able to reproduce the issue. It looks like a bug in our exception serialization code. @cgillum The problem is `FieldValidationException`. It's a complex data structure and the Json.NET serializer we use runs into a `StackOverflowExcepti...💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.