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): reset all profiling c++ mutexes on fork #11768

Merged
merged 12 commits into from
Dec 19, 2024

Conversation

sanchda
Copy link
Contributor

@sanchda sanchda commented Dec 18, 2024

I'm not sure why it took so long to surface this defect, but it turns out that stack v2 can deadlock applications because not all mutices are reset.

The repro in #11762 appears to be pretty durable. I need to investigate it a bit more in order to distill it down into a native stress test we can use moving forward. In practice, this patch suppresses the noted behavior in the repro.

Checklist

  • PR author has checked that all the criteria below are met
  • The PR description includes an overview of the change
  • The PR description articulates the motivation for the change
  • The change includes tests OR the PR description describes a testing strategy
  • The PR description notes risks associated with the change, if any
  • Newly-added code is easy to change
  • The change follows the library release note guidelines
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

  • Reviewer has checked that all the criteria below are met
  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Newly-added code is easy to change
  • Release note makes sense to a user of the library
  • If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment
  • Backport labels are set in a manner that is consistent with the release branch maintenance policy

@sanchda sanchda requested a review from a team as a code owner December 18, 2024 03:25
Copy link
Contributor

github-actions bot commented Dec 18, 2024

CODEOWNERS have been resolved as:

releasenotes/notes/fix-profiling-native-mutices-62440b5a3d9d6c4b.yaml   @DataDog/apm-python
ddtrace/internal/datadog/profiling/dd_wrapper/include/uploader_builder.hpp  @DataDog/profiling-python
ddtrace/internal/datadog/profiling/dd_wrapper/src/code_provenance.cpp   @DataDog/profiling-python
ddtrace/internal/datadog/profiling/dd_wrapper/src/ddup_interface.cpp    @DataDog/profiling-python
ddtrace/internal/datadog/profiling/dd_wrapper/src/profile.cpp           @DataDog/profiling-python
ddtrace/internal/datadog/profiling/dd_wrapper/src/sample.cpp            @DataDog/profiling-python
ddtrace/internal/datadog/profiling/dd_wrapper/src/uploader.cpp          @DataDog/profiling-python
ddtrace/internal/datadog/profiling/dd_wrapper/src/uploader_builder.cpp  @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack_v2/src/sampler.cpp             @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack_v2/src/thread_span_links.cpp   @DataDog/profiling-python

@pr-commenter
Copy link

pr-commenter bot commented Dec 18, 2024

Benchmarks

Benchmark execution time: 2024-12-19 18:45:43

Comparing candidate commit 695834c in PR branch sanchda/reset_all_mutex with baseline commit e9dbe4f in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 394 metrics, 2 unstable metrics.

@sanchda sanchda changed the title fix(profiling): Reset all native mutices on fork fix(profiling): Reset all native mutexes on fork Dec 18, 2024
@sanchda sanchda changed the title fix(profiling): Reset all native mutexes on fork fix(profiling): reset all native mutexes on fork Dec 18, 2024
@sanchda sanchda changed the title fix(profiling): reset all native mutexes on fork fix(profiling): reset all profiling c++ mutexes on fork Dec 18, 2024
@sanchda sanchda added the Profiling Continous Profling label Dec 18, 2024
@danielsn
Copy link
Contributor

For future, we should upstream these changes to echion

@romainkomorndatadog romainkomorndatadog requested review from P403n1x87 and removed request for romainkomorndatadog December 19, 2024 09:04
Refactor the postfork handler in the child to use placement-new in order
to leak the old mutex and create a new one.  We can't destroy, unlock,
reinitialize, or reset because all of those operations are UB in the
standard.  We could reimplement the locking mechanisms on top of
pthreads, but then in our port to Windows/macos, we'd need to replace
those primitives with equivalents on the respective platforms.

At the same time, the typical Python application which calls `fork()`
only does so one level deep--in other words, it is common for a
coordinator process to fork one level of children, but it is
unbelievably uncommon for those children to have long-lived forks of
their own (`popen()` sure, but absolutely no workers spawning workers).
@sanchda sanchda merged commit d855c4a into main Dec 19, 2024
257 checks passed
@sanchda sanchda deleted the sanchda/reset_all_mutex branch December 19, 2024 19:43
Copy link
Contributor

The backport to 2.16 failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-2.16 2.16
# Navigate to the new working tree
cd .worktrees/backport-2.16
# Create a new branch
git switch --create backport-11768-to-2.16
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 d855c4a28824c15fd3afdbbe89315808efafdf07
# Push it to GitHub
git push --set-upstream origin backport-11768-to-2.16
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-2.16

Then, create a pull request where the base branch is 2.16 and the compare/head branch is backport-11768-to-2.16.

Copy link
Contributor

The backport to 2.17 failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-2.17 2.17
# Navigate to the new working tree
cd .worktrees/backport-2.17
# Create a new branch
git switch --create backport-11768-to-2.17
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 d855c4a28824c15fd3afdbbe89315808efafdf07
# Push it to GitHub
git push --set-upstream origin backport-11768-to-2.17
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-2.17

Then, create a pull request where the base branch is 2.17 and the compare/head branch is backport-11768-to-2.17.

Copy link
Contributor

The backport to 2.18 failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-2.18 2.18
# Navigate to the new working tree
cd .worktrees/backport-2.18
# Create a new branch
git switch --create backport-11768-to-2.18
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 d855c4a28824c15fd3afdbbe89315808efafdf07
# Push it to GitHub
git push --set-upstream origin backport-11768-to-2.18
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-2.18

Then, create a pull request where the base branch is 2.18 and the compare/head branch is backport-11768-to-2.18.

sanchda added a commit that referenced this pull request Dec 19, 2024
I'm not sure why it took so long to surface this defect, but it turns
out that stack v2 can deadlock applications because not all mutices are
reset.

The repro in #11762 appears to be pretty durable. I need to investigate
it a bit more in order to distill it down into a native stress test we
can use moving forward. In practice, this patch suppresses the noted
behavior in the repro.

- [X] PR author has checked that all the criteria below are met
- The PR description includes an overview of the change
- The PR description articulates the motivation for the change
- The change includes tests OR the PR description describes a testing
strategy
- The PR description notes risks associated with the change, if any
- Newly-added code is easy to change
- The change follows the [library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
- The change includes or references documentation updates if necessary
- Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))

- [x] Reviewer has checked that all the criteria below are met
- Title is accurate
- All changes are related to the pull request's stated goal
- Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- Testing strategy adequately addresses listed risks
- Newly-added code is easy to change
- Release note makes sense to a user of the library
- If necessary, author has acknowledged and discussed the performance
implications of this PR as reported in the benchmarks PR comment
- Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)

---------

Co-authored-by: Taegyun Kim <[email protected]>
(cherry picked from commit d855c4a)
sanchda added a commit that referenced this pull request Dec 19, 2024
…8] (#11768) (#11808)

## Checklist
- [X] PR author has checked that all the criteria below are met
- The PR description includes an overview of the change
- The PR description articulates the motivation for the change
- The change includes tests OR the PR description describes a testing
strategy
- The PR description notes risks associated with the change, if any
- Newly-added code is easy to change
- The change follows the [library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
- The change includes or references documentation updates if necessary
- Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))

## Reviewer Checklist
- [X] Reviewer has checked that all the criteria below are met 
- Title is accurate
- All changes are related to the pull request's stated goal
- Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- Testing strategy adequately addresses listed risks
- Newly-added code is easy to change
- Release note makes sense to a user of the library
- If necessary, author has acknowledged and discussed the performance
implications of this PR as reported in the benchmarks PR comment
- Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)

---------

Co-authored-by: Taegyun Kim <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants