Skip to content

Commit fe38106

Browse files
authored
chore(profiling): increase ref before stealing (#15031)
## Description Noticed this while looking into crashes related to the memory profiler. [See notebook here](https://app.datadoghq.com/notebook/13359436/memory-profiler-crashes). We are not sure whether this would be root cause of such crashes, but still think it's 'logically' correct to increment the reference before calling `PyTuple_SET_ITEM()`. When you call `PyTuple_SET_ITEM(p, pos, obj);`, then the ownership of the object `o` is transferred to the tuple. - https://docs.python.org/3/c-api/tuple.html#c.PyTuple_SET_ITEM - https://docs.python.org/3/c-api/intro.html#reference-count-details > Stealing a reference means that when you pass a reference to a function, that function assumes that it now owns that reference, and you are not responsible for it any longer > When you want to keep using an object although the reference to it will be stolen, use [Py_INCREF()](https://docs.python.org/3/c-api/refcounting.html#c.Py_INCREF) to grab another reference before calling the reference-stealing function. <!-- Provide an overview of the change and motivation for the change --> ## Testing <!-- Describe your testing strategy or note what tests are included --> ## Risks <!-- Note any risks associated with this change, or "None" if no risks --> ## Additional Notes <!-- Any other information that would be helpful for reviewers -->
1 parent 123e5bd commit fe38106

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

ddtrace/profiling/collector/_memalloc_tb.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,14 +301,14 @@ traceback_to_tuple(traceback_t* tb)
301301

302302
frame_t* frame = &tb->frames[nframe];
303303

304-
PyTuple_SET_ITEM(frame_tuple, 0, frame->filename);
305304
Py_INCREF(frame->filename);
305+
PyTuple_SET_ITEM(frame_tuple, 0, frame->filename);
306306
PyTuple_SET_ITEM(frame_tuple, 1, PyLong_FromUnsignedLong(frame->lineno));
307-
PyTuple_SET_ITEM(frame_tuple, 2, frame->name);
308307
Py_INCREF(frame->name);
308+
PyTuple_SET_ITEM(frame_tuple, 2, frame->name);
309309
/* Class name */
310-
PyTuple_SET_ITEM(frame_tuple, 3, empty_string);
311310
Py_INCREF(empty_string);
311+
PyTuple_SET_ITEM(frame_tuple, 3, empty_string);
312312

313313
// Try to set the class. If we cannot (e.g., if the sofile is reloaded
314314
// without module initialization), then this will result in an error if

0 commit comments

Comments
 (0)