Skip to content

Instantly share code, notes, and snippets.

@alpaca-tc
Created January 19, 2024 07:40
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 alpaca-tc/eb9ac65b7761276766c63948c7099cff to your computer and use it in GitHub Desktop.
Save alpaca-tc/eb9ac65b7761276766c63948c7099cff to your computer and use it in GitHub Desktop.
Example of deadlocking by executing require in multithread
# ruby 3.2.3 (2024-01-18 revision 52bb2ac0a6) [arm64-darwin23]
$LOAD_PATH << "#{__dir__}/lib"
# # lib/first.rb
# module First
# require 'second'
# end
# # lib/second.rb
# module Second
# require 'first'
# end
[
Thread.new { require 'second' },
Thread.new { require 'first' }
].each(&:join)
# ./a.rb:16:in `join': No live threads left. Deadlock? (fatal)
# 3 threads, 3 sleeps current:0x000000011f715450 main thread:0x00000001201048d0
# * #<Thread:0x0000000100eab108 sleep_forever>
# rb_thread_t:0x00000001201048d0 native:0x00000001d6f2d000 int:0
#
# * #<Thread:0x0000000101224780 ./a.rb:14 sleep_forever>
# rb_thread_t:0x000000011f713700 native:0x000000016f26b000 int:0 mutex:0x000000011f717720 cond:1
# depended by: tb_thread_id:0x00000001201048d0
#
# * #<Thread:0x0000000101224640 ./a.rb:15 sleep_forever>
# rb_thread_t:0x000000011f715450 native:0x000000016f477000 int:0
#
#
# from ./a.rb:16:in `each'
# from ./a.rb:16:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment