mattn / mruby-thread

48 stars 31 forks source link

Queue and Mutex cannot be migrated #31

Closed Asmod4n closed 9 years ago

Asmod4n commented 9 years ago

This happens when you try to access a Queue created outside of a thread

  thread #1: tid = 0x2012d1, 0x00007fff91cfc48a libsystem_kernel.dylib`__semwait_signal + 10, queue = 'com.apple.main-thread'
    frame #0: 0x00007fff91cfc48a libsystem_kernel.dylib`__semwait_signal + 10
    frame #1: 0x00007fff95c6fb98 libsystem_pthread.dylib`pthread_join + 445
    frame #2: 0x00000001000ce5e9 mrdb`mrb_thread_join(mrb=0x0000000100504cd0, self=mrb_value at 0x00007fff5fbfd7d0) + 121 at mrb_thread.c:379
    frame #3: 0x00000001000422a6 mrdb`mrb_context_run(mrb=0x0000000100504cd0, proc=0x00000001018243e0, self=mrb_value at 0x00007fff5fbfe590, stack_keep=2) + 8326 at vm.c:1125
    frame #4: 0x000000010003f80b mrdb`mrb_run(mrb=0x0000000100504cd0, proc=0x00000001018243e0, self=mrb_value at 0x00007fff5fbfe5c0) + 59 at vm.c:2411
    frame #5: 0x000000010003f0f9 mrdb`mrb_funcall_with_block(mrb=0x0000000100504cd0, self=mrb_value at 0x00007fff5fbfe740, mid=8, argc=0, argv=0x0000000000000000, blk=mrb_value at 0x00007fff5fbfe738) + 1545 at vm.c:404
    frame #6: 0x000000010000f6ad mrdb`mrb_instance_new(mrb=0x0000000100504cd0, cv=mrb_value at 0x00007fff5fbfe7b0) + 141 at class.c:1252
    frame #7: 0x00000001000422a6 mrdb`mrb_context_run(mrb=0x0000000100504cd0, proc=0x0000000101823540, self=mrb_value at 0x00007fff5fbff570, stack_keep=0) + 8326 at vm.c:1125
    frame #8: 0x000000010004940b mrdb`mrb_toplevel_run_keep(mrb=0x0000000100504cd0, proc=0x0000000101823540, stack_keep=0) + 123 at vm.c:2421
    frame #9: 0x0000000100066459 mrdb`load_exec(mrb=0x0000000100504cd0, p=0x0000000101812020, c=0x000000010075eaf0) + 1065 at parse.y:5619
    frame #10: 0x0000000100066013 mrdb`mrb_load_file_cxt(mrb=0x0000000100504cd0, f=0x00007fff7e906070, c=0x000000010075eaf0) + 67 at parse.y:5628
    frame #11: 0x0000000100005b6f mrdb`main(argc=2, argv=0x00007fff5fbff900) + 463 at mrdb.c:692
    frame #12: 0x00007fff95c765c9 libdyld.dylib`start + 1

* thread #2: tid = 0x2012ea, 0x00007fff95c6a5c1 libsystem_pthread.dylib`_pthread_mutex_lock + 23, stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
  * frame #0: 0x00007fff95c6a5c1 libsystem_pthread.dylib`_pthread_mutex_lock + 23
    frame #1: 0x00000001000d01a5 mrdb`mrb_queue_lock(mrb=0x0000000100519b60, self=mrb_value at 0x000000010217ff70) + 37 at mrb_thread.c:511
    frame #2: 0x00000001000cee19 mrdb`mrb_queue_pop(mrb=0x0000000100519b60, self=mrb_value at 0x000000010217fff0) + 41 at mrb_thread.c:555
    frame #3: 0x00000001000422a6 mrdb`mrb_context_run(mrb=0x0000000100519b60, proc=0x0000000101823390, self=mrb_value at 0x0000000102180db0, stack_keep=3) + 8326 at vm.c:1125
    frame #4: 0x000000010003f80b mrdb`mrb_run(mrb=0x0000000100519b60, proc=0x0000000101823390, self=mrb_value at 0x0000000102180de0) + 59 at vm.c:2411
    frame #5: 0x0000000100040122 mrdb`mrb_yield_with_class(mrb=0x0000000100519b60, b=mrb_value at 0x0000000102180e80, argc=1, argv=0x0000000100576dd0, self=mrb_value at 0x0000000102180e78, c=0x00000001008209d0) + 626 at vm.c:631
    frame #6: 0x00000001000cfcd5 mrdb`mrb_thread_func(data=0x0000000100519b20) + 133 at mrb_thread.c:316
    frame #7: 0x00007fff95c6c268 libsystem_pthread.dylib`_pthread_body + 131
    frame #8: 0x00007fff95c6c1e5 libsystem_pthread.dylib`_pthread_start + 176
    frame #9: 0x00007fff95c6a41d libsystem_pthread.dylib`thread_start + 13
Asmod4n commented 9 years ago

This happens on OS X and Linux with the latest mruby.

murasesyuka commented 9 years ago

I happen SEGV too, on Linux.

mattn commented 9 years ago

which script do you trying?

Asmod4n commented 9 years ago

https://github.com/mattn/mruby-thread/blob/master/example/queue.rb

mattn commented 9 years ago

original code ? or your modified ?

Asmod4n commented 9 years ago

Not modified,

But doesn't matter, as soon as you access a queue inside a thread mruby crashes.

Von einem mobilen Gerät gesendet

Am 07.09.2015 um 10:05 schrieb mattn notifications@github.com:

original code ? or your modified ?

— Reply to this email directly or view it on GitHub.

mattn commented 9 years ago

Well, I can't reproduce it on windows. I'll try to reproduce on linux.

murasesyuka commented 9 years ago

this hotfix and run queue.rb is success.

diff --git a/src/mrb_thread.c b/src/mrb_thread.c
index f01abf7..1d0fcc0 100644
--- a/src/mrb_thread.c
+++ b/src/mrb_thread.c
@@ -287,14 +287,6 @@ migrate_simple_value(mrb_state *mrb, mrb_value v, mrb_state *mrb2) {
     migrate_simple_iv(mrb, v, mrb2, nv);
     break;
   case MRB_TT_DATA:
-    {
-      if (!mrb_immediate_p(v)) {
-        struct RBasic *p;
-        p = mrb_obj_alloc(mrb2, mrb_type(v), mrb_obj_class(mrb, v));
-        nv = mrb_obj_value(p);
-        break;
-      }
-    }
     if (!is_safe_migratable_datatype(DATA_TYPE(v)))
       mrb_raise(mrb, E_TYPE_ERROR, "cannot migrate object");
     nv = v;
murasesyuka commented 9 years ago

mrb_immediate_p(v) is needed? this condition always FALSE. because MRB_TT_DATA is not immediate.

Asmod4n commented 9 years ago

Seams to work so far, thanks :)