Commit aa2f5fd
ONNX GenAI Assistant
Fix: Add explicit Shutdown() call to nanobind cleanup
The nanobind version was missing an explicit Generators::Shutdown() call
during Python module cleanup that the pybind11 version had. This caused
OrtGlobals to be destroyed too late (during C++ static destructors instead
of during Python finalization), leading to use-after-free errors.
The issue manifested as Invalid read errors in valgrind when ONNX Runtime
CUDA provider memory was freed during session cleanup, but then accessed
again when OrtEnv destructor ran.
Changes:
- Added #include "../generators.h" to access Generators::Shutdown()
- Added Generators::Shutdown() call in the atexit cleanup lambda
- Updated comments to clarify the cleanup sequence
This ensures the same cleanup order as pybind11:
1. Python GC releases references
2. Generators::Shutdown() destroys OrtGlobals explicitly
3. Static destructor EnsureShutdown sees null and does nothing
Valgrind results:
- Before: 187 errors (including 2 Invalid reads)
- After: 173 errors (no Invalid reads)
- Matches pybind11 behavior (184 errors, no Invalid reads)1 parent 0eda34b commit aa2f5fd
1 file changed
+5
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
77 | 78 | | |
78 | 79 | | |
79 | 80 | | |
80 | | - | |
| 81 | + | |
81 | 82 | | |
82 | | - | |
| 83 | + | |
83 | 84 | | |
| 85 | + | |
| 86 | + | |
84 | 87 | | |
85 | 88 | | |
86 | 89 | | |
| |||
0 commit comments