Created
April 21, 2021 07:45
-
-
Save calvinxiao/a7793c63524259cc87cebb392212394c to your computer and use it in GitHub Desktop.
SystemStackOverflow in CRuby
This file contains 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
require 'json' | |
require 'thread' | |
# Thread.abort_on_exception = true | |
# Thread.report_on_exception = true | |
# this will raise SystemStackOverflow | |
def json_dump | |
obj = {} | |
obj['c'] = obj | |
::JSON.dump obj | |
end | |
def run_in_thread | |
t = Thread.new do | |
begin | |
json_dump | |
rescue Exception => ee | |
puts "run_in_thread rescue #{ee}" | |
ensure | |
puts "run_in_thread ensure" | |
end | |
end | |
t.join | |
end | |
# try 1 | |
begin | |
puts "running try 1" | |
json_dump | |
rescue Exception => e | |
puts "try 1 rescued #{e.class} #{e.message}" | |
ensure | |
puts 'try 1 run the code is OK' | |
end | |
# try 2 | |
# https://bugs.ruby-lang.org/issues/13164 | |
begin | |
puts "\nrunning try 2" | |
json_dump | |
rescue Exception => e | |
puts "try 2 rescued #{e.class} #{e.message}" | |
ensure | |
puts 'try 2 run the code is OK' | |
end | |
# try 3 | |
begin | |
puts "\nrunning try 3" | |
run_in_thread | |
rescue Exception => e | |
puts "try 3 rescued #{e.class} #{e.message}" | |
ensure | |
puts 'try 3 ensure run_without_block is OK' | |
end | |
gets |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment