Skip to content

Fix NullReferenceException in DSC ClearCache()#15373

Merged
anmenaga merged 3 commits into
PowerShell:masterfrom
anmenaga:NullRefDscFix
May 12, 2021
Merged

Fix NullReferenceException in DSC ClearCache()#15373
anmenaga merged 3 commits into
PowerShell:masterfrom
anmenaga:NullRefDscFix

Conversation

@anmenaga
Copy link
Copy Markdown

@anmenaga anmenaga commented May 10, 2021

PR Summary

In a multi-threaded environment there is a NullReferenceException in DSC ClearCache().
This fix is needed for GuestConfig.

PR Context

Initial values for fields marked with ThreadStaticAttribute should not be used, because such initialization occurs only once, when the class constructor executes, and therefore affects only one thread. Sometimes in a multi-threaded GuestConfig environment this was causing NullReferenceException in ClearCache() methods where the ThreadStatic filed t_currentImportDscResourceInvocations was accessed.

GuestConfig verified this fix on private binaries.

Side note: the entire section of code, where current fix is, will be removed soon by DSC as a subsystem PR.

PR Checklist

@anmenaga anmenaga added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label May 10, 2021
@ghost ghost assigned iSazonov May 10, 2021
@daxian-dbw
Copy link
Copy Markdown
Member

Good catch, @anmenaga! Quoted from docs:

Do not specify initial values for fields marked with ThreadStaticAttribute, because such initialization occurs only once, when the class constructor executes, and therefore affects only one thread. If you do not specify an initial value, you can rely on the field being initialized to its default value if it is a value type, or to null if it is a reference type.

Comment thread src/System.Management.Automation/DscSupport/JsonDscClassCache.cs Outdated
@anmenaga
Copy link
Copy Markdown
Author

This is needed for GC release in a couple of days, so merging.

@anmenaga anmenaga merged commit ae77246 into PowerShell:master May 12, 2021
@ghost
Copy link
Copy Markdown

ghost commented May 27, 2021

🎉v7.2.0-preview.6 has been released which incorporates this pull request.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants