Skip to content

Pytest plugin pytest-sentry not sending issue events because it is killed #4141

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
antonpirker opened this issue Mar 17, 2025 · 3 comments
Labels

Comments

@antonpirker
Copy link
Member

antonpirker commented Mar 17, 2025

How do you use Sentry?

Sentry Saas (sentry.io)

Version

all

Steps to Reproduce

See this example project:
https://github.com/antonpirker/testing-sentry/tree/main/test-pytest-sentry

We run a flaky test multiples times using the pytest-sentry plugin for pytest (which can be found here: https://github.com/getsentry/pytest-sentry)

Expected Result

Issues representing the failed tests runs showing up in Sentry.

Actual Result

The envelopes never reach Sentry. It seems that pytest kills the process right in the middle of sending the envelope: https://github.com/getsentry/sentry-python/blob/2.23.1/sentry_sdk/transport.py#L349-L354

The line after self._request() is never reached.

@bukzor
Copy link

bukzor commented Mar 17, 2025

This may (or may not) be a workaround, below. The idea is to ensure that client.flush() is called before the main thread exits.

import pytest_sentry import hookwrapper

@hookwrapper(itemgetter=lambda session: session)
def pytest_sessionfinish(session):
    del session
    yield
    Scope.get_client().flush(timeout=2.0)

To do this in a generic way requires a bit more code. For an example, see
https://github.com/getsentry/sentry/blob/1785768/src/sentry/testutils/pytest/json_report_reruns.py

HOWEVER, it's a mystery why this isn't already handled by the AtExit integration. Its purpose is exactly this, and I double-checked that it's active and enabled, so why/how isn't it doing its job?

@szokeasaurusrex
Copy link
Member

@antonpirker, why is this a bug in Sentry SDK and not in the pytest-sentry plugin?

@antonpirker
Copy link
Member Author

Because no one ever looks at issues in pytest-sentry. This way we do not forget about this.

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

No branches or pull requests

4 participants