- 
                Notifications
    
You must be signed in to change notification settings  - Fork 5.2k
 
STJ: Dispose enumerator on exception #100194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
STJ: Dispose enumerator on exception #100194
Conversation
        
          
                ...Json/src/System/Text/Json/Serialization/Converters/Collection/IEnumerableDefaultConverter.cs
              
                Outdated
          
            Show resolved
            Hide resolved
        
      There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's been a while since I last visited this problem, but IIRC fixing the issue is not as simple as proposed in this PR:
- It needs to take into account the state machines used in async serialization. See https://github.com/dotnet/runtime/pull/50778/files#diff-65021de09c4fb9b39607f1f3ed228e53cd04b483880f66b0ea56310f71a5e400R131-R137 for hints on how this was done for IAsyncEnumerable.
 - The serializer is now making recursive calls within a 
try/finallyclause in the serialization hot path. There is a good chance that this could regress performance so it would need to be benchmarked using the suite defined in thedotnet/performancerepo. - The change doesn't handle other enumerable converters such as the one for dictionary types.
 
        
          
                src/libraries/System.Text.Json/src/System/Text/Json/Serialization/WriteStackFrame.cs
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                src/libraries/System.Text.Json/src/System/Text/Json/Serialization/WriteStack.cs
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                ...System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfoOfT.WriteHelpers.cs
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                ....Json/src/System/Text/Json/Serialization/Converters/Collection/DictionaryDefaultConverter.cs
          
            Show resolved
            Hide resolved
        
              
          
                ...Json/src/System/Text/Json/Serialization/Converters/Collection/IEnumerableDefaultConverter.cs
          
            Show resolved
            Hide resolved
        
      c1c77cb    to
    576c426      
    Compare
  
    | 
           The implementation looks good to me now, however my concerns about this potentially regressing performance because we're adding a  Given that this change is made to address a hypothetical bug that nobody has actually has reported (Disposable IEnumerators are largely legacy and if they do exist chances are users would be better off using  I'm going to close this PR for now and the related issue, thank you for taking the time to work on this!  | 
    
          
 Not disputing the rest of your comment, but can you elaborate on this? Any C# iterator with a try/finally is going to have a meaningful IDisposable.Dispose.  | 
    
| 
           I wasn't aware of that use case. It definitely adds more points in favor of us doing this, but we still need to run the benchmarks against the change.  | 
    
| 
           Ok, let's see if we can get this merged. I rebased the branch and pushed a change the removes the need for new   | 
    
Fixes #50851