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

Add CoroutineExceptionHandler #4259

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

lcian
Copy link
Member

@lcian lcian commented Mar 13, 2025

📜 Description

Adds a CoroutineExceptionHandler, similar to UncaughtExceptionHandler for exceptions thrown in coroutines.
It can be used together with SentryContext when launching coroutines.

💡 Motivation and Context

Closes #1524
I have tried https://github.com/Anamorphosee/stacktrace-decoroutinator as well but it doesn't really improve the stack trace and also only supports being enabled using a plugin in Android.

💚 How did you test it?

Unit tests and manual test in the UI triggered by button in Android app

📝 Checklist

  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

Update the docs

Sorry, something went wrong.

Copy link
Contributor

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 388.40 ms 398.72 ms 10.32 ms
Size 1.58 MiB 2.21 MiB 646.60 KiB

@lcian lcian marked this pull request as ready for review March 17, 2025 11:58
@lcian
Copy link
Member Author

lcian commented Mar 17, 2025

Screenshot 2025-03-17 at 13 13 18
This is how the issue and stack trace look like when running it from the sample app with an intermediate function, like so:

    fun throwInCoroutine() {
        GlobalScope.launch(SentryContext() + SentryCoroutineExceptionHandler()) {
            someFunction()
        }
    }

    fun someFunction() {
        throw RuntimeException("Exception in coroutine")
    }

val mechanism = Mechanism().apply {
type = "CoroutineExceptionHandler"
}
// the current thread is not necessarily the one that threw the exception
Copy link
Member Author

Choose a reason for hiding this comment

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

I think this behavior also happens in some of our other integrations for async stuff, right?

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.

Support SentryCoroutineExceptionHandler
1 participant