@@ -1873,9 +1873,8 @@ make_shareable_check_shareable(VALUE obj)
18731873 else if (!allow_frozen_shareable_p (obj )) {
18741874 if (!RB_TYPE_P (obj , T_DATA )) {
18751875 if (rb_ractor_isolation_check_p ()) {
1876- rb_category_warn (RB_WARN_CATEGORY_RACTOR_ISOLATION ,
1877- "can not make shareable object of class %+" PRIsVALUE ,
1878- rb_class_of (obj ));
1876+ rb_ractor_isolation_violation ("can not make shareable object of class %+" PRIsVALUE ,
1877+ rb_class_of (obj ));
18791878 return traverse_stop ;
18801879 }
18811880 rb_raise (rb_eRactorError ,
@@ -1888,9 +1887,8 @@ make_shareable_check_shareable(VALUE obj)
18881887 return traverse_skip ;
18891888 }
18901889 else if (rb_ractor_isolation_check_p ()) {
1891- rb_category_warn (RB_WARN_CATEGORY_RACTOR_ISOLATION ,
1892- "can not make shareable object of class %+" PRIsVALUE
1893- " because it refers unshareable objects" , rb_class_of (obj ));
1890+ rb_ractor_isolation_violation ("can not make shareable object of class %+" PRIsVALUE
1891+ " because it refers unshareable objects" , rb_class_of (obj ));
18941892 return traverse_stop ;
18951893 }
18961894 else {
@@ -1903,9 +1901,8 @@ make_shareable_check_shareable(VALUE obj)
19031901 return rb_ractor_shareable_p (obj ) ? traverse_cont : traverse_stop ;
19041902 }
19051903 else if (rb_ractor_isolation_check_p ()) {
1906- rb_category_warn (RB_WARN_CATEGORY_RACTOR_ISOLATION ,
1907- "can not make shareable object of class %+" PRIsVALUE ,
1908- rb_class_of (obj ));
1904+ rb_ractor_isolation_violation ("can not make shareable object of class %+" PRIsVALUE ,
1905+ rb_class_of (obj ));
19091906 return traverse_stop ;
19101907 }
19111908 else {
@@ -4296,7 +4293,8 @@ static const struct st_hash_type isolation_warn_hash_type = {
42964293static bool
42974294isolation_warnings_enabled_p (void )
42984295{
4299- return !NIL_P (ruby_verbose ) && rb_warning_category_enabled_p (RB_WARN_CATEGORY_RACTOR_ISOLATION );
4296+ return !GET_EC ()-> ractor_isolation_warning && !NIL_P (ruby_verbose ) &&
4297+ rb_warning_category_enabled_p (RB_WARN_CATEGORY_RACTOR_ISOLATION );
43004298}
43014299
43024300// nearest Ruby frame outside <internal:...>, so Ractor.new and Port#<< report the app site
@@ -4354,20 +4352,39 @@ isolation_warn_first_p(VALUE message, VALUE path, int line)
43544352 return first ;
43554353}
43564354
4357- void
4358- rb_ractor_isolation_warn (VALUE message )
4355+ static VALUE
4356+ isolation_warn_emit (VALUE message )
43594357{
4360- if (!isolation_warnings_enabled_p ()) return ;
4361-
43624358 int line ;
43634359 VALUE path = isolation_source_location (& line );
43644360 StringValueCStr (message );
4365- if (ruby_ractor_isolation_enabled < 2 && !isolation_warn_first_p (message , path , line )) return ;
4361+ if (ruby_ractor_isolation_enabled < 2 && !isolation_warn_first_p (message , path , line )) return Qnil ;
43664362
43674363 const char * file = NIL_P (path ) ? NULL : RSTRING_PTR (path );
43684364 rb_category_compile_warn (RB_WARN_CATEGORY_RACTOR_ISOLATION , file , line , "%s" , RSTRING_PTR (message ));
43694365 RB_GC_GUARD (message );
43704366 RB_GC_GUARD (path );
4367+ return Qnil ;
4368+ }
4369+
4370+ static VALUE
4371+ isolation_warn_end (VALUE ec_ptr )
4372+ {
4373+ ((rb_execution_context_t * )ec_ptr )-> ractor_isolation_warning = false;
4374+ return Qnil ;
4375+ }
4376+
4377+ void
4378+ rb_ractor_isolation_warn (VALUE message )
4379+ {
4380+ if (!isolation_warnings_enabled_p ()) return ;
4381+
4382+ // A user-defined warning hook can itself violate isolation, including
4383+ // before entering its body when defined with define_singleton_method.
4384+ // Keep the guard fiber-local and restore it even if the hook raises.
4385+ rb_execution_context_t * ec = GET_EC ();
4386+ ec -> ractor_isolation_warning = true;
4387+ rb_ensure (isolation_warn_emit , message , isolation_warn_end , (VALUE )ec );
43714388}
43724389
43734390void
0 commit comments