fix: a built-in channel's wait, cancelled by its token, completes on the real thread pool

Aperta
#59 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

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
43/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
csharp

Direzione di ricerca

Start with SimulationTaskCompletionRace and the built-in channel cancellation callbacks described in the issue; inspect how UnboundedChannel<T> and the bounded channel complete pending waits. Then compare the controlled cancellation race with Task.WaitAsync and check the ChannelConformanceTests scenarios named in the issue. Done means cancelled waits complete through controlled operations and channel state still matches the BCL behavior.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Problem

A built-in channel completes a pending wait (ReadAsync, WaitToReadAsync, WriteAsync, WaitToWriteAsync, including the waits that ChannelReader<T>.ReadAllAsync makes) when the token passed to that wait is cancelled. It does this from a cancellation callback that queues the completion to the real thread pool: UnboundedChannel<T>'s CancellationCallbackDelegate calls ChannelUtilities.UnsafeQueueUserWorkItem, and the bounded channel behaves the same way.

When rewritten code cancels the token, even through the controlled CancellationTokenSource.Cancel, the waiter's value task completes on a thread-pool thread at a nondeterministic time. The controlled awaiter polls the value task's status, so the run usually observes the completion, but the step at which it becomes ready depends on real thread-pool timing.

How it was found

The thread-pool escape guard added in #57 (ThreadPoolEscapeGuard, an EventListener on FrameworkEventSource.ThreadPoolEnqueueWork) caught it in ChannelConformanceTests.ScenarioHasTheBclOutcome(combine/{method,enumeration,same}/pending). The enqueue's stack:

System.Threading.ThreadPool.UnsafeQueueUserWorkItem[TState](...)
System.Threading.Channels.ChannelUtilities.UnsafeQueueUserWorkItem[TState](...)
System.Threading.Channels.UnboundedChannel`1.<get_CancellationCallbackDelegate>b__13_0(Object state, CancellationToken cancellationToken)
System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(Boolean throwOnFirstException)
Clockwork.Runtime.Tasks.SimulationCompletion ... Cancel

#57 turns the guard off for those three scenarios, with a reference to this issue.

Possible fix

Don't pass the token to a built-in channel's wait. Instead, race the uncancellable wait against the token in a controlled operation, as Task.WaitAsync does (SimulationTaskCompletionRace). Then remove the waiter, or let a later write consume it, so the channel's state matches the BCL's.

Lingua principale
C#
Stelle
0
Fork
0
Merge medio
2h 32m
PR unite (30g)
23

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di ReubenBond/Clockwork

Tutte le issue di ReubenBond/Clockwork

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.