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

Another thread can not read main thread class and modules at mruby 3.0 #68

Open
jakitliang opened this issue Dec 14, 2021 · 5 comments
Open

Comments

@jakitliang
Copy link

When in child thread do some code with:

class AAA; end

Thread.new do
   a = AAA.new
end

Will tiggers:

undefined class/module 'AAA'

Maybe symbols are not beeing migrate?

@mattn
Copy link
Owner

mattn commented Dec 14, 2021

This is limitation of mruby-thread. Thread is executed as separated VM instance. So you need to pass values to argument of constructor of the Thread.

th = Thread.new(v, m) do |v, m|

@jakitliang
Copy link
Author

This is limitation of mruby-thread. Thread is executed as separated VM instance. So you need to pass values to argument of constructor of the Thread.

I pass it to the argument but not works

@jakitliang
Copy link
Author

I write this example:

class AAA; end

a = AAA.new

t = Thread.new(a) do |a|
  p a
end

Will triggers:

undefined class/module AAA (ArgumentError)
Abort trap: 6

@lemmuh
Copy link

lemmuh commented Feb 23, 2022

What's missing is a migration of the newly generated module/class objects of the main thread (VM)

class A
end

Thread.new(Object.constants) do |constants|
  p constants - Object.constants
end

sleep 1

which will print

[:A, :ARGV]

Does anybody know how to migrate the module/class objects to the new thread (VM) ?

@dearblue
Copy link

(I've been probing at mruby/mruby#6077)

undefined class/module AAA (ArgumentError)
Abort trap: 6

This is because mrb_open() is called inside Thread.new, but class AAA is not defined at this time.
Classes, modules, and methods defined after mrb_open() are not defined on the new mruby state side, meaning they are not available.

The workaround I can think of is,

  • Create own mrbgem so that class AAA is defined when performing mrb_open().
  • Use a serializer such as mruby-marshal and pass it to Thread.new to deserialize the data after defining the class in the thread's block.
  • Otherwise, mruby-thread needs to be extended.

Also, the reason for the SIGABRT is that the mruby state created by Thread.new does not catch the exception.
This is the same situation as calling mrb_raise() directly in the main() function.
It would be better to pass not only mrb2 variables but also mrb variables when calling path2class(), and mrb_raise() should be addressed to mrb.

c = path2class(mrb2, RSTRING_PTR(cls_path), RSTRING_LEN(cls_path));

mrb_raisef(M, mrb_class_get(M, "ArgumentError"), "undefined class/module %S",

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

No branches or pull requests

4 participants