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

Metrics not sent on program exit #2983

Open
szokeasaurusrex opened this issue Apr 16, 2024 · 3 comments
Open

Metrics not sent on program exit #2983

szokeasaurusrex opened this issue Apr 16, 2024 · 3 comments
Labels
Type: Bug Something isn't working

Comments

@szokeasaurusrex
Copy link
Member

szokeasaurusrex commented Apr 16, 2024

How do you use Sentry?

Sentry Saas (sentry.io)

Version

1.45.0 and 2.0.0rcX

Steps to Reproduce

Follow these steps.

Expected Result

The drank-drinks metric is sent to Sentry, regardless of whether we call sentry_sdk.flush() before the program's end.

Actual Result

The drank-drinks method is only sent if we call sentry_sdk.flush() before the program ends, or if we interrupt the program by pressing ⌃C. If we run the code with sentry_sdk.flush() commented out, i.e. without modifying the sample in "Steps to Reproduce," the metric does not get sent.

@szokeasaurusrex
Copy link
Member Author

Possibly related: #687

szokeasaurusrex added a commit to szokeasaurusrex/issue-reproductions that referenced this issue Apr 16, 2024
@szokeasaurusrex szokeasaurusrex added the Type: Bug Something isn't working label Apr 16, 2024
@sentrivana
Copy link
Contributor

This is because in Python 3.12 you can't start a new thread at interpreter shutdown. If you just let the program finish, the atexit hook will try to get the transport to send the pending events, but since the transport worker doesn't have a thread running yet, it first has to try to start one -- which fails here. If you Ctrl+C the program in between, the thread gets started before the atexit hook, which is fine.

We can look into improving this but I'm not sure how likely this is to happen in real world usage. Usually the worker thread has already been set up so there's no need to start it at shutdown. We could probably start it right away instead of on demand, but that could lead to issues on setups that fork the process the SDK is running in.

@szokeasaurusrex
Copy link
Member Author

@sentrivana Thank you for the detailed explanation. It indeed appears that this issue is specific to Python 3.12, since metrics are sent at program exit in the other Python versions I tested (3.11 and 3.8).

This behavior is quite confusing from a user perspective, however, so I think we should invest in creating a solution that allows us to ensure the transport is flushed at program exit in Python 3.12.

Would running the worker in a separate process perhaps allow us to work around the situation where the user code forks the process the SDK is running in?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants