When running the Capybara test test suite calling last_reponse on a rack_test Session object will sporadically result in a TypeError from forwardable.rb
TypeError:
wrong argument type Integer (expected Proc)
stops the error from occurring, so it appears that something def_delegators is doing is causing problems. Unfortunately I have not yet been able to establish exactly what causes the issues to produce a simple test case.
The mongo gem is also affected by this bug (https://jira.mongodb.org/browse/RUBY-1191). Running the tests in the spec/mongo/grid/stream/read_spec.rb file reliably reproduces it.
I bisected the changes to forwardable.rb between v2.3.0 and v2.4.0, and determined that the bug was introduced in r55376.
I've attached a patch that fixes the problem, although it effectively reverts the optimisation from r55376, so there may be a performance impact.
Here's another patch that also fixes the problem, while keeping the optimisation from r55376.
I'll admit that I have no idea what this option does or how it could cause this bug - I noticed that the "portable" implementation of forwardable/impl.rb wasn't affected, and fiddled with the MRI-specific one until it worked. Please review this patch carefully if you intend to use it!
Removing tailcall_optimization also appears to work, although I think we need that option to get a sensible stack trace. Maybe trace_instruction: false, tailcall_optimization: true is an invalid combination?
vm_insnhelper.c (vm_call_iseq_setup_tailcall): check interrupts
after set up the new frame, not the passed block to be clobbered
by invoked finalizers and so on. [ruby-core:78981] [Bug #13107]
When interrupted by a finalizer during setting up tailcall frame, a block argument on the stack was overwritten by the magic number in the finalizer frame which looks an Integer.
I tried in vain to make a test case, but a failure in mongo-ruby-driver seems fixed.
Could you try the latest trunk?