Access violation at exit on Windows when a collection has just run

Abierto
#1,470 4 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
18/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
cpp

Línea de trabajo

The fault is in src/hx/gc/Immix.cpp: start at finishThreadJob and sThreadPoolLock, then follow how tpjAsyncZero jobs run on GlobalAllocator::SThreadLoop and when the static HxMutex is destroyed at exit. The issue has no standalone reproduction, so first confirm the ordering problem on Windows. Done means a collection followed by Sys.exit no longer faults, and the collector threads are stopped or joined before the lock is torn down.

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

Descripción

Version: hxcpp 4.3.2, Haxe 4.3.6, MSVC, x64 (GitHub's windows-latest runner).

What happens: a program that calls Sys.exit, or returns from main, shortly after a collection can end with an access violation instead of its exit code. It is intermittent: it depends on whether the collector's threads are still zeroing memory when the C runtime runs static destructors.

Where: with every thread's stack printed at the fault (cdb):

  • The main thread is in the C runtime's common_exit, past the static destructors, called from Sys.exit.
  • The faulting thread is a collector thread: GlobalAllocator::SThreadLoop -> ThreadLoop -> finishThreadJob -> TAutoLock<HxMutex> -> RtlEnterCriticalSection -> RtlpWaitOnCriticalSection.

Why, from src/hx/gc/Immix.cpp: a collection ends by starting a job (tpjAsyncZero or tpjAsyncZeroJit) that zeroes reclaimed memory on the collector's threads, and does not wait for it. Each thread calls finishThreadJob when it is done, which locks sThreadPoolLock, a static HxMutex. On Windows ~HxMutex is DeleteCriticalSection, which exit runs while those threads are still going, so a thread that finishes afterwards enters a critical section that has been deleted.

To reproduce: it was met in a test program that allocates heavily, forces a collection (cpp.vm.Gc.run(true)), prints a line and exits at once; the same program that did more work after its collection did not fault. It has not been reduced to a standalone program.

Possible fixes: wait for, or stop, the thread jobs before the lock is destroyed; or do not destroy sThreadPoolLock at exit.

Way round in the meantime: flush standard output and call _Exit, so that no static destructor runs.

Lenguaje dominante
C++
Estrellas
330
Forks
237
Merge medio
1 d 3 h
PR fusionados (30 d)
20

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

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 HaxeFoundation/hxcpp

Todos los issues de HaxeFoundation/hxcpp

Issues similares

Más issues de C++

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.