Skip to content

fix: Add sentry_reset_backend API - #388

Closed
GenuineAster wants to merge 1 commit into
getsentry:masterfrom
dagg-ai:fix/handler_reset
Closed

GenuineAster wants to merge 1 commit into
getsentry:masterfrom
dagg-ai:fix/handler_reset

Conversation

@GenuineAster

Copy link
Copy Markdown
Contributor

We have a third-party dynamic library (.NET Core runtime) that is loaded far into our application's runtime which, on load, steals all signal handlers. On Linux this works out fine, they are responsible and call whatever signal handler was there before them. No such luck on Windows. Forking and patching the .NET Core runtime is quite a bit more work, and we may run into this kind of issue in the future, so our internal fix is to add this API, and hopefully we can get this upstreamed and avoid forking sentry-native altogether.

I'm not really sure how to write tests for this, but I can definitely add tests for it with a bit of guidance!

@codecov-commenter

codecov-commenter commented Oct 1, 2020 •

Copy link
Copy Markdown

Codecov Report

Merging #388 into master will decrease coverage by 0.12%.
The diff coverage is 0.00%.

@@            Coverage Diff             @@
##           master     #388      +/-   ##
==========================================
- Coverage   87.49%   87.37%   -0.13%     
==========================================
  Files          49       49              
  Lines        4142     4149       +7     
==========================================
+ Hits         3624     3625       +1     
- Misses        518      524       +6     

We have a third-party dynamic library (.NET Core runtime) that is loaded far into our application's runtime which, on load, steals all signal handlers. On Linux this works out fine, they are responsible and call whatever signal handler was there before them. No such luck on Windows. Forking and patching the .NET Core runtime is quite a bit more work, and we may run into this kind of issue in the future, so our internal fix is to add this API, and hopefully we can get this upstreamed and avoid forking sentry-native altogether.
Comment thread src/sentry_core.c
int
sentry_reset_backend(void)
{
sentry_backend_t *backend = g_options->backend;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You have to wrap this in SENTRY_WITH_OPTIONS (options) {
Otherwise you are not thread safe, and another thread can just free g_options from under you.
Also note that you can’t early-return from that macro :-(

Comment thread include/sentry.h
SENTRY_EXPERIMENTAL_API void sentry_clear_modulecache(void);

/**
* Re-initializes the Sentry backend

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Re-initializes the Sentry backend
* Re-initializes the Sentry backend.

Comment thread include/sentry.h
/**
* Re-initializes the Sentry backend
*
* This is mainly meant for if a third-party library hijacks Sentry's signal

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would maybe phrase this more positively:

This is needed if a third-party library overrides the previously installed signal handler, and reinstalls the Sentry handler.

Comment thread include/sentry.h
*
* Returns 0 on success.
*/
SENTRY_API int sentry_reset_backend(void);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
SENTRY_API int sentry_reset_backend(void);
SENTRY_EXPERIMENTAL_API int sentry_reset_backend(void);

@Swatinem

Swatinem commented Oct 2, 2020

Copy link
Copy Markdown
Contributor

I really like this!

Also, I was running into this case recently, investigating why native crashes when using a .NET won’t be captured; instead .NET handles them somehow.
I’m a bit concerned as for example the crashpad backend is not really meant to be restarted.

Also, an e2e test would be nice, but I think its a bit too complex to integrate into our example (which we use for tests)

@bruno-garcia

Copy link
Copy Markdown
Member

Could this be achieved by the call to init?
Basically close any previous clients, flushes stuff, and initializes again?

It's what we do in .NET:

https://github.com/getsentry/sentry-dotnet/blob/42b0f921f0503c561046b96c3ad486511487e7cb/src/Sentry/SentrySdk.cs#L92-L94

Ref swap the current thing with the new one, the close out the previous one once it's not bound anymore/

@GenuineAster

Copy link
Copy Markdown
Contributor Author

I am quite busy this week (worked on this as part of a hackathon project) but I will fix your comments early next week, as well as testing with crashpad and looking into testing this a bit more!

@Swatinem

Swatinem commented Oct 8, 2020

Copy link
Copy Markdown
Contributor

Could this be achieved by the call to init?

@bruno-garcia Thats already what sentry_init does. In theory, one could just call init again, but that re-initializes the whole SDK. Here we just want to reset the backend, and make sure the signal handlers are correct.

Not quite sure whats the case with .NET, what part of the code runs when, and who wins, with respect to which signal handlers are active.

@bruno-garcia

Copy link
Copy Markdown
Member

Fwiw I was able to capture minidumps on CoreCLR on all platforms:

https://github.com/getsentry/sentry-dotnet-minidump

@Swatinem

Swatinem commented Jan 5, 2021

Copy link
Copy Markdown
Contributor

Rebased and merged in 2a55499

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.

4 participants