← Back to Pull requests

fix(profiling): memalloc obj domain module finalizer #20608

Closed
unknown closed this pull request without merging on Oct 08, 2026 16:38 UTC
View on GitHub
unknown unknown commented on Sep 28, 2026 17:23 UTC
Author

Description


While looking at crash reports, I saw a series of crash reports that look like such: https://app.datadoghq.com/error-tracking/issue/4fe0aa66-5607-11f1-b89e-da7ad0900002?query=version%3A4.15.2&et-issue__error-sample-viz=sample&refresh_mode=sliding&from_ts=1789393982142&to_ts=1790603582142&live=true

The crashes are in the memalloc profiler's allocator hook lifecycle caused SIGSEGV crashes in gunicorn/gevent workers (observed as SI_KERNEL at alloc.malloc(alloc.ctx, ...) in memalloc_alloc).

From looking at the code, I saw that there was no module finalizer so hooks outlive the interpreter

PyModuleDef had no m_free callback. When a process such as a gevent worker exits without the Python-level collector calling stop() explicitly, the OBJ and MEM hooks remain installed through Py_Finalize(). CPython then tears down its internal allocator state, and any allocation that reaches the hook calls alloc.malloc(alloc.ctx, ...) with a stale context which will fault.

I added g_saved_alloc_pub.store(nullptr) in stop() to match the MEM domain, and register memalloc_module_free as m_free in PyModuleDef to always uninstall hooks during module deallocation.

Testing

Its hard to write a deterministic test for this as waiting on GC to do what it does with module unload is not deterministic.I added a test-only helper _test_invoke_module_free that exposes the m_free path directly to Python and added this regression test: test_m_free_uninstalls_hooks_deterministic

Not the biggest fan of writing API just for testing but 🤷

Risks


Low IMO

PyMem_SetAllocator is a plain struct copy with no heap allocation, safe to call at any shutdown stage. memalloc_heap_tracker_deinit_no_cpython frees C++ objects without calling any CPython API.

Additional Notes

I used AI to write the test following my directions.

0x7f5d9485469c __pthread_kill_implementation (nptl/nptl/pthread_kill.c:44)
0x7f5d947ffd70 __restore_rt
0x7f5d94b58c1c
memalloc_alloc (/go/src/github.com/DataDog/apm-reliability/dd-trace-py/ddtrace/profiling/collector/_memalloc.cpp:117)
0x7f5d9030a535 memalloc_malloc(void*, unsigned long) (/go/src/github.com/DataDog/apm-reliability/dd-trace-py/ddtrace/profiling/collector/_memalloc.cpp:130)
0x7f5d94b5a644 PyUnicode_New
0x7f5d94c44b2e
0x7f5d94401469
0x7f5d943ff160
0x7f5d943ff52e
0x7f5d943ff160
0x7f5d943fe442
0x7f5d94b6a793 _PyObject_MakeTpCall
0x7f5d94b7311b _PyEval_EvalFrameDefault
0x7f5d94b6f63f
0x7f5d5769a848 __pyx_f_7asyncpg_8protocol_8protocol_5Codec_decode_in_python (/project/asyncpg/protocol/protocol.c:14555)
__pyx_f_7asyncpg_8protocol_8protocol_5Codec_decode (/project/asyncpg/protocol/protocol.c:14615)
0x7f5d576a59df __pyx_f_7asyncpg_8protocol_8protocol_22PreparedStatementState__decode_row (/project/asyncpg/protocol/protocol.c:58903)
0x7f5d576a6690 __pyx_f_7asyncpg_8protocol_8protocol_12BaseProtocol__decode_row (/project/asyncpg/protocol/protocol.c:77580)
0x7f5d5767d95c __pyx_f_7asyncpg_8protocol_8protocol_12CoreProtocol__parse_data_msgs (/project/asyncpg/protocol/protocol.c:46130)
0x7f5d5767c5e2 __pyx_f_7asyncpg_8protocol_8protocol_12CoreProtocol__process__bind_execute (/project/asyncpg/protocol/protocol.c:43641)
0x7f5d576aef4f __pyx_f_7asyncpg_8protocol_8protocol_12CoreProtocol__read_server_messages (/project/asyncpg/protocol/protocol.c:41942)
__pyx_pf_7asyncpg_8protocol_8protocol_12BaseProtocol_64data_received (/project/asyncpg/protocol/protocol.c:79532)
0x7f5d5768910b __pyx_pw_7asyncpg_8protocol_8protocol_12BaseProtocol_65data_received (/project/asyncpg/protocol/protocol.c:79495)
0x7f5d94b6efde PyObject_VectorcallMethod
Py_DECREF (/opt/_internal/cpython-3.11.13/include/python3.11/object.h:537)
Py_XDECREF (/opt/_internal/cpython-3.11.13/include/python3.11/object.h:602)
0x7f5d6753c4b6 __pyx_f_6uvloop_4loop_11SSLProtocol__do_read__copied (/project/uvloop/loop.c:152450)
0x7f5d6751ee2e __pyx_f_6uvloop_4loop_11SSLProtocol__do_read (/project/uvloop/loop.c:151167)
__pyx_pf_6uvloop_4loop_11SSLProtocol_14buffer_updated (/project/uvloop/loop.c:145987)
0x7f5d674e1465 __pyx_pw_6uvloop_4loop_11SSLProtocol_15buffer_updated (/project/uvloop/loop.c:145873)
0x7f5d94ba7c0a
0x7f5d94b3230c
0x7f5d94b3238d
0x7f5d94ba312f
0x7f5d94b6efde PyObject_VectorcallMethod
Py_DECREF (/opt/_internal/cpython-3.11.13/include/python3.11/object.h:537)
Py_XDECREF (/opt/_internal/cpython-3.11.13/include/python3.11/object.h:602)
0x7f5d674c8f83 __pyx_f_6uvloop_4loop_run_in_context1 (/project/uvloop/loop.c:13464)
0x7f5d67528450 __pyx_f_6uvloop_4loop___uv_stream_buffered_on_read (/project/uvloop/loop.c:108025)
0x7f5d675912ea uv__read (/project/build/libuv-x86_64/src/unix/stream.c:1146)
0x7f5d67591e34 uv__stream_io (/project/build/libuv-x86_64/src/unix/stream.c:1205)
0x7f5d6759868b uv__io_poll (/project/build/libuv-x86_64/src/unix/linux.c:1528)
0x7f5d6758ab36 uv_run (/project/build/libuv-x86_64/src/unix/core.c:452)
0x7f5d674dd3a0 __pyx_f_6uvloop_4loop_4Loop__Loop__run (/project/uvloop/loop.c:18471)
0x7f5d6751b12d __pyx_f_6uvloop_4loop_4Loop__run (/project/uvloop/loop.c:18876)
__pyx_pf_6uvloop_4loop_4Loop_24run_forever (/project/uvloop/loop.c:31528)
0x7f5d67516e85 __pyx_pw_6uvloop_4loop_4Loop_25run_forever (/project/uvloop/loop.c:31331)
0x7f5d94b6efde PyObject_VectorcallMethod
Py_DECREF (/opt/_internal/cpython-3.11.13/include/python3.11/object.h:537)
Py_XDECREF (/opt/_internal/cpython-3.11.13/include/python3.11/object.h:602)
0x7f5d67519844 __pyx_pf_6uvloop_4loop_4Loop_44run_until_complete (/project/uvloop/loop.c:33769)
0x7f5d6751a8b8 __pyx_pw_6uvloop_4loop_4Loop_45run_until_complete (/project/uvloop/loop.c:33318)
0x7f5d94b7ea97 PyObject_Vectorcall
0x7f5d94b7311b _PyEval_EvalFrameDefault
0x7f5d94b6f63f
0x7f5d94b97dcb
0x7f5d94b76f97 _PyEval_EvalFrameDefault
0x7f5d94b6f63f
0x7f5d94bf6ccc PyEval_EvalCode
0x7f5d94c1319d
0x7f5d94c0f77a
0x7f5d94c0573d PyRun_StringFlags
0x7f5d94c05640 PyRun_SimpleStringFlags
0x7f5d94c1e90c Py_RunMain
0x7f5d94be6b5b Py_BytesMain
0x7f5d947e9ca8 __libc_start_call_main (sysdeps/nptl/libc_start_call_main.h:74)
call_init (csu/libc-start.c:128)
0x7f5d947e9d65 __libc_start_main_alias_2 (csu/libc-start.c:347)
0x561fd04b0071 _start

Diff: 650 lines (+487 / -163) across 8 files Reviewer: gyuheon0h Review duration: 14355m
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
0.52
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.388
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #20608"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 52% (MEDIUM) · Higher = larger blast radius
52%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 20.5% · Safe to merge
20.5%