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

Abierto
#59 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
43/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
csharp

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
C#
Estrellas
0
Forks
0
Merge medio
2 h 32 min
PR fusionados (30 d)
23

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de ReubenBond/Clockwork

Todos los issues de ReubenBond/Clockwork

Issues similares

Más issues de C#

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.