Skip to content

Instantly share code, notes, and snippets.

@osak
Last active August 29, 2015 14:16
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 osak/71648a7360d9a0a34f57 to your computer and use it in GitHub Desktop.
Save osak/71648a7360d9a0a34f57 to your computer and use it in GitHub Desktop.
require 'set'
require 'monitor'
log = []
END {
log << "end"
puts log
}
m = Monitor.new
h = Set.new
f = ->(objid){
begin
m.synchronize do
log << "delete #{objid.to_s(16)}"
h.delete(objid)
end
rescue Exception => e
log << e.to_s
end
}
loop do
a = Array.new(10000000)
ObjectSpace.define_finalizer(a, &f)
m.synchronize do
if h.include?(a.object_id)
raise "#{a.object_id.to_s(16)} already included!"
end
h << a.object_id
log << "insert #{a.object_id.to_s(16)}"
end
end
YUKI.N@aincrad> ruby a.rb
insert b1394c
insert b27f78
can't be called from trap context
insert b27f00
can't be called from trap context
insert 919b64
can't be called from trap context
insert a21f34
can't be called from trap context
insert a21e6c
can't be called from trap context
insert b27eb0
insert b27dfc
can't be called from trap context
can't be called from trap context
insert b27d98
can't be called from trap context
insert 91977c
can't be called from trap context
insert a21fac
can't be called from trap context
insert a21e08
can't be called from trap context
end
a.rb:26:in `block (2 levels) in <main>': b27eb0 already included! (RuntimeError)
from /usr/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
from a.rb:24:in `block in <main>'
from a.rb:21:in `loop'
from a.rb:21:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment