Monitor.Wait can throw ThreadInterruptedException without reacquiring the lock
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 42/100
Hướng nghiên cứu
Start with Condition.Wait and Lock.Reenter in System.Private.CoreLib, as linked in the issue, then trace the Monitor.Wait path introduced around #118371. Reproduce with the provided C# program on .NET 11 and inspect nearby synchronization tests for coverage. Done means Monitor.Wait throws ThreadInterruptedException only after reacquiring the lock at its prior recursion count, without leaving the waiter list inconsistent.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Description
On .NET 11, a Thread.Interrupt that arrives while Monitor.Wait is blocked reacquiring the lock makes Monitor.Wait throw ThreadInterruptedException without holding the lock. The enclosing lock statement then throws SynchronizationLockException from Monitor.Exit, which replaces the original exception.
The program below gets there with two interrupts: the first ends the wait, the second arrives during the reacquisition.
Reproduction Steps
Save as repro.cs and run dotnet run repro.cs:
object gate = new();
bool ready = false;
Thread worker = new(() =>
{
lock (gate)
{
Volatile.Write(ref ready, true);
try
{
Monitor.Wait(gate);
}
catch (ThreadInterruptedException)
{
Console.WriteLine($"Lock held after Monitor.Wait threw: {Monitor.IsEntered(gate)}");
}
}
});
worker.Start();
SpinWait.SpinUntil(() => Volatile.Read(ref ready));
// Entered once the worker is waiting.
lock (gate)
{
// Ends the wait: the worker now blocks to reacquire the lock.
worker.Interrupt();
Thread.Sleep(100);
// Interrupts the reacquisition.
worker.Interrupt();
Thread.Sleep(100);
}
worker.Join();
Expected behavior
A thread that enters Monitor.Wait holding the lock reacquires it, with its previous recursion count, before a ThreadInterruptedException reaches the caller. The program prints the following and exits normally, as it does on .NET 10:
Lock held after Monitor.Wait threw: True
Actual behavior
Lock held after Monitor.Wait threw: False
Unhandled exception. System.Threading.SynchronizationLockException: The calling thread does not hold the lock.
at System.Threading.Lock.Exit()
at System.Threading.Monitor.Exit(Object obj)
at Program.<>c__DisplayClass0_0.<<Main>$>b__0() in repro.cs:line 18
The ThreadInterruptedException caught in the worker has this stack trace:
at System.Threading.Thread.CheckForPendingInterrupt()
at System.Threading.WaitHandle.WaitForMultipleObjectsIgnoringSyncContext(IntPtr* pHandles, Int32 numHandles, Boolean waitAll, Int32 millisecondsTimeout, Boolean useTrivialWaits)
at System.Threading.WaitHandle.WaitOneNoCheck(Int32 millisecondsTimeout, Boolean useTrivialWaits, Object associatedObject, WaitHandleWaitSourceMap waitSource)
at System.Threading.Lock.TryEnterSlow(Int32 timeoutMs, Int32 currentThreadId)
at System.Threading.Lock.Enter()
at System.Threading.Condition.Wait(Int32 millisecondsTimeout, Object associatedObjectForMonitorWait)
Regression?
Yes. The program behaves as expected on .NET 10.0.15 and fails every time on .NET 11.0.0.
Known Workarounds
None confirmed.
Configuration
- .NET 11.0.0, CoreCLR
- Windows 11 x64 and Linux x64
Other information
Condition.Wait reacquires the lock in its finally block through Lock.Reenter, which calls Enter(), an interruptible wait:
When Reenter throws, the rest of that finally block, which removes the waiter from the list, does not run either.
CoreCLR has used this code for Monitor since #118371.
- Ngôn ngữ chính
- C#
- Star
- 18.4k
- Fork
- 5.8k
- Merge trung bình
- 2 ngày 19 giờ
- Pull request đã merge (30 ngày)
- 590
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của dotnet/runtime
-
area-Setup untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
dotnet/runtime#137376 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area-System.Memory untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
dotnet/runtime#137334 · 4 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
ImmutableSortedSet<T>.Builder: enumerating through IEnumerable<T> doesn't throw on modification and can return wrong itemsCó thể đã có người làm @aw0lid đã nhận 3 ngày trước. Đang mởarea-System.Collections
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
dotnet/runtime#137167 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
DbTransaction.SupportsSavepoints docs say Release/ReleaseAsync are expected to throw, but they are no-ops by designCó thể đã có người làm @alaricd đã nhận 7 ngày trước. Đang mởarea-System.Transactions untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
dotnet/runtime#137040 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
FileSystemGlobbing: Matcher throws ArgumentOutOfRangeException when the rootDir is the same value as the fileCó thể đã có người làm @makazeu đã nhận 7 ngày trước. Đang mởarea-Extensions-FileSystem
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
dotnet/runtime#136989 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của dotnet/runtime
Issue tương tự
-
bug emitter:client:csharp
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
microsoft/typespec#12572 · 1 bình luận · 1 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày
-
needs-area-label
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 2 ngày
-
AWS Lambda: KeyNotFoundException drops HTTP-triggered transactions when allowAllHeaders is disabledĐang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
newrelic/newrelic-dotnet-agent#3949 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area-networking
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
dotnet/aspnetcore#70545 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 87/100
Maintainer thường phản hồi trong vòng 1 ngày