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

Query ruby thread and GVL states instead of relying on our call frame for callbacks #584

Merged
merged 4 commits into from Jan 25, 2019

Conversation

larskanis
Copy link
Member

This fixes #527 and other interoperability issues that can happen, when a callback is called without a valid call frame.

The change is enabled for ruby-2.3+ only, since it requires the functions ruby_native_thread_p() and ruby_thread_has_gvl_p().

The creation of a call frame is not (yet) removed, because it still carries callback expections around.

This also adds tests for interoperability with Fiddle and embedded ruby use case. Both are affected by the change to the FFI call frame.

@larskanis
Copy link
Member Author

@kugel- FYI

@kugel-
Copy link
Contributor

kugel- commented Jul 26, 2017

Awesome, thanks

@larskanis
Copy link
Member Author

I fixed the failing test on MacOS.

end

# https://github.com/ffi/ffi/issues/527
if RUBY_ENGINE == 'ruby' && RUBY_VERSION.split('.').map(&:to_i).pack("C*") >= [2,3,0].pack("C*")
Copy link
Member

@tduehr tduehr Jul 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can just compare the strings here as strings.

RUBY_VERSION >= "2.3.0"

Does rbx not support this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have seen ruby-2.1.10 and I know you as a very exact character, I thought it's best to compare exactly...

rbx doesn't support fiddle, so that these tests fail. The embed_test seems to work in rbx after some tweeking. May I adjust the tests for compatibility with rbx?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough...

changing the tests for rbx would depend on the nature of the changes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand your last comment (as a non English speaker). Shall I adjust the tests so that they pass on rbx (although it is no longer tested on travis-ci)?

Anyway I already tested this with JRuby (see 047371b) , so that the update of the testsuite could be merged there someday.

@tduehr
Copy link
Member

tduehr commented Aug 11, 2017

yeah. That wasn't super clear even to me on rereading it...

Is RBX failing because of incorrect behavior in FFI when running under RBX?

int state = 0;
VALUE ret;

rb_eval_string_protect(script, &state);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is RBX failing because of incorrect behavior in FFI when running under RBX?

It's failing because RBX doesn't know rb_eval_string_protect() but only rb_eval_string(). However the state is important to verify that the embedded script worked. So the program has to be changed, so that the success state is transferred in some other way (rb_rescue() or similar).

@kugel-
Copy link
Contributor

kugel- commented Oct 4, 2017

What's holding this back?

@tduehr
Copy link
Member

tduehr commented Jan 23, 2018

Can you rebase this so it runs against the new travis config?

@larskanis
Copy link
Member Author

I rebased to current master, but travis-ci tests on MacOS are still pending after 7 hours.

@tduehr
Copy link
Member

tduehr commented Jan 25, 2018 via email

@larskanis
Copy link
Member Author

Travis-CI has finished and it is mostly green.

@larskanis
Copy link
Member Author

Sorry, looked at the wrong pull request :-(

@larskanis larskanis force-pushed the fix-527-v2 branch 5 times, most recently from 029a5ea to 3f0a33a Compare February 13, 2018 20:25
@tduehr
Copy link
Member

tduehr commented Jan 25, 2019

Can you rebase this?

larskanis and others added 3 commits January 25, 2019 09:00
… for callbacks

This fixes ffi#527 and other interoperability issues that can happen,
when a callback is called without a valid call frame.

The change is enabled for ruby-2.3+ only, since it requires the
functions ruby_native_thread_p() and ruby_thread_has_gvl_p().

The creation of a call frame is not (yet) removed, because
it still carries callback expections around.
This is particular interesting because FFI stores thread local call info
on the stack, which is retrieved when a callback is received.
This is something that Fiddle doesn't do, but a callback should be handled
gracefully nevertheless.

The test "from fiddle to ffi" fails without the previous commit.

These tests succeed on MRI and on JRuby (provided
jruby/jruby#4518 will be merged).
Since the problem is a dead lock (inside pthread_cond_wait, on Linux), the test
case has to spawn a separate process which can be killed on failure.
@larskanis
Copy link
Member Author

OK, rebased and I fixed the issue on Windows.

Windows doesn't resolve symbols of external DLLs, if called with
CURRENT_PROCESS. Instead the DLL has to be named explicit.

Add an expectation to the spec while being over it.
@tduehr tduehr merged commit 772d2a5 into ffi:master Jan 25, 2019
@larskanis larskanis deleted the fix-527-v2 branch February 5, 2019 16: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.

Problem when embedding Ruby
3 participants