Skip to content

Fix TaskHost crash when task returns string[] with null elements#13190

Merged
JanProvaznik merged 6 commits intodotnet:mainfrom
JanProvaznik:fix/taskhost-null-string-array-13174
Feb 4, 2026
Merged

Fix TaskHost crash when task returns string[] with null elements#13190
JanProvaznik merged 6 commits intodotnet:mainfrom
JanProvaznik:fix/taskhost-null-string-array-13174

Conversation

@JanProvaznik
Copy link
Copy Markdown
Member

@JanProvaznik JanProvaznik commented Feb 3, 2026

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

  • 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.

@JanProvaznik JanProvaznik force-pushed the fix/taskhost-null-string-array-13174 branch from 16df08f to b8f7786 Compare February 3, 2026 16:23
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
@JanProvaznik JanProvaznik force-pushed the fix/taskhost-null-string-array-13174 branch from b8f7786 to eaa70e4 Compare February 3, 2026 16:36
@JanProvaznik JanProvaznik marked this pull request as ready for review February 3, 2026 16:49
@JanProvaznik JanProvaznik self-assigned this Feb 3, 2026
Copy link
Copy Markdown
Member

@ViktorHofer ViktorHofer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@JanProvaznik
Copy link
Copy Markdown
Member Author

JanProvaznik commented Feb 3, 2026

we generally avoid linq
~~- I decided to use it because this is not a hot path (and the logging overhead dominates anyway)

  • I realized that Contains is also linq thing and I should use indexof instead
    ~~ I refactored
    filtering under the try: seems most logical to me to do it between the getter and setter, it would be confusing to do it after with managing the local variable scopes differently

Copilot AI review requested due to automatic review settings February 4, 2026 09:31
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 in OutOfProcTaskAppDomainWrapperBase.cs to remove null elements from string[] task outputs before serialization
  • Added resource string TaskHostAcquired_NullsFiltered for 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)

@JanProvaznik JanProvaznik enabled auto-merge (squash) February 4, 2026 11:16
@JanProvaznik JanProvaznik merged commit 4d130a2 into dotnet:main Feb 4, 2026
9 checks passed
JanProvaznik added a commit to JanProvaznik/msbuild that referenced this pull request Feb 25, 2026
…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.
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.

Taskhost should correctly serialize string[] containing null elements

3 participants