Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a very unlikely GC bug when using a callback block #1089

Merged
merged 1 commit into from
Mar 31, 2024

Conversation

larskanis
Copy link
Member

This could happen, when the Proc object created by rb_block_proc() is GC'ed before the call is executed. Practically this is very unlikely unless called with GC.stress=true .

It can easily be fixed, by returning the Proc object and keeping it in a local variable while the call is executed.

When running the test suite with GC.stress=true then it failed like so: Failures:

  1) Callback struct by value parameter
     Failure/Error:
       LibTest.testCallbackTrV(s) do |struct|
         s2[:s8] = struct[:s8]
         s2[:f32] = struct[:f32]
         s2[:s32] = struct[:s32]
       end

     NoMethodError:
       undefined method `call' for an instance of FFI::MemoryPointer
      ./spec/ffi/callback_spec.rb:312:in `testCallbackTrV'
      ./spec/ffi/callback_spec.rb:312:in `block (2 levels) in <module:CallbackSpecs>'

  2) Callback with  struct by reference argument
     Failure/Error: LibTest.testCallbackYrV(magic) { |i| v = i }

     NoMethodError:
       undefined method `call' for an instance of FFI::Pointer
      ./spec/ffi/callback_spec.rb:802:in `testCallbackYrV'
      ./spec/ffi/callback_spec.rb:802:in `block (2 levels) in <module:CallbackWithSpecs>'

  3) Callback with  struct by reference argument with nil value
     Failure/Error: LibTest.testCallbackYrV(nil) { |i| v = i }

     NoMethodError:
       undefined method `call' for an instance of CallbackWithSpecs::LibTest::S8F32S32
      ./spec/ffi/callback_spec.rb:809:in `testCallbackYrV'
      ./spec/ffi/callback_spec.rb:809:in `block (2 levels) in <module:CallbackWithSpecs>'

Finished in 100 minutes 21 seconds (files took 0.56651 seconds to load) 5014 examples, 3 failures

This could happen, when the Proc object created by rb_block_proc() is GC'ed before the call is executed.
Practically this is very unlikely unless called with GC.stress=true .

It can easily be fixed, by returning the Proc object and keeping it in a local variable while the call is executed.

When running the test suite with GC.stress=true then it failed like so:
Failures:

  1) Callback struct by value parameter
     Failure/Error:
       LibTest.testCallbackTrV(s) do |struct|
         s2[:s8] = struct[:s8]
         s2[:f32] = struct[:f32]
         s2[:s32] = struct[:s32]
       end

     NoMethodError:
       undefined method `call' for an instance of FFI::MemoryPointer
      ./spec/ffi/callback_spec.rb:312:in `testCallbackTrV'
      ./spec/ffi/callback_spec.rb:312:in `block (2 levels) in <module:CallbackSpecs>'

  2) Callback with  struct by reference argument
     Failure/Error: LibTest.testCallbackYrV(magic) { |i| v = i }

     NoMethodError:
       undefined method `call' for an instance of FFI::Pointer
      ./spec/ffi/callback_spec.rb:802:in `testCallbackYrV'
      ./spec/ffi/callback_spec.rb:802:in `block (2 levels) in <module:CallbackWithSpecs>'

  3) Callback with  struct by reference argument with nil value
     Failure/Error: LibTest.testCallbackYrV(nil) { |i| v = i }

     NoMethodError:
       undefined method `call' for an instance of CallbackWithSpecs::LibTest::S8F32S32
      ./spec/ffi/callback_spec.rb:809:in `testCallbackYrV'
      ./spec/ffi/callback_spec.rb:809:in `block (2 levels) in <module:CallbackWithSpecs>'

Finished in 100 minutes 21 seconds (files took 0.56651 seconds to load)
5014 examples, 3 failures
@larskanis larskanis merged commit 24143c4 into ffi:master Mar 31, 2024
80 of 81 checks passed
@larskanis larskanis deleted the fix-gcstress branch March 31, 2024 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant