Skip to content

Instantly share code, notes, and snippets.

@calvinxiao
Created April 21, 2021 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calvinxiao/a7793c63524259cc87cebb392212394c to your computer and use it in GitHub Desktop.
Save calvinxiao/a7793c63524259cc87cebb392212394c to your computer and use it in GitHub Desktop.
SystemStackOverflow in CRuby
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