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

Exceptions captured in threads not associating with correct transaction #3865

Open
jvntf opened this issue Dec 6, 2024 · 1 comment
Open

Comments

@jvntf
Copy link

jvntf commented Dec 6, 2024

How do you use Sentry?

Sentry Python SDK

Version

2.19.0 and 1.43.0 (tried with both versions)

Steps to Reproduce

I am having an issue with correctly tracking a transaction in a multithreaded context. We use a threadpool executor to execute some functions on a series of tasks. On each task, we use the executor to perform one function synchronously (wait for the result) and one function asynchronously (do not wait for the result). The asynchronous function will finish the transaction when it completes, meanwhile the main thread moves on to the next task. As a result, there will be conditions where the main thread has started a new transaction while the previous one is still completing in the previous task's async_work function.

On the sentry performance tab, I see correct traces. The child transactions are correctly rendered. My issue is for manually captured exceptions, sentry_sdk.capture_exception. These issues maintain a link to the correct trace, but for some reason are rendered with the wrong transaction name. Appreciate all insight here!

Below is a minimal example of the circumstance:

import time
from concurrent.futures import ThreadPoolExecutor

import sentry_sdk
import sentry_sdk.scope
from structlog import get_logger

from src.utils.sentry import init_sentry

logger = get_logger(__name__)
TASKS = [{"taskId": i} for i in range(5)]


def sync_work(transaction):
    with transaction.start_child(op="task", name="sync"):
        logger.info("Running sync work")


def async_work(transaction):
    with transaction.start_child(op="task", name="async"):
        logger.info("Running async work")
        time.sleep(2)
        logger.info("Finished async work")
        sentry_sdk.capture_exception(Exception("Async work failed"))
    transaction.finish()


def main():
    init_sentry()
    with ThreadPoolExecutor(max_workers=4) as executor:
        for task in TASKS:
            try:
                transaction = sentry_sdk.start_transaction(name="test-task", op="task")
                sync_result = executor.submit(sync_work, transaction)
                sync_result.result()
                executor.submit(async_work, transaction)
            except Exception as err:
                logger.error(err)


if __name__ == "__main__":
    main()

Expected Result

The exception should show the right transaction name

Actual Result

Image

Image

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Dec 6, 2024
@sentrivana
Copy link
Contributor

Thanks @jvntf for raising this -- we will take a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

No branches or pull requests

2 participants