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(taskworker) remove signal handler safely #88799

Merged
merged 2 commits into from
Apr 4, 2025
Merged

Conversation

markstory
Copy link
Member

There are some tests for child_worker that are not in child processes, and we're currently leaking signal handlers. Use a context manager to add/cancel the signal handler.

Fixes #88742

There are some tests for `child_worker` that are not in child processes,
and we're currently leaking signal handlers. Use a context manager to
add/cancel the signal handler.

Fixes #88742
@markstory markstory requested a review from a team as a code owner April 4, 2025 15:01
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Apr 4, 2025
@@ -48,7 +52,22 @@ class ProcessingResult:
status: TaskActivationStatus.ValueType


AT_MOST_ONCE_TIMEOUT = 60 * 60 * 24 # 1 day
@contextlib.contextmanager
def timeout_alarm(seconds: int, handler: Callable[[int, Any], None]):
Copy link
Member

Choose a reason for hiding this comment

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

since the actual handler doesn't care about either of its arguments it can be Callable[[object, object], None] and this should -> Generator[None]

next_state = TASK_ACTIVATION_STATUS_FAILURE
execution_start_time = time.time()
Copy link
Member

Choose a reason for hiding this comment

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

time.time should really never be used for timing as it is subjected to smear / etc.

you probably want time.monotonic() https://youtu.be/hb-12mgQrzM

Copy link
Member Author

Choose a reason for hiding this comment

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

While I can switch a few usage of time.time(), I don't think all of them can be changed. We need to do some time differences against the message produce time which is a protobuf timestamp. These won't be precise measurements as clock times can vary from one system to another, but there aren't many other portable time representations.

Copy link
Member

@asottile-sentry asottile-sentry left a comment

Choose a reason for hiding this comment

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

Copy link

codecov bot commented Apr 4, 2025

Codecov Report

Attention: Patch coverage is 94.73684% with 1 line in your changes missing coverage. Please review.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/sentry/taskworker/worker.py 94.73% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master   #88799       +/-   ##
===========================================
+ Coverage   32.97%   87.73%   +54.75%     
===========================================
  Files        8505    10066     +1561     
  Lines      475082   569399    +94317     
  Branches    22355    22355               
===========================================
+ Hits       156646   499536   +342890     
+ Misses     318037    69464   -248573     
  Partials      399      399               

@markstory markstory merged commit 3badc19 into master Apr 4, 2025
49 checks passed
@markstory markstory deleted the fix-signal-leak branch April 4, 2025 17:20
andrewshie-sentry pushed a commit that referenced this pull request Apr 8, 2025
There are some tests for `child_worker` that are not in child processes,
and we're currently leaking signal handlers. Use a context manager to
add/cancel the signal handler.

Fixes #88742
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Scope: Backend Automatically applied to PRs that change backend components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix signal leak in taskworker child_worker
2 participants