Monitor.Wait can throw ThreadInterruptedException without reacquiring the lock
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 42/100
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- C#
- Stelle
- 18.4k
- Fork
- 5.8k
- Merge medio
- 2g 19h
- PR unite (30g)
- 590
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di dotnet/runtime
-
area-Setup untriaged
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
dotnet/runtime#137376 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
area-System.Memory untriaged
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
dotnet/runtime#137334 · 4 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
ImmutableSortedSet<T>.Builder: enumerating through IEnumerable<T> doesn't throw on modification and can return wrong itemsForse già presa @aw0lid l’ha presa 3 giorni fa. Apertaarea-System.Collections
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
dotnet/runtime#137167 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
DbTransaction.SupportsSavepoints docs say Release/ReleaseAsync are expected to throw, but they are no-ops by designForse già presa @alaricd l’ha presa 7 giorni fa. Apertaarea-System.Transactions untriaged
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
dotnet/runtime#137040 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
FileSystemGlobbing: Matcher throws ArgumentOutOfRangeException when the rootDir is the same value as the fileForse già presa @makazeu l’ha presa 7 giorni fa. Apertaarea-Extensions-FileSystem
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
dotnet/runtime#136989 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di dotnet/runtime
Issue simili
-
bug emitter:client:csharp
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
microsoft/typespec#12572 · 1 commento · 1 reazione ·
I maintainer di solito rispondono entro 1 giorno
-
needs-area-label
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 2 giorni
-
AWS Lambda: KeyNotFoundException drops HTTP-triggered transactions when allowAllHeaders is disabledApertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
newrelic/newrelic-dotnet-agent#3949 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
area-networking
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
dotnet/aspnetcore#70545 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 87/100
I maintainer di solito rispondono entro 1 giorno