Skip to content

python: properly propagate shutdown signal in weblogs#6374

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 3 commits into
mainfrom
florentin.labelle/properly-forward-signals
Feb 26, 2026
Merged

python: properly propagate shutdown signal in weblogs#6374
gh-worker-dd-mergequeue-cf854d[bot] merged 3 commits into
mainfrom
florentin.labelle/properly-forward-signals

Conversation

@florentinl
Copy link
Copy Markdown
Contributor

@florentinl florentinl commented Feb 24, 2026

Motivation

Re-enable this test: tests/test_resource_renaming.py::Test_Resource_Renaming_Stats_Aggregation_Keys::test_stats_aggregation_with_method_and_endpoint.

The main problem is that the weblog never receive the SIGTERM signal from Docker and get killed right away without having the possibility to flush their buffers on shutdown.

This PR ensures that the SIGTERM signal properly propagates to the python interpreters running the app and and can be caught by dd-trace-py.

Changes

  • Use exec to replace the shell process with the actual ddtrace-run command.
  • for uwsgi, updating + adding a bit more configuration was required to get the signal to properly propagate to the python interpreter
  • re-enable the test

Workflow

  1. ⚠️ Create your PR as draft ⚠️
  2. Work on you PR until the CI passes
  3. Mark it as ready for review
    • Test logic is modified? -> Get a review from RFC owner.
    • Framework is modified, or non obvious usage of it -> get a review from R&P team

🚀 Once your PR is reviewed and the CI green, you can merge it!

🛟 #apm-shared-testing 🛟

Reviewer checklist

  • Anything but tests/ or manifests/ is modified ? I have the approval from R&P team
  • A docker base image is modified?
    • the relevant build-XXX-image label is present
  • A scenario is added, removed or renamed?

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Feb 24, 2026

CODEOWNERS have been resolved as:

manifests/python.yml                                                    @DataDog/apm-python @DataDog/asm-python
tests/test_resource_renaming.py                                         @DataDog/system-tests-core
utils/build/docker/python/django/app.sh                                 @DataDog/apm-python @DataDog/asm-python @DataDog/system-tests-core
utils/build/docker/python/fastapi/app.sh                                @DataDog/apm-python @DataDog/asm-python @DataDog/system-tests-core
utils/build/docker/python/flask/app.sh                                  @DataDog/apm-python @DataDog/asm-python @DataDog/system-tests-core
utils/build/docker/python/flask/requirements-uwsgi-poc.txt              @DataDog/apm-python @DataDog/asm-python @DataDog/system-tests-core
utils/build/docker/python/flask/uwsgi.ini                               @DataDog/apm-python @DataDog/asm-python @DataDog/system-tests-core
utils/build/docker/python/flask/uwsgi_app.sh                            @DataDog/apm-python @DataDog/asm-python @DataDog/system-tests-core
utils/build/docker/python/tornado/app.sh                                @DataDog/apm-python @DataDog/asm-python @DataDog/system-tests-core

@datadog-datadog-prod-us1-2
Copy link
Copy Markdown

datadog-datadog-prod-us1-2 Bot commented Feb 24, 2026

✅ Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

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

@florentinl florentinl force-pushed the florentin.labelle/properly-forward-signals branch 2 times, most recently from edc43ef to e599acc Compare February 24, 2026 16:58
@florentinl florentinl force-pushed the florentin.labelle/properly-forward-signals branch from 4b1e3f2 to aec0b37 Compare February 25, 2026 11:40
@florentinl florentinl force-pushed the florentin.labelle/properly-forward-signals branch from aec0b37 to a5517db Compare February 25, 2026 11:44
@florentinl florentinl force-pushed the florentin.labelle/properly-forward-signals branch from a5517db to 8e28b38 Compare February 25, 2026 12:33
@florentinl florentinl marked this pull request as ready for review February 25, 2026 13:23
@florentinl florentinl requested review from a team as code owners February 25, 2026 13:23
@florentinl florentinl requested review from VianneyRuhlmann, tabgok and wconti27 and removed request for a team February 25, 2026 13:23
@florentinl florentinl marked this pull request as draft February 25, 2026 15:47
@florentinl florentinl force-pushed the florentin.labelle/properly-forward-signals branch from 25b6b3d to c070b72 Compare February 25, 2026 15:49
@florentinl florentinl force-pushed the florentin.labelle/properly-forward-signals branch from c070b72 to fda6c57 Compare February 25, 2026 16:08
Copy link
Copy Markdown
Contributor

@VianneyRuhlmann VianneyRuhlmann left a comment

Choose a reason for hiding this comment

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

Nice, thx for fixing it

@florentinl florentinl marked this pull request as ready for review February 25, 2026 23:40
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c102ca18a4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread manifests/python.yml
@gh-worker-dd-mergequeue-cf854d gh-worker-dd-mergequeue-cf854d Bot merged commit d0f8b2d into main Feb 26, 2026
2615 of 2681 checks passed
@gh-worker-dd-mergequeue-cf854d gh-worker-dd-mergequeue-cf854d Bot deleted the florentin.labelle/properly-forward-signals branch February 26, 2026 09:28

if [[ $(python -c 'import sys; print(sys.version_info >= (3,12))') == "True" ]]; then
ddtrace-run gunicorn -w 1 -b 0.0.0.0:7777 --header-map dangerous --access-logfile - django_app.wsgi -k gevent
exec ddtrace-run gunicorn -w 1 -b 0.0.0.0:7777 --header-map dangerous --access-logfile - django_app.wsgi -k gevent
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

For my knowledge, what are the diff between using exec and not, and why it makes the app more stable ?

Copy link
Copy Markdown
Contributor Author

@florentinl florentinl Feb 26, 2026

Choose a reason for hiding this comment

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

In the previous setup: the main process (pid 1) was bash (defined in the ENTRYPOINT of the upstream python images) and at startup bash runs our app.sh scripts. When docker wants to stop the container, it sends a SIGTERM (graceful shutdown) to the main process (bash) that does not forward signals to child processes and the app never has the occasion to run the flushing logic on graceful shutdown. docker then sends after a grace period a SIGKILL that kills the app at the kernel level.

When using exec, the launched process replaces the current process so ddtrace-run becomes the main process (pid 1) and docker sends its SIGTERM directly to ddtrace-run and we have the possibility to properly flush everything on shutdown.

If the flushing logic on shutdown works correctly (and it seems to be the case), that would mean that we can now set the library interface timeout back to 0 (from 5s currently)

Copy link
Copy Markdown
Contributor Author

@florentinl florentinl Feb 26, 2026

Choose a reason for hiding this comment

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

PHP images use dumb-init for the same purpose if I understand correctly, this is just a leaner version for when you have a single process

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you very much !

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