Skip to content
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

Avoid deadlocks in TypeDescriptor #97236

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public sealed class TypeDescriptor
// A value of `null` indicates initialization is in progress.
// A value of s_initializedDefaultProvider indicates the provider is initialized.
private static readonly object s_initializedDefaultProvider = new object();
private static readonly object s_defaultProviderSyncObject = new object();

private static WeakHashtable? s_associationTable;
private static int s_metadataVersion; // a version stamp for our metadata. Used by property descriptors to know when to rebuild attributes.
Expand Down Expand Up @@ -271,15 +270,18 @@ private static void CheckDefaultProvider(Type type)
return;
}

lock (s_defaultProviderSyncObject)
// Lock on s_providerTable even though s_providerTable is not modified here.
// Using a single lock prevents deadlocks since other methods that call into or are called
// by this method also lock on s_providerTable and the ordering of the locks may be different.
lock (s_providerTable)
{
AddDefaultProvider(type);
}
}

/// <summary>
/// Add the default provider, if it exists.
/// For threading, this is always called under a 'lock (s_defaultProviderSyncObject)'.
/// For threading, this is always called under a 'lock (s_providerTable)'.
/// </summary>
private static void AddDefaultProvider(Type type)
{
Expand Down Expand Up @@ -326,11 +328,11 @@ private static void AddDefaultProvider(Type type)
}

/// <summary>
/// The CreateAssocation method creates an association between two objects.
/// The CreateAssociation method creates an association between two objects.
/// Once an association is created, a designer or other filtering mechanism
/// can add properties that route to either object into the primary object's
/// property set. When a property invocation is made against the primary
/// object, GetAssocation will be called to resolve the actual object
/// object, GetAssociation will be called to resolve the actual object
/// instance that is related to its type parameter.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Advanced)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DotNet.RemoteExecutor;
using Moq;
using Xunit;

Expand Down Expand Up @@ -1289,6 +1290,57 @@ public void ConcurrentGetProperties_ReturnsExpected()
}
}

[SkipOnPlatform(TestPlatforms.Browser, "Thread.Start is not supported on browsers.")]
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public static void ConcurrentAddProviderAndGetProvider()
{
// Use a timeout value lower than RemoteExecutor in order to get a nice Fail message.
const int Timeout = 50000;

RemoteInvokeOptions options = new()
{
TimeOut = 60000
};

ConcurrentAddProvider();
steveharter marked this conversation as resolved.
Show resolved Hide resolved

RemoteExecutor.Invoke(() =>
{
using var finished = new CountdownEvent(2);

Thread t1 = new Thread(() =>
{
ConcurrentAddProvider();
finished.Signal();
});

Thread t2 = new Thread(() =>
{
ConcurrentGetProvider();
finished.Signal();
});

t1.Start();
t2.Start();
finished.Wait(Timeout);

if (finished.CurrentCount != 0)
{
Assert.Fail("Timeout. Possible deadlock.");
}
}, options).Dispose();

static void ConcurrentAddProvider()
{
TypeDescriptor.AddProvider(new EmptyPropertiesTypeProvider(), typeof(MyClass));
}

static void ConcurrentGetProvider()
{
TypeDescriptor.GetProvider(typeof(TypeWithProperty));
}
}

public sealed class EmptyPropertiesTypeProvider : TypeDescriptionProvider
{
private sealed class EmptyPropertyListDescriptor : ICustomTypeDescriptor
Expand Down
Loading