Skip to content

Commit 2ba19e1

Browse files
author
ONNX GenAI Assistant
committed
fix: Handle log callback with None parameter in nanobind
- Use nb::handle instead of nb::callable to accept both callable and None - Check is_none() explicitly before casting to callable - Fixes segfault when callback is invoked after being cleared - Store callback in unique_ptr to manage lifetime properly
1 parent b413234 commit 2ba19e1

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/python/python.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -323,15 +323,22 @@ void SetLogOptions(const nb::kwargs& dict) {
323323
}
324324

325325
void SetLogCallback(std::optional<nb::callable> callback) {
326-
static std::optional<nb::callable> log_callback;
327-
log_callback = callback;
328-
329-
if (log_callback.has_value()) {
326+
// Use a pointer to heap-allocated callable to avoid lifetime issues
327+
static std::unique_ptr<nb::callable> log_callback_ptr;
328+
329+
if (callback.has_value()) {
330+
// Store the callback on the heap and keep it alive
331+
log_callback_ptr = std::make_unique<nb::callable>(callback.value());
332+
330333
OgaPy::OgaCheckResult(OgaSetLogCallback([](const char* message, size_t length) {
331-
nb::gil_scoped_acquire gil;
332-
(*log_callback)(std::string_view(message, length));
334+
if (log_callback_ptr) {
335+
nb::gil_scoped_acquire gil;
336+
(*log_callback_ptr)(std::string_view(message, length));
337+
}
333338
}));
334339
} else {
340+
// Clear the callback
341+
log_callback_ptr.reset();
335342
OgaPy::OgaCheckResult(OgaSetLogCallback(nullptr));
336343
}
337344
}
@@ -862,7 +869,13 @@ NB_MODULE(onnxruntime_genai, m) {
862869
});
863870

864871
m.def("set_log_options", &SetLogOptions);
865-
m.def("set_log_callback", &SetLogCallback);
872+
m.def("set_log_callback", [](nb::handle callback) {
873+
if (callback.is_none()) {
874+
SetLogCallback(std::nullopt);
875+
} else {
876+
SetLogCallback(nb::cast<nb::callable>(callback));
877+
}
878+
});
866879

867880
m.def("is_cuda_available", []() { return USE_CUDA != 0; });
868881
m.def("is_dml_available", []() { return USE_DML != 0; });

0 commit comments

Comments
 (0)