From 6eb95b97141d074380c05b56c5647c916cbc9008 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Wed, 25 Mar 2026 15:40:34 -0700 Subject: [PATCH 1/2] test_broken_bignum: avoid fork and subprocess for robustness The raw fork + Process.waitpid2 pattern causes a SEGV on i686-linux in ruby/ruby CI. The crash occurs in waitpid_blocking_no_SIGCHLD going through the 32-bit vdso, which is a Ruby VM / kernel interaction issue unrelated to the json gem itself. Replace the fork-based approach with an in-process test that temporarily patches Integer#to_s and restores it via ensure. This avoids the fork SEGV on i686, works under valgrind (assert_separately doesn't), and is guarded to only run on CRuby (not JRuby/TruffleRuby) where the C extension behavior being tested exists. --- test/json/json_generator_test.rb | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/test/json/json_generator_test.rb b/test/json/json_generator_test.rb index 1e44117e..245a547a 100755 --- a/test/json/json_generator_test.rb +++ b/test/json/json_generator_test.rb @@ -493,25 +493,20 @@ def foo.to_h assert_equal '2', state.indent end - def test_broken_bignum # [ruby-core:38867] - pid = fork do - x = 1 << 64 - x.class.class_eval do + if defined?(JSON::Ext::Generator) and RUBY_PLATFORM != "java" + def test_broken_bignum # [ruby-core:38867] + bignum = 1 << 64 + original_to_s = bignum.class.instance_method(:to_s) + bignum.class.class_eval do def to_s end end - begin - JSON::Ext::Generator::State.new.generate(x) - exit 1 - rescue TypeError - exit 0 + assert_raise(TypeError) do + JSON::Ext::Generator::State.new.generate(bignum) end + ensure + bignum.class.define_method(:to_s, original_to_s) if original_to_s end - _, status = Process.waitpid2(pid) - assert status.success? - rescue NotImplementedError - # forking to avoid modifying core class of a parent process and - # introducing race conditions of tests are run in parallel end def test_hash_likeness_set_symbol From 79883fdb4e5208c33f0648f666e1c2cfec316886 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 26 Mar 2026 07:06:26 +0100 Subject: [PATCH 2/2] Avoid method redefinition warnings in test_broken_bignum --- lib/json/truffle_ruby/generator.rb | 6 ++++-- test/json/json_generator_test.rb | 27 +++++++++++++++++---------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/json/truffle_ruby/generator.rb b/lib/json/truffle_ruby/generator.rb index 09459286..01911645 100644 --- a/lib/json/truffle_ruby/generator.rb +++ b/lib/json/truffle_ruby/generator.rb @@ -413,7 +413,7 @@ def generate(obj, anIO = nil) buf << obj.to_json(self) end when Integer - buf << obj.to_s + buf << String(obj) when Symbol if @strict fast_serialize_string(obj.name, buf) @@ -652,7 +652,9 @@ def json_transform(state) module Integer # Returns a JSON string representation for this Integer number. - def to_json(*) to_s end + def to_json(*) + String(self) + end end module Float diff --git a/test/json/json_generator_test.rb b/test/json/json_generator_test.rb index 245a547a..00960dff 100755 --- a/test/json/json_generator_test.rb +++ b/test/json/json_generator_test.rb @@ -493,20 +493,27 @@ def foo.to_h assert_equal '2', state.indent end - if defined?(JSON::Ext::Generator) and RUBY_PLATFORM != "java" - def test_broken_bignum # [ruby-core:38867] - bignum = 1 << 64 - original_to_s = bignum.class.instance_method(:to_s) - bignum.class.class_eval do - def to_s - end + def test_broken_bignum # [Bug #5173] + bignum = 1 << 64 + bignum_to_s = bignum.to_s + + original_to_s = bignum.class.instance_method(:to_s) + bignum.class.class_eval do + def to_s + nil end + alias_method :to_s, :to_s + end + case RUBY_PLATFORM + when "java" + assert_equal bignum_to_s, JSON.generate(bignum) + else assert_raise(TypeError) do - JSON::Ext::Generator::State.new.generate(bignum) + JSON.generate(bignum) end - ensure - bignum.class.define_method(:to_s, original_to_s) if original_to_s end + ensure + bignum.class.define_method(:to_s, original_to_s) if original_to_s end def test_hash_likeness_set_symbol