Fix breaking references after GC.compact#2822
Merged
ksss merged 2 commits intoruby:masterfrom Jan 23, 2026
Merged
Conversation
Collaborator
Author
|
I found that even static symbols can have broken references starting from 4.0, so I fixed it to copy uniquely. |
ksss
added a commit
to ksss/rbs
that referenced
this pull request
Jan 30, 2026
If GC.compact occurs after parsing, strings referenced from the parse result are corrupted.
Below is a reproduction script.
In this case, it is intentionally reproducing a scenario where `GC.compact` occurs during parsing.
```rb
require 'rbs'
module Patch
def to_i(...)
super(...)
end
end
String.prepend(Patch)
TracePoint.new(:call) do |tp|
GC.start
GC.start
GC.compact
end.enable(target: String.instance_method(:to_i))
errors = 0
n = 100
n.times do |i|
1000.times { |i| "\x00" * (1000); "X#{i}" * 100 }
len = 500
var = ("A" * len + "_V#{i}").to_sym
type = "(42 | #{var}) -> Array[#{var}]"
result = RBS::Parser.parse_method_type(type, variables: [var])
if var == result.type.return_type.args.first.name
print "."
else
errors += 1
print "X"
end
end
puts "\n#{errors}/n errors (#{(errors * 100.0 / n).round(1)}%)"
exit(errors > 0 ? 1 : 0)
```
To fix this issue, similar to ruby#2822 ,
strings of symbols given as arguments are copied and retained.
If it is an **owned** type, `free()` will also be performed when the constant pool is freed.
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.
Although rare, there's a possibility of breaking references after
GC.compactif a dynamic symbol happens to be interned into the constant pool.To fix this issue, I've modified the code to copy the string and intern it as an owned type when a dynamic symbol is passed.
Additionally, we've written a test case that reproduces the problem.