Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions src/coreclr/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11927,6 +11927,9 @@ bool Compiler::impWrapTopOfStackInAwait()
info.compIsStatic ? fgGetCritSectOfStaticMethod() : gtNewLclvNode(info.compThisArg, TYP_REF);
GenTree* exitMon = gtNewHelperCallNode(CORINFO_HELP_MON_EXIT, TYP_VOID, lockObject, varAddrNode);
impAppendTree(exitMon, CHECK_SPILL_ALL, impCurStmtDI);

// The fault handler must not release the monitor again if the await throws.
impStoreToTemp(lvaMonAcquired, gtNewZeroConNode(TYP_I_IMPL), CHECK_SPILL_ALL);
}

if (impFoldAwaitedTopOfStack())
Expand Down
43 changes: 43 additions & 0 deletions src/tests/async/regression/synchronized-async-version.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,56 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

public class Async2Synchronized
{
[Theory]
[InlineData(false, false)]
[InlineData(false, true)]
[InlineData(true, false)]
[InlineData(true, true)]
public static void FaultedAwaitable(bool useValueTask, bool callerHoldsLock)
{
Async2Synchronized p = new();
InvalidOperationException expected = new("boom");
Task task = Task.FromException(expected);

if (callerHoldsLock)
{
Monitor.Enter(p);
}

try
{
InvalidOperationException actual = Assert.Throws<InvalidOperationException>(() =>
{
if (useValueTask)
{
p.FooValueTask(new ValueTask(task)).GetAwaiter().GetResult();
}
else
{
p.Foo(task).GetAwaiter().GetResult();
}
});

Assert.Same(expected, actual);
Assert.Equal(callerHoldsLock, Monitor.IsEntered(p));
}
finally
{
if (Monitor.IsEntered(p))
{
Monitor.Exit(p);
}
}
}

[Fact]
public static void TestEntryPoint()
{
Expand Down
Loading