Fix TaskHost crash when task returns string[] with null elements#13190
Conversation
16df08f to
b8f7786
Compare
When a task running in TaskHost returns a string[] containing null elements, BinaryTranslator.Translate(ref string[]) crashes because BinaryWriter.Write(null) throws ArgumentNullException. This fix filters null elements from string[] task outputs in OutOfProcTaskAppDomainWrapperBase before serialization. This produces identical results to in-process execution, where nulls are already filtered when converting to items in TaskExecutionHost.GatherArrayStringAndValueOutputs(). A low-importance message is logged when nulls are filtered. Fixes dotnet#13174
b8f7786 to
eaa70e4
Compare
ViktorHofer
left a comment
There was a problem hiding this comment.
Does the filtering need to be run under the try? Try/catch regions are more costly as the runtime puts barriers in those paths.
Not sure how much Linq we already use but if we want to avoid that, consider replacing the Linq.Where with a loop.
|
we generally avoid linq
|
There was a problem hiding this comment.
Pull request overview
This PR fixes a crash in MSBuild's TaskHost (out-of-process task execution) that occurs when a task returns a string[] containing null elements. The crash happens during binary serialization because BinaryWriter.Write(null) throws ArgumentNullException.
Changes:
- Added
FilterNullsFromStringArray()helper method inOutOfProcTaskAppDomainWrapperBase.csto remove null elements from string[] task outputs before serialization - Added resource string
TaskHostAcquired_NullsFilteredfor diagnostic logging when nulls are filtered - Updated all localization (xlf) files with the new resource string
Reviewed changes
Copilot reviewed 17 out of 17 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| src/MSBuild/OutOfProcTaskAppDomainWrapperBase.cs | Added null filtering logic for string[] task outputs and a helper method to efficiently filter null elements with diagnostic logging |
| src/MSBuild/Resources/Strings.resx | Added new resource string for diagnostic message when null elements are filtered |
| src/MSBuild/Resources/xlf/Strings.*.xlf | Updated all 14 localization files with the new resource string (marked as "new" requiring translation) |
…net#13190) Context When a task running in TaskHost (out-of-process task execution) returns a \string[]\ containing null elements, the build crashes with \ArgumentNullException\ in \BinaryTranslator.Translate(ref string[])\ because \BinaryWriter.Write(null)\ throws. affects TaskHost scenarios. In-process task execution already filters null elements when converting to items in TaskExecutionHost.GatherArrayStringAndValueOutputs(). Changes Made Added \FilterNullsFromStringArray()\ helper in \OutOfProcTaskAppDomainWrapperBase.cs\ that filters null elements from string[] task outputs before serialization Logs a \MessageImportance.Normal\ message when nulls are filtered (for diagnostic purposes) Performance-efficient: single pass to count nulls, only allocates if nulls exist Added resource string \TaskHostAcquired_NullsFiltered\ for the log message Testing end to end test with a task running in taskhost and producing null containing string array Notes The fix produces identical results to in-process execution since MSBuild already discards null elements when converting task output arrays to items.
Fixes #13174
Context
When a task running in TaskHost (out-of-process task execution) returns a \string[]\ containing null elements, the build crashes with \ArgumentNullException\ in \BinaryTranslator.Translate(ref string[])\ because \BinaryWriter.Write(null)\ throws.
affects TaskHost scenarios. In-process task execution already filters null elements when converting to items in
TaskExecutionHost.GatherArrayStringAndValueOutputs().Changes Made
Testing
end to end test with a task running in taskhost and producing null containing string array
Notes
The fix produces identical results to in-process execution since MSBuild already discards null elements when converting task output arrays to items.