Skip to content

fix(profiling): track signal altstack ownership - #19018

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 5 commits into
mainfrom
taegyunkim/prof-14213-safe-copy-altstack-ownership
Jul 14, 2026
Merged

gh-worker-dd-mergequeue-cf854d[bot] merged 5 commits into
mainfrom
taegyunkim/prof-14213-safe-copy-altstack-ownership

Conversation

@taegyunkim

@taegyunkim taegyunkim commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

The profiling safe-copy helper uses a per-thread alternate signal stack for its SIGSEGV and SIGBUS handlers. When an alternate stack was already installed by the application, CPython's faulthandler, or crashtracker, ThreadAltStack reused it but still disabled it during thread-local teardown. That could leave the original owner without its alternate stack and weaken its fault handling.

Track whether ThreadAltStack allocated the mapping itself. Teardown now disables and unmaps only profiler-owned stacks, while adopted stacks remain installed for their original owner. The installation failure path also unmaps a profiler allocation when sigaltstack() fails.

A release note documents the fault-handling fix.

Testing

  • Passed locally: ddtrace/internal/datadog/profiling/build_standalone.sh -- RelWithDebInfo stack_test
    • 4/4 native CTests passed
    • verifies that an adopted alternate stack remains installed after ThreadAltStack teardown
    • verifies that a profiler-owned alternate stack is disabled during teardown
  • scripts/lint cformat

Risks

Low. The change narrows teardown to resources owned by the profiler and does not alter public APIs or configuration. Profiler-owned alternate stacks retain their existing cleanup behavior.

Additional Notes

Stacked out from the CPU timer profiler work, #18724, to keep this native safety fix independently reviewable.

Follow-up PR #19026 handles the complementary case where another component replaces a profiler-owned alternate stack after the profiler installs it.

PROF-14213

@taegyunkim taegyunkim added the changelog/no-changelog A changelog entry is not required for this PR. label Jul 13, 2026
@datadog-datadog-prod-us1-2

datadog-datadog-prod-us1-2 Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Pipelines  Tests

✨ Fix all issues with BitsAI

⚠️ Warnings

🚦 9 Pipeline jobs failed

DataDog/apm-reliability/dd-trace-py | build linux serverless: [amd64, cp315-cp315, v113741238-d2b8243-manylinux2014_x86_64, 1]   View in Datadog   GitLab

DataDog/apm-reliability/dd-trace-py | build linux serverless: [amd64, cp315-cp315, v113741491-d2b8243-musllinux_1_2_x86_64, 1]   View in Datadog   GitLab

DataDog/apm-reliability/dd-trace-py | build linux serverless: [arm64, cp315-cp315, v113741357-d2b8243-manylinux2014_aarch64, 1]   View in Datadog   GitLab

View all 9 failed jobs.

ℹ️ Info

No other issues found (see more)

🧪 All tests passed
❄️ No new flaky tests detected

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: e7809e7 | Docs | Datadog PR Page | Give us feedback!

@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Jul 13, 2026 •

Copy link
Copy Markdown

Codeowners resolved as

ddtrace/internal/datadog/profiling/stack/echion/echion/danger.h         @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/test/CMakeLists.txt            @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/test/test_alt_stack_ownership.cpp  @DataDog/profiling-python
releasenotes/notes/profiling-altstack-ownership-4e353919a8480856.yaml   @DataDog/apm-python

@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Jul 13, 2026 •

Copy link
Copy Markdown

Circular import analysis

⚠️ Existing circular imports

There are 2524 circular imports that already exist on the base branch and have not been changed by this PR.

Show existing cycles (showing 5 of 2524 shortest)
ddtrace.internal.datastreams -> ddtrace.internal.datastreams.botocore -> ddtrace.internal.datastreams
ddtrace.internal.datastreams -> ddtrace.internal.datastreams.kombu -> ddtrace.internal.datastreams
ddtrace.internal.core -> ddtrace._trace.span -> ddtrace.internal.core
ddtrace.internal.datastreams -> ddtrace.internal.datastreams.aiokafka -> ddtrace.internal.datastreams
ddtrace.internal.datastreams -> ddtrace.internal.datastreams.google_cloud_pubsub -> ddtrace.internal.datastreams

To see all cycles, download the cycles-base.json and cycles-pr.json artifacts from this CI job and run:

uv run --script scripts/import-analysis/cycles.py compare cycles-base.json cycles-pr.json

@taegyunkim taegyunkim changed the title fix(profiling): preserve pre-existing signal altstacks chore(profiling): track signal altstack ownership Jul 13, 2026
@taegyunkim
taegyunkim force-pushed the taegyunkim/prof-14213-safe-copy-altstack-ownership branch from 6c03d6b to cafd079 Compare July 13, 2026 17:32
@taegyunkim
taegyunkim force-pushed the taegyunkim/prof-14213-safe-copy-altstack-ownership branch from cafd079 to 55a7cba Compare July 13, 2026 17:34
@taegyunkim
taegyunkim force-pushed the taegyunkim/prof-14213-safe-copy-altstack-ownership branch from ace0881 to 8f952d9 Compare July 13, 2026 19:13
@taegyunkim
taegyunkim marked this pull request as ready for review July 13, 2026 19:18
@taegyunkim
taegyunkim requested a review from a team as a code owner July 13, 2026 19:18
Explain why ThreadAltStack adopts a pre-existing alternate signal stack
instead of replacing it: the application, CPython's faulthandler, or
libdatadog's crashtracker (which by default creates and owns a larger alt
stack) may have installed it, and the destructor must never disable or free
an alt stack this object did not allocate.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@taegyunkim
taegyunkim force-pushed the taegyunkim/prof-14213-safe-copy-altstack-ownership branch from 8f952d9 to a84b45a Compare July 13, 2026 20:16
Add a native gtest for ThreadAltStack: an adopted (pre-existing) alternate
signal stack must not be disabled on thread-local cleanup, while a stack this
helper allocated is disabled and freed. The adopted-stack case fails without
the owns_mapping guard, so it locks in this PR's behavior.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@taegyunkim taegyunkim changed the title chore(profiling): track signal altstack ownership fix(profiling): track signal altstack ownership Jul 13, 2026
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@taegyunkim
taegyunkim requested a review from a team as a code owner July 13, 2026 20:31
@taegyunkim
taegyunkim requested a review from Kyle-Verhoog July 13, 2026 20:31
@taegyunkim taegyunkim added Profiling Continous Profling and removed changelog/no-changelog A changelog entry is not required for this PR. labels Jul 13, 2026
@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot merged commit 9e268d1 into main Jul 14, 2026
703 of 708 checks passed
@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot deleted the taegyunkim/prof-14213-safe-copy-altstack-ownership branch July 14, 2026 16:10
gh-worker-dd-mergequeue-cf854d Bot pushed a commit that referenced this pull request Jul 14, 2026
…d it (#19026)

## Description

Follow-up to #19018. `ThreadAltStack`'s destructor disabled whatever alternate signal stack was installed on the thread whenever it owned a mapping, without checking that the installed stack was still the one it installed. If another component (for example libdatadog crashtracker) replaced the thread's alt stack after installation, teardown would strip that replacement and weaken the other owner's fault handling. The destructor now disables the alt stack only when it is still ours (`cur.ss_sp == mem`) and frees our own mapping regardless.

## Testing

- Native gtest `ThreadAltStackOwnership.DestructorDoesNotDisableReplacedAltStack`, which fails without this guard (verified via `build_standalone.sh -- RelWithDebInfo stack_test`).

## Risks

Low. Narrows destructor cleanup to the alternate signal stack the profiler still owns.

## Additional Notes

Stacked on #19018 — base branch `taegyunkim/prof-14213-safe-copy-altstack-ownership`.

[PROF-14213](https://datadoghq.atlassian.net/browse/PROF-14213?atlOrigin=eyJpIjoiY2NiZjFmMTg3MjZiNDBlOGFmMDFjYTBlOTBhZGZhNzIiLCJwIjoiaiJ9)

[PROF-14213]: https://datadoghq.atlassian.net/browse/PROF-14213?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

Co-authored-by: taegyun.kim <taegyun.kim@datadoghq.com>
vlad-scherbich pushed a commit that referenced this pull request Jul 14, 2026
## Description

The profiling safe-copy helper uses a per-thread alternate signal stack for its `SIGSEGV` and `SIGBUS` handlers. When an alternate stack was already installed by the application, CPython's faulthandler, or crashtracker, `ThreadAltStack` reused it but still disabled it during thread-local teardown. That could leave the original owner without its alternate stack and weaken its fault handling.

Track whether `ThreadAltStack` allocated the mapping itself. Teardown now disables and unmaps only profiler-owned stacks, while adopted stacks remain installed for their original owner. The installation failure path also unmaps a profiler allocation when `sigaltstack()` fails.

A release note documents the fault-handling fix.

## Testing

- Passed locally: `ddtrace/internal/datadog/profiling/build_standalone.sh -- RelWithDebInfo stack_test`
  - 4/4 native CTests passed
  - verifies that an adopted alternate stack remains installed after `ThreadAltStack` teardown
  - verifies that a profiler-owned alternate stack is disabled during teardown
- `scripts/lint cformat`

## Risks

Low. The change narrows teardown to resources owned by the profiler and does not alter public APIs or configuration. Profiler-owned alternate stacks retain their existing cleanup behavior.

## Additional Notes

Stacked out from the CPU timer profiler work, #18724, to keep this native safety fix independently reviewable.

Follow-up PR #19026 handles the complementary case where another component replaces a profiler-owned alternate stack after the profiler installs it.

[PROF-14213](https://datadoghq.atlassian.net/browse/PROF-14213)


[PROF-14213]: https://datadoghq.atlassian.net/browse/PROF-14213?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

Co-authored-by: taegyun.kim <taegyun.kim@datadoghq.com>
vlad-scherbich pushed a commit that referenced this pull request Jul 15, 2026
## Description

The profiling safe-copy helper uses a per-thread alternate signal stack for its `SIGSEGV` and `SIGBUS` handlers. When an alternate stack was already installed by the application, CPython's faulthandler, or crashtracker, `ThreadAltStack` reused it but still disabled it during thread-local teardown. That could leave the original owner without its alternate stack and weaken its fault handling.

Track whether `ThreadAltStack` allocated the mapping itself. Teardown now disables and unmaps only profiler-owned stacks, while adopted stacks remain installed for their original owner. The installation failure path also unmaps a profiler allocation when `sigaltstack()` fails.

A release note documents the fault-handling fix.

## Testing

- Passed locally: `ddtrace/internal/datadog/profiling/build_standalone.sh -- RelWithDebInfo stack_test`
  - 4/4 native CTests passed
  - verifies that an adopted alternate stack remains installed after `ThreadAltStack` teardown
  - verifies that a profiler-owned alternate stack is disabled during teardown
- `scripts/lint cformat`

## Risks

Low. The change narrows teardown to resources owned by the profiler and does not alter public APIs or configuration. Profiler-owned alternate stacks retain their existing cleanup behavior.

## Additional Notes

Stacked out from the CPU timer profiler work, #18724, to keep this native safety fix independently reviewable.

Follow-up PR #19026 handles the complementary case where another component replaces a profiler-owned alternate stack after the profiler installs it.

[PROF-14213](https://datadoghq.atlassian.net/browse/PROF-14213)


[PROF-14213]: https://datadoghq.atlassian.net/browse/PROF-14213?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

Co-authored-by: taegyun.kim <taegyun.kim@datadoghq.com>
vlad-scherbich pushed a commit that referenced this pull request Jul 15, 2026
…d it (#19026)

## Description

Follow-up to #19018. `ThreadAltStack`'s destructor disabled whatever alternate signal stack was installed on the thread whenever it owned a mapping, without checking that the installed stack was still the one it installed. If another component (for example libdatadog crashtracker) replaced the thread's alt stack after installation, teardown would strip that replacement and weaken the other owner's fault handling. The destructor now disables the alt stack only when it is still ours (`cur.ss_sp == mem`) and frees our own mapping regardless.

## Testing

- Native gtest `ThreadAltStackOwnership.DestructorDoesNotDisableReplacedAltStack`, which fails without this guard (verified via `build_standalone.sh -- RelWithDebInfo stack_test`).

## Risks

Low. Narrows destructor cleanup to the alternate signal stack the profiler still owns.

## Additional Notes

Stacked on #19018 — base branch `taegyunkim/prof-14213-safe-copy-altstack-ownership`.

[PROF-14213](https://datadoghq.atlassian.net/browse/PROF-14213?atlOrigin=eyJpIjoiY2NiZjFmMTg3MjZiNDBlOGFmMDFjYTBlOTBhZGZhNzIiLCJwIjoiaiJ9)

[PROF-14213]: https://datadoghq.atlassian.net/browse/PROF-14213?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

Co-authored-by: taegyun.kim <taegyun.kim@datadoghq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Profiling Continous Profling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants