Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(profiling): fix SystemError when collecting memory profiler events #12075

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions ddtrace/profiling/collector/_memalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,20 +394,18 @@ iterevents_new(PyTypeObject* type, PyObject* Py_UNUSED(args), PyObject* Py_UNUSE
}

IterEventsState* iestate = (IterEventsState*)type->tp_alloc(type, 0);
if (!iestate)
if (!iestate) {
PyErr_SetString(PyExc_RuntimeError, "failed to allocate IterEventsState");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the exception handled somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's handled here:

def collect(self):
# TODO: The event timestamp is slightly off since it's going to be the time we copy the data from the
# _memalloc buffer to our Recorder. This is fine for now, but we might want to store the nanoseconds
# timestamp in C and then return it via iter_events.
try:
events_iter, count, alloc_count = _memalloc.iter_events()
except RuntimeError:
# DEV: This can happen if either _memalloc has not been started or has been stopped.
LOG.debug("Unable to collect memory events from process %d", os.getpid(), exc_info=True)
return tuple()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try:
events_iter, count, alloc_count = _memalloc.iter_events()
except RuntimeError:
# DEV: This can happen if either _memalloc has not been started or has been stopped.
LOG.debug("Unable to collect memory events from process %d", os.getpid(), exc_info=True)
return tuple()

Yes, we do handle RuntimeError here.

return NULL;
}

memalloc_assert_gil();

/* reset the current traceback list */
if (memlock_trylock(&g_memalloc_lock)) {
iestate->alloc_tracker = global_alloc_tracker;
global_alloc_tracker = alloc_tracker_new();
memlock_unlock(&g_memalloc_lock);
} else {
Py_TYPE(iestate)->tp_free(iestate);
return NULL;
}
memlock_lock(&g_memalloc_lock);
iestate->alloc_tracker = global_alloc_tracker;
global_alloc_tracker = alloc_tracker_new();
memlock_unlock(&g_memalloc_lock);
iestate->seq_index = 0;

PyObject* iter_and_count = PyTuple_New(3);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
profiling: fix SystemError from the memory profiler returning NULL when collecting events
Loading