-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Description
Our application makes extensive use of the System.ComponentModel.Composition libraries and uses the CompositionContainer from many different threads. When the container is created, it is marked (via the constructor) as thread safe.
We've successfully used the container like this in .NET Framework 4.6.2 for many many years, but with a recent update to .NET 5, we're seeing some crashes.
Specifically, the path along CompositionContainer.Compose() does not look thread safe. We eventually get to RecompositionManager.GetAffectedParts, and while enumerating the alive items list, an exception is thrown because a different thread has modified that list. The other thread is in the middle of satisfying a call to GetExportedValue. Examination of the crashing stack reveals that there is no locking around the modified members.
Here's a snapshot of the stack that crashes:

The crash has been seen against .NET 5 (runtime version 5.0.9) and .NET 6 (runtime version 6.0.0-rc.1.21451.13).
Here is a (relatively) simple single file c# program that will reliably reproduce the crash we're seeing.