Skip to content

Add async task background worker #4591

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

Open
wants to merge 15 commits into
base: srothh/worker-class-hierarchy
Choose a base branch
from

Conversation

srothh
Copy link
Member

@srothh srothh commented Jul 17, 2025

Add a new implementation of the transport background worker based on an async task. This worker mostly mirrors the same functionality as the thread-based worker, with the exception that it exposes a non-blocking async flush (which can be awaited from an async context). Furthermore, the worker itself is not thread-safe and should be called using run_coroutine_threadsafe or similar when called from another thread (this is fixed and handled by the transport). I have kept the fork check from the threaded worker, but I am not sure if it is necessary as forking in an async application would also break the event loop.

GH-4581

Copy link

codecov bot commented Jul 17, 2025

Codecov Report

❌ Patch coverage is 14.70588% with 87 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.48%. Comparing base (ef780f3) to head (859a0e2).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
sentry_sdk/worker.py 14.70% 86 Missing and 1 partial ⚠️
Additional details and impacted files
@@                        Coverage Diff                        @@
##           srothh/worker-class-hierarchy    #4591      +/-   ##
=================================================================
- Coverage                          84.98%   84.48%   -0.51%     
=================================================================
  Files                                158      158              
  Lines                              15414    15516     +102     
  Branches                            2432     2448      +16     
=================================================================
+ Hits                               13100    13108       +8     
- Misses                              1561     1654      +93     
- Partials                             753      754       +1     
Files with missing lines Coverage Δ
sentry_sdk/worker.py 46.94% <14.70%> (-34.14%) ⬇️

... and 1 file with indirect coverage changes

@srothh srothh force-pushed the srothh/worker-class-hierarchy branch from 1261319 to 2896602 Compare July 21, 2025 09:58
@srothh srothh force-pushed the srothh/async-task-worker branch from 7ada4b3 to 1a129f7 Compare July 21, 2025 10:42
@srothh srothh force-pushed the srothh/worker-class-hierarchy branch from 2896602 to 268ea1a Compare July 23, 2025 14:02
@srothh srothh force-pushed the srothh/async-task-worker branch from 1a129f7 to 97c5e3d Compare July 23, 2025 14:04
@srothh srothh marked this pull request as ready for review July 24, 2025 07:49
@srothh srothh requested a review from a team as a code owner July 24, 2025 07:49
Copy link
Member

@antonpirker antonpirker left a comment

Choose a reason for hiding this comment

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

Really great work. I have some comments for improvement.

@srothh srothh force-pushed the srothh/worker-class-hierarchy branch from 1fbf85f to ef780f3 Compare July 28, 2025 08:55
srothh added 10 commits July 30, 2025 11:41
Add a new implementation of the worker interface, implementing the worker as an async task. This is
to be used by the upcoming async transport.

GH-4581
Refactor the flush method in the async worker to use the async_flush coroutine.

GH-4581
…unctions

Add a check to see wheter callbacks are awaitable coroutines or functions, as coroutines need to be awaited.

GH-4581
…coroutines

Coroutines do not return None, therefore it is necessary to consider this in the callback parameter of the worker. Previously,
only callbacks with return Type None were accepted.

GH-4581
Enable concurrent callbacks on async task worker by firing them as a task rather than awaiting them. A done callback handles the necessary queue and exception logic.

GH-4581
Changed kill to also use the _TERMINATOR sentinel, so the queue is still drained to this point on kill instead of cancelled immediately. This should also fix potential race conditions with flush_async.

GH-4581
Add proper type annotation to worker task list to fix linting problems

GH-4581
Refactor worker implementation to simplify callback processing, fix pending calculation and improve queue initialisation.

GH-4581
@srothh srothh force-pushed the srothh/async-task-worker branch from 97c5e3d to b5eda0e Compare July 30, 2025 12:07
cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

srothh added 2 commits July 30, 2025 15:01
The flush method in the transport enqueues a sync callback for the worker, therefore the check needs to be here after all.

GH-4581
The callbacks passed to the worker from the transport are all async now, so this is currently not needed.

GH-4581
@srothh
Copy link
Member Author

srothh commented Jul 31, 2025

CI lint issue is in an unrelated file, not sure why it is popping up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants