Skip to content

fix(profiling): don't disable an alt stack replaced after we installed it - #19026

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

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

Conversation

@taegyunkim

@taegyunkim taegyunkim commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

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

taegyunkim and others added 5 commits July 13, 2026 17:34
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>
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>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@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.kafka -> ddtrace.internal.datastreams
ddtrace.internal.core -> ddtrace._trace.span -> ddtrace.internal.core
ddtrace.internal.datastreams -> ddtrace.internal.datastreams.google_cloud_pubsub -> ddtrace.internal.datastreams
ddtrace.internal.datastreams -> ddtrace.internal.datastreams.aiokafka -> ddtrace.internal.datastreams
ddtrace.internal.datastreams -> ddtrace.internal.datastreams.botocore -> 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

@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/test_alt_stack_ownership.cpp  @DataDog/profiling-python
releasenotes/notes/profiling-altstack-replaced-teardown-d5a92b5574e47710.yaml  @DataDog/apm-python

@taegyunkim taegyunkim added the Profiling Continous Profling label Jul 13, 2026
@datadog-official

datadog-official Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

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

taegyunkim and others added 2 commits July 13, 2026 21:02
…d it

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 we
installed ours, teardown would strip that replacement, degrading the other
owner's fault handling. Only disable the alt stack when it is still ours
(cur.ss_sp == mem); free our own mapping regardless.

Adds a regression test (DestructorDoesNotDisableReplacedAltStack) that fails
without this guard.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@taegyunkim
taegyunkim force-pushed the taegyunkim/prof-14213-safe-copy-altstack-teardown-guard branch from 58623e5 to a536d62 Compare July 13, 2026 21:03
@taegyunkim
taegyunkim marked this pull request as ready for review July 14, 2026 14:22
@taegyunkim
taegyunkim requested review from a team as code owners July 14, 2026 14:22
@taegyunkim
taegyunkim requested review from florentinl and vlad-scherbich and removed request for a team July 14, 2026 14:22
@gyuheon0h

Copy link
Copy Markdown
Contributor

Taking a look 👀

gh-worker-dd-mergequeue-cf854d Bot 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>
Base automatically changed from taegyunkim/prof-14213-safe-copy-altstack-ownership to main July 14, 2026 16:10
@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot merged commit 6f3d5f2 into main Jul 14, 2026
469 checks passed
@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot deleted the taegyunkim/prof-14213-safe-copy-altstack-teardown-guard branch July 14, 2026 18:59
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>
@KowalskiThomas

Copy link
Copy Markdown
Collaborator

Nice catch 💯

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.

3 participants