• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

llnl-asr / dftracer-utils / 45735107228

26 Nov 2026 01:20AM UTC coverage: 77.696% (+0.4%) from 77.313%
45735107228

push

github

rayandrew
fix: convert natively at the edge and read chunked nested columns safely

to_pandas, from_pandas, table files, loc, reindex, where, unnest and the
dask viewer called pyarrow kernels to convert columns, so they failed
without pyarrow and copied text twice. They now use native codes, masks
and buffers, and pyarrow appears only when a user asks for Arrow.
Readers of list offsets and struct children now go through flat pieces
of a column, so a chunked, selected or dictionary nested column no
longer crashes explode, the CLI printer or child access.

164371 of 253360 branches covered (64.88%)

Branch coverage included in aggregate %.

1455 of 1580 new or added lines in 20 files covered. (92.09%)

795 existing lines in 33 files now uncovered.

190713 of 203655 relevant lines covered (93.65%)

4484549.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.43
/src/dftracer/utils/python/runtime/task_handle.cpp
1
// SPDX-License-Identifier: MIT
2
#define PY_SSIZE_T_CLEAN
3
#include <Python.h>
4
#include <dftracer/utils/dstd/memory.h>
5
#include <dftracer/utils/dstd/no_destructor.h>
6
#include <dftracer/utils/python/core/method.h>
7
#include <dftracer/utils/python/core/type_helpers.h>
8
#include <dftracer/utils/python/runtime/task_handle.h>
9

10
#include <condition_variable>
11
#include <mutex>
12
#include <new>
13
#include <utility>
14

15
namespace {
16

17
using dftracer::utils::python::runtime::AsyncState;
18
using StatePtr = dftracer::utils::dstd::SharedPtr<AsyncState>;
19

20
struct TaskHandleObject {
21
    PyObject_HEAD StatePtr state;
22
    // The converted result, kept so a second get() returns it again.
23
    PyObject *value;
24
};
25

26
PyTypeObject TaskHandleType;
27

28
bool python_is_finalizing() {
420 ✔
29
#if PY_VERSION_HEX >= 0x030D0000
30
    return Py_IsFinalizing();
31
#else
32
    return _Py_IsFinalizing();
420 ✔
33
#endif
34
}
35

36
void call_callback(PyObject *fn, PyObject *handle) {
426 ✔
37
    PyObject *result = PyObject_CallFunctionObjArgs(fn, handle, nullptr);
426 ✔
38
    if (result)
426 ✔
39
        Py_DECREF(result);
424 ✔
40
    else
41
        PyErr_WriteUnraisable(fn);
2 ✔
42
}
426 ✔
43

44
}  // namespace
45

46
namespace dftracer::utils::python::runtime {
47

48
namespace {
49

50
// Native threads that may still take the GIL. The exit hook stops new ones and
51
// waits for those inside, so none reaches PyGILState_Ensure once the
52
// interpreter finalizes.
53
struct CallbackGate {
109 ✔
54
    std::mutex mutex;
55
    std::condition_variable idle;
56
    bool closing{false};
109 ✔
57
    int active{0};
109 ✔
58
};
59

60
CallbackGate &callback_gate() {
1,062 ✔
61
    static dstd::NoDestructor<CallbackGate> gate;
1,062 !
62
    return *gate;
1,062 ✔
63
}
64

65
}  // namespace
66

67
void AsyncState::wait() {
1,276 ✔
68
    std::unique_lock<std::mutex> lock(mutex_);
1,276 ✔
69
    ended_.wait(lock, [this] { return done(); });
2,702 !
70
}
1,276 ✔
71

72
bool AsyncState::add_callback(PyObject *handle, PyObject *fn) {
430 ✔
73
    std::lock_guard<std::mutex> lock(mutex_);
430 ✔
74
    if (done()) return false;
430 ✔
75
    if (!owner_) {
424 !
76
        Py_INCREF(handle);
424 !
77
        owner_ = handle;
424 ✔
78
    }
212 ✔
79
    Py_INCREF(fn);
424 !
80
    callbacks_.push_back(fn);
424 !
81
    return true;
212 ✔
82
}
430 ✔
83

84
void AsyncState::finish() {
1,286 ✔
85
    dstd::Vector<PyObject *> pending;
1,286 ✔
86
    PyObject *owner = nullptr;
1,286 ✔
87
    {
643 ✔
88
        std::lock_guard<std::mutex> lock(mutex_);
1,286 !
89
        pending.swap(callbacks_);
1,284 ✔
90
        owner = std::exchange(owner_, nullptr);
1,284 ✔
91
        done_.store(true, std::memory_order_release);
1,284 ✔
92
    }
1,286 ✔
93
    ended_.notify_all();
1,286 ✔
94
    if (pending.empty()) return;
1,286 ✔
95
    if (!enter_python()) return;
424 !
96
    PyGILState_STATE gil = PyGILState_Ensure();
420 !
97
    for (PyObject *fn : pending) {
840 ✔
98
        call_callback(fn, owner);
420 !
99
        Py_DECREF(fn);
630 !
100
    }
210 ✔
101
    Py_DECREF(owner);
420 !
102
    PyGILState_Release(gil);
420 !
103
    leave_python();
420 !
104
}
1,286 !
105

106
bool enter_python() {
424 ✔
107
    CallbackGate &gate = callback_gate();
424 ✔
108
    std::lock_guard<std::mutex> lock(gate.mutex);
424 ✔
109
    if (gate.closing || python_is_finalizing()) return false;
424 !
110
    ++gate.active;
420 ✔
111
    return true;
420 ✔
112
}
424 ✔
113

114
void leave_python() {
420 ✔
115
    CallbackGate &gate = callback_gate();
420 ✔
116
    std::lock_guard<std::mutex> lock(gate.mutex);
420 ✔
117
    --gate.active;
420 ✔
118
    gate.idle.notify_all();
420 ✔
119
}
420 ✔
120

121
namespace {
122

123
PyObject *close_task_callbacks(PyObject *, PyObject *) {
218 ✔
124
    CallbackGate &gate = callback_gate();
218 ✔
125
    Py_BEGIN_ALLOW_THREADS {
218 ✔
126
        std::unique_lock<std::mutex> lock(gate.mutex);
218 ✔
127
        gate.closing = true;
218 ✔
128
        gate.idle.wait(lock, [&] { return gate.active == 0; });
329 !
129
    }
218 ✔
130
    Py_END_ALLOW_THREADS Py_RETURN_NONE;
327 ✔
131
}
109 ✔
132

133
PyMethodDef task_handle_module_methods[] = {
134
    {"_close_task_callbacks", close_task_callbacks, METH_NOARGS,
135
     "Drop the done callbacks that have not started and wait for those "
136
     "running. Called at interpreter exit."},
137
    {nullptr, nullptr, 0, nullptr}};
138

139
}  // namespace
140

141
}  // namespace dftracer::utils::python::runtime
142

143
namespace {
144

145
PyObject *TaskHandle_add_done_callback(TaskHandleObject *self, PyObject *fn) {
430 ✔
146
    if (!PyCallable_Check(fn)) {
430 ✔
UNCOV
147
        PyErr_SetString(PyExc_TypeError, "callback must be callable");
×
UNCOV
148
        return nullptr;
×
149
    }
150
    PyObject *handle = reinterpret_cast<PyObject *>(self);
430 ✔
151
    if (!self->state->add_callback(handle, fn)) call_callback(fn, handle);
430 ✔
152
    Py_RETURN_NONE;
215 ✔
153
}
215 ✔
154

155
PyObject *TaskHandle_cancel(TaskHandleObject *self,
8 ✔
156
                            PyObject *Py_UNUSED(ignored)) {
157
    self->state->cancel();
8 ✔
158
    Py_RETURN_NONE;
8 ✔
159
}
160

161
void TaskHandle_dealloc(TaskHandleObject *self) {
1,282 ✔
162
    Py_XDECREF(self->value);
1,282 ✔
163
    self->state.~StatePtr();
1,282 !
164
    Py_TYPE(self)->tp_free(reinterpret_cast<PyObject *>(self));
1,282 ✔
165
}
1,282 ✔
166

167
PyObject *TaskHandle_get(TaskHandleObject *self, PyObject *Py_UNUSED(ignored)) {
1,280 ✔
168
    if (!self->value) {
1,280 ✔
169
        AsyncState *state = self->state.get();
1,276 ✔
170
        Py_BEGIN_ALLOW_THREADS state->wait();
1,276 ✔
171
        Py_END_ALLOW_THREADS
1,276 ✔
172
            // Another thread may have converted it while this one waited.
173
            if (!self->value) {
1,276 ✔
174
            PyObject *converted = state->to_python();
1,276 ✔
175
            if (!converted) return nullptr;
1,276 ✔
176
            self->value = converted;
1,260 ✔
177
        }
638 ✔
178
    }
638 ✔
179
    Py_INCREF(self->value);
1,264 ✔
180
    return self->value;
1,264 ✔
181
}
640 ✔
182

183
PyObject *TaskHandle_wait(TaskHandleObject *self,
4 ✔
184
                          PyObject *Py_UNUSED(ignored)) {
185
    PyObject *value = TaskHandle_get(self, nullptr);
4 ✔
186
    if (!value) return nullptr;
4 !
187
    Py_DECREF(value);
4 ✔
188
    Py_RETURN_NONE;
2 ✔
189
}
2 ✔
190

191
PyObject *TaskHandle_done(TaskHandleObject *self,
3,130 ✔
192
                          PyObject *Py_UNUSED(ignored)) {
193
    return PyBool_FromLong(self->state->done() ? 1 : 0);
5,977 ✔
194
}
195

196
PyObject *TaskHandle_get_name(TaskHandleObject *self, void *) {
1,280 ✔
197
    const auto &name = self->state->name();
1,280 ✔
198
    return PyUnicode_FromStringAndSize(name.data(),
2,560 ✔
199
                                       static_cast<Py_ssize_t>(name.size()));
1,280 ✔
200
}
640 ✔
201

202
PyObject *TaskHandle_get_task_id(TaskHandleObject *self, void *) {
1,278 ✔
203
    return PyLong_FromLongLong(static_cast<long long>(self->state->id()));
1,278 ✔
204
}
205

206
PyMethodDef TaskHandle_methods[] = {
207
    {"get", DFTU_PYCFUNCTION(TaskHandle_get), METH_NOARGS,
208
     "get() -> result\n"
209
     "--\n"
210
     "\n"
211
     "Block, with the GIL released, until the task ends. Return its result\n"
212
     "or raise its error. A second call returns the same result.\n"},
213
    {"wait", DFTU_PYCFUNCTION(TaskHandle_wait), METH_NOARGS,
214
     "wait() -> None\n"
215
     "--\n"
216
     "\n"
217
     "Block until the task ends. Raise its error.\n"},
218
    {"done", DFTU_PYCFUNCTION(TaskHandle_done), METH_NOARGS,
219
     "done() -> bool\n"
220
     "--\n"
221
     "\n"
222
     "Return True if the task has ended, without blocking.\n"},
223
    {"add_done_callback", DFTU_PYCFUNCTION(TaskHandle_add_done_callback),
224
     METH_O,
225
     "add_done_callback(fn) -> None\n"
226
     "--\n"
227
     "\n"
228
     "Call fn(handle) once when the task ends: on the calling thread if it\n"
229
     "already has, otherwise on the pool worker that finished it, which\n"
230
     "takes the GIL for the call. A slow callback holds that worker. An\n"
231
     "exception of fn is reported as unraisable.\n"},
232
    {"cancel", DFTU_PYCFUNCTION(TaskHandle_cancel), METH_NOARGS,
233
     "cancel() -> None\n"
234
     "--\n"
235
     "\n"
236
     "Ask the task to stop. get() then raises its cancelled error.\n"},
237
    {nullptr, nullptr, 0, nullptr}};
238

239
PyGetSetDef TaskHandle_getsetters[] = {
240
    {"name", reinterpret_cast<getter>(TaskHandle_get_name), nullptr,
241
     "Task name", nullptr},
242
    {"task_id", reinterpret_cast<getter>(TaskHandle_get_task_id), nullptr,
243
     "Task identifier", nullptr},
244
    {nullptr, nullptr, nullptr, nullptr, nullptr}};
245

246
}  // namespace
247

248
namespace dftracer::utils::python::runtime {
249

250
PyObject *new_task_handle(dstd::SharedPtr<AsyncState> state) {
1,286 ✔
251
    auto *self = reinterpret_cast<TaskHandleObject *>(
1,929 ✔
252
        TaskHandleType.tp_alloc(&TaskHandleType, 0));
1,286 ✔
253
    if (!self) return nullptr;
1,286 !
254
    new (&self->state) StatePtr(std::move(state));
1,286 ✔
255
    self->value = nullptr;
1,286 ✔
256
    return reinterpret_cast<PyObject *>(self);
1,286 ✔
257
}
643 ✔
258

259
int init_task_handle(PyObject *m) {
218 ✔
260
    TaskHandleType = {};
218 ✔
261
    TaskHandleType.ob_base = {PyObject_HEAD_INIT(nullptr) 0};
218 ✔
262
    TaskHandleType.tp_name = "dftracer.utils.dftracer_utils_ext.TaskHandle";
218 ✔
263
    TaskHandleType.tp_basicsize = sizeof(TaskHandleObject);
218 ✔
264
    TaskHandleType.tp_dealloc =
218 ✔
265
        reinterpret_cast<destructor>(TaskHandle_dealloc);
266
    TaskHandleType.tp_flags = Py_TPFLAGS_DEFAULT;
218 ✔
267
    TaskHandleType.tp_doc =
218 ✔
268
        "Handle to a native task that runs on the Runtime pool.\n"
269
        "\n"
270
        "Made by build_async() and collect_async(); not constructible.";
271
    TaskHandleType.tp_methods = TaskHandle_methods;
218 ✔
272
    TaskHandleType.tp_getset = TaskHandle_getsetters;
218 ✔
273
    if (register_type(m, &TaskHandleType, "TaskHandle") < 0) return -1;
218 !
274
    return PyModule_AddFunctions(
218 ✔
275
        m, dftracer::utils::python::runtime::task_handle_module_methods);
218 ✔
276
}
109 ✔
277

278
}  // namespace dftracer::utils::python::runtime
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc