Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

ObjectDisposedException on shutdown

Abierto
#293 4 comentarios 4 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
32/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
csharp
Área
api, backend

Línea de trabajo

Comienza con WebApiAppBuilderExtensions.cs y HttpMessageHandlerAdapter.cs en el registro de disposición vinculado y, a continuación, sigue el flujo de host.OnAppDisposing y HostingEnvironment.StopListening. Determina cómo puede la disposición evitar interrumpir las solicitudes en curso y qué evento o mecanismo del ciclo de vida debería realizar la disposición final; se considerará terminado cuando el apagado ya no provoque ObjectDisposedException y los recursos se limpien finalmente.

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

Descripción

bug

An app running in IIS with OWIN integration gets ObjectDisposedExceptions on shutdown rather than gracefully draining requests.

Exception Details: System.ObjectDisposedException, Cannot access a disposed object.
Object name: '[Redacted].AuthenticationHandler'.,
    at System.Net.Http.DelegatingHandler.CheckDisposed()
   at System.Net.Http.DelegatingHandler.SetOperationStarted()
   at System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at [Redacted].AuthenticationHandler.<SendAsync>d__12.MoveNext()

Here's where dispose is called:

   at [Redacted].AuthenticationHandler.Dispose(Boolean disposing)
   at System.Net.Http.DelegatingHandler.Dispose(Boolean disposing)
   at System.Web.Http.HttpServer.Dispose(Boolean disposing)
   at System.Net.Http.HttpMessageInvoker.Dispose(Boolean disposing)
   at System.Web.Http.Owin.HttpMessageHandlerAdapter.OnAppDisposing()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.CancellationCallbackInfo.ExecuteCallback()
   at System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(Boolean throwOnFirstException)
   at System.Threading.CancellationTokenSource.NotifyCancellation(Boolean throwOnFirstException)
   at Microsoft.Owin.Host.SystemWeb.ShutdownDetector.Cancel()
   at Microsoft.Owin.Host.SystemWeb.ShutdownDetector.Stop(Boolean immediate)
   at System.Web.Hosting.HostingEnvironment.StopRegisteredObjects(Boolean immediate)
   at System.Web.Hosting.HostingEnvironment.InitiateShutdownWorkItemCallback(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()'

The root event here is HostingEnvironment.StopListening. "Occurs when the worker process or application pool associated with this host has stopped listening for new requests and will eventually shut down." The app is shutting down, but there may still be requests in flight that should be completed if possible.

Microsoft.Owin wired into the StopListening event so that SignalR could gracefully shut down long polling and WebSocket requests. WebAPI however wired up it's pipeline disposal to this event. This causes the pipeline to be disposed while request are in flight.
https://github.com/aspnet/AspNetWebStack/blob/ba26cfbfbf958d548e4c0a96e853250f13450dc6/src/System.Web.Http.Owin/WebApiAppBuilderExtensions.cs#L111
https://github.com/aspnet/AspNetWebStack/blob/ba26cfbfbf958d548e4c0a96e853250f13450dc6/src/System.Web.Http.Owin/WebApiAppBuilderExtensions.cs#L119-L128
https://github.com/aspnet/AspNetWebStack/blame/ba26cfbfbf958d548e4c0a96e853250f13450dc6/src/System.Web.Http.Owin/HttpMessageHandlerAdapter.cs#L87

There is no alternate event in IIS that fires when requests have drained.

Workaround: Remove the CancellationToken from the IAppBuilder so WebAPI cannot register with it.

appBuilder.Properties.Remove("host.OnAppDisposing");

Proposed fix: WebApi should not register for disposal with this event.
https://github.com/aspnet/AspNetWebStack/blame/ba26cfbfbf958d548e4c0a96e853250f13450dc6/src/System.Web.Http.Owin/HttpMessageHandlerAdapter.cs#L87
(When should it be disposed then?)

Lenguaje dominante
C#
Estrellas
897
Forks
358
Merge medio
40 min
PR fusionados (30 d)
1

Guía de contribución

Abrir la guía de contribución

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 aspnet/AspNetWebStack

Todos los issues de aspnet/AspNetWebStack

Issues similares

Más issues de C#

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.