FCO / Red

A WiP ORM for Raku
Artistic License 2.0
70 stars 27 forks source link

`MVM_str_hash_lvalue_fetch_nocheck called concurrently on the same hash` in Red::Model #529

Closed patrickbkr closed 2 years ago

patrickbkr commented 2 years ago

I work on an app that uses Red in parallel. I didn't look into it deeply, but I don't think I do something nasty.

The following patch is probably not the right fix, but does seem to work around the problem:

diff --git a/lib/MetamodelX/Red/Model.pm6 b/lib/MetamodelX/Red/Model.pm6
index 6d4e346..4d5e0f5 100644
--- a/lib/MetamodelX/Red/Model.pm6
+++ b/lib/MetamodelX/Red/Model.pm6
@@ -55,6 +55,7 @@ has $.table;
 has Bool $!temporary;
 has Bool $!default-null;
 has %!alias-cache;
+has $!alias-cache-lock = Lock.new;

 multi method emit(Mu $model, Red::Event $event) {
     start try get-RED-DB.emit: $event.clone: :model($model.WHAT)
@@ -221,7 +222,9 @@ my UInt $alias_num = 1;
 method alias(|c (Red::Model:U \type, Str $name = "{type.^name}_{$alias_num++}", :$base, :$relationship, :$join-type)) {
     return %!alias-cache{$name} if %!alias-cache{$name}:exists;
     my \alias = ::?CLASS.new_type(:$name);
-    %!alias-cache{$name} := alias;
+    $!alias-cache-lock.protect({
+        %!alias-cache{$name} := alias;
+    });
     my role RAlias[Red::Model:U \rtype, Str $rname, \alias, \rel, \base, \join-type, @cols] {
         method columns(|)     { @cols }
         method table(|)       { rtype.^table }

Full error message below.

MoarVM oops: MVM_str_hash_lvalue_fetch_nocheck called concurrently on the same hash                                                                                                                                                   
   at SETTING::src/core.c/Hash.pm6:147  (/home/patrick/rrepos/install/share/perl6/runtime/CORE.c.setting.moarvm:BIND-KEY)                                                                                                             
 from SETTING::src/core.c/hash_slice.pm6:13  (/home/patrick/rrepos/install/share/perl6/runtime/CORE.c.setting.moarvm:postcircumfix:<{ }>)                                                                                             
 from /home/patrick/repos/Red/lib/MetamodelX/Red/Model.pm6 (MetamodelX::Red::Model):224  (/home/patrick/repos/RakuCIBot/.precomp/23A33F92DE051072A0810737FDD2D159EEE174E3/8B/8B1AA6D9F43A092F412A8E07D637DC82016DA006:alias)          
 from /home/patrick/repos/Red/lib/MetamodelX/Red/Model.pm6 (MetamodelX::Red::Model):217  (/home/patrick/repos/RakuCIBot/.precomp/23A33F92DE051072A0810737FDD2D159EEE174E3/8B/8B1AA6D9F43A092F412A8E07D637DC82016DA006:join)           
 from /home/patrick/repos/Red/lib/MetamodelX/Red/Model.pm6 (MetamodelX::Red::Model):206  (/home/patrick/repos/RakuCIBot/.precomp/23A33F92DE051072A0810737FDD2D159EEE174E3/8B/8B1AA6D9F43A092F412A8E07D637DC82016DA006:join)            from /home/patrick/repos/Red/lib/MetamodelX/Red/Relationship.pm6 (MetamodelX::Red::Relationship):30  (/home/patrick/repos/RakuCIBot/.precomp/23A33F92DE051072A0810737FDD2D159EEE174E3/14/145608ECEFD6808003B97602F6C27E6A0DC68DC3:<anon>)                                                                                                                                                                                                                                  
 from /home/patrick/repos/Red/lib/Red/Driver/CommonSQL.pm6 (Red::Driver::CommonSQL):371  (/home/patrick/repos/RakuCIBot/.precomp/23A33F92DE051072A0810737FDD2D159EEE174E3/7C/7CB3556581630284BB3680044580D44875B4B0C9:)               
 from SETTING::src/core.c/Any-iterable-methods.pm6:326  (/home/patrick/rrepos/install/share/perl6/runtime/CORE.c.setting.moarvm:pull-one)                                                                                             
 from SETTING::src/core.c/Seq.pm6:80  (/home/patrick/rrepos/install/share/perl6/runtime/CORE.c.setting.moarvm:join)                                                                                                                   
 from /home/patrick/repos/Red/lib/Red/Driver/CommonSQL.pm6 (Red::Driver::CommonSQL):370  (/home/patrick/repos/RakuCIBot/.precomp/23A33F92DE051072A0810737FDD2D159EEE174E3/7C/7CB3556581630284BB3680044580D44875B4B0C9:translate)      
 from /home/patrick/repos/Red/lib/Red/Driver/Pg.pm6 (Red::Driver::Pg):48  (/home/patrick/repos/RakuCIBot/.precomp/23A33F92DE051072A0810737FDD2D159EEE174E3/A5/A5685D695871790C8E2180AC1FBA3C11E67DF9A6:)                              
 from /home/patrick/repos/Red/lib/Red/Driver/Pg.pm6 (Red::Driver::Pg):45  (/home/patrick/repos/RakuCIBot/.precomp/23A33F92DE051072A0810737FDD2D159EEE174E3/A5/A5685D695871790C8E2180AC1FBA3C11E67DF9A6:translate)                     
 from /home/patrick/repos/Red/lib/Red/Driver.pm6 (Red::Driver):80  (/home/patrick/repos/RakuCIBot/.precomp/23A33F92DE051072A0810737FDD2D159EEE174E3/85/85B3D6F3F0DA159BD6B56E02934405CD01703FC3:)                                     
 from /home/patrick/repos/Red/lib/Red/Driver.pm6 (Red::Driver):80  (/home/patrick/repos/RakuCIBot/.precomp/23A33F92DE051072A0810737FDD2D159EEE174E3/85/85B3D6F3F0DA159BD6B56E02934405CD01703FC3:prepare)                              
 from /home/patrick/repos/Red/lib/Red/ResultSeq/Iterator.pm6 (Red::ResultSeq::Iterator):14  (/home/patrick/repos/RakuCIBot/.precomp/23A33F92DE051072A0810737FDD2D159EEE174E3/D8/D82482EB277CEADA099A912A41ED56C09CCE564E:TWEAK)       
 from /home/patrick/repos/Red/lib/Red/ResultSeq/Iterator.pm6 (Red::ResultSeq::Iterator):5  (/home/patrick/repos/RakuCIBot/.precomp/23A33F92DE051072A0810737FDD2D159EEE174E3/D8/D82482EB277CEADA099A912A41ED56C09CCE564E:BUILDALL)     
 from SETTING::src/core.c/Mu.pm6:116  (/home/patrick/rrepos/install/share/perl6/runtime/CORE.c.setting.moarvm:new)                                                                                                                    
 from /home/patrick/repos/Red/lib/Red/ResultSeq.pm6 (Red::ResultSeq):89  (/home/patrick/repos/RakuCIBot/.precomp/23A33F92DE051072A0810737FDD2D159EEE174E3/9C/9C0DA19BBA8B4C79099061DFB9EF678A13E561EC:iterator)                       
 from /home/patrick/repos/RakuCIBot/lib/CITestSetManager.rakumod (CITestSetManager):21  (/home/patrick/repos/RakuCIBot/.precomp/23A33F92DE051072A0810737FDD2D159EEE174E3/E8/E8610AE56D925957F38202B48A46A54D897FBAF8:process-worklist)
 from /home/patrick/repos/RakuCIBot/lib/SerialDedup.rakumod (SerialDedup):31  (/home/patrick/repos/RakuCIBot/.precomp/23A33F92DE051072A0810737FDD2D159EEE174E3/B9/B95321DA71D1C1AD00492711BC68C16F473CF6FD:)                          
 from SETTING::src/core.c/Promise.pm6:295  (/home/patrick/rrepos/install/share/perl6/runtime/CORE.c.setting.moarvm:)                                                                                                                  
 from SETTING::src/core.c/ThreadPoolScheduler.pm6:880  (/home/patrick/rrepos/install/share/perl6/runtime/CORE.c.setting.moarvm:)                                                                                                      
 from SETTING::src/core.c/ThreadPoolScheduler.pm6:251  (/home/patrick/rrepos/install/share/perl6/runtime/CORE.c.setting.moarvm:)                                                                                                      
 from SETTING::src/core.c/ThreadPoolScheduler.pm6:245  (/home/patrick/rrepos/install/share/perl6/runtime/CORE.c.setting.moarvm:)                                                                                                       from SETTING::src/core.c/ThreadPoolScheduler.pm6:242  (/home/patrick/rrepos/install/share/perl6/runtime/CORE.c.setting.moarvm:run-one)                                                                                                from SETTING::src/core.c/ThreadPoolScheduler.pm6:284  (/home/patrick/rrepos/install/share/perl6/runtime/CORE.c.setting.moarvm:)                                                                                                      
 from SETTING::src/core.c/Thread.pm6:53  (/home/patrick/rrepos/install/share/perl6/runtime/CORE.c.setting.moarvm:THREAD-ENTRY)
jonathanstowe commented 2 years ago

Seems about right, any chance of a small example that can reproduce for a test?

patrickbkr commented 2 years ago

I'm pretty sure the workaround in the OP should also include the read in the lock.

I'll see if I manage to create a test.

patrickbkr commented 2 years ago

Fixed by #536