Fix crash when a span is finished twice or used from multiple threads - #1609
Merged
Merged
Conversation
On sentry-native platforms, finishing a span or transaction hands ownership to the SDK, which frees it. The wrappers kept the stale pointer and only locked the setters, so a second Finish, or Finish racing a setter, StartChild or GetTrace on another thread, used freed memory. Wrappers now take their lock for every native call and null the pointer on finish; later calls are no-ops. USentrySpan::Finish gets the IsFinished guard USentryTransaction already had.
limbonaut
approved these changes
Oct 2, 2026
Collaborator
|
If |
Collaborator
Author
I think a double-finish guard makes sense regardless. Otherwise, a second finish would release another reference and repeat the finish work, no? |
jpnurmi
approved these changes
Oct 2, 2026
jpnurmi
left a comment
Collaborator
There was a problem hiding this comment.
Yeah, probably doesn't help much 🤦♂️
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes crashes on Windows, Linux and other
sentry-nativeplatforms when a span or transaction is finished twice, or finished on one thread while another thread is still using it.sentry_span_finish/sentry_transaction_finishhand ownership tosentry-native, which frees the object. The plugin wrappers kept the stale pointer and only locked the tag/data setters, so:Finishtwice on aUSentrySpanfreed the native span twice and corrupted the heap. UnlikeUSentryTransaction,USentrySpan::Finishhad noIsFinished()guard.Finishon one thread while another called a setter,StartChildorGetTraceon the same span or transaction could use freed memory.Key changes
FGenericPlatformSentrySpan/FGenericPlatformSentryTransactiontake their lock around every native call and set the native pointer to null after finishing. Later calls are no-ops, andIsFinished()is now based on that pointer, replacing the separate flag.USentrySpan::Finish/FinishWithTimestampnow checkIsFinished(), asUSentryTransactionalready did.