Collect stack frames immediately in Ruby 3.0 #150
Merged
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.
As of ruby/ruby@0e276dc, which shipped in Ruby 3.0, it seems to be safe to collect stack frames inside the signal handler. This should allow more accurate results than waiting for the postponed job to run since that can only measure when interrupts are checked.
This new behaviour is wrapped inside a preprocessor check for Ruby 3+
Additionally, this moves the "in signal handler" checks up a level, and uses pthread_mutex_trylock, which should be more reliable and I believe will fix the issue described in #123 and #124
@tenderlove and I had some discussion about whether this could have issues due to the writes in ruby/ruby@0e276dc being reordered.
One concern is whether there could be issues with hardware memory reordering (particularly on arm). I believe this is safe only because we are only considering the stack from our current thread. These writes will appear consistent in our interrupt handler because of this.
Another concern is that the compiler could reorder the writes in ruby/ruby@0e276dc. It doesn't seem to be, but that could absolutely happen. I think we should investigate making a change to Ruby to ensure the writes in
vm_push_frame/vm_pop_framearen't reordered.