ruby-changes:64416
From: Koichi <ko1@a...>
Date: Mon, 21 Dec 2020 22:29:26 +0900 (JST)
Subject: [ruby-changes:64416] dca6752fec (master): Introduce Ractor::IsolationError
https://git.ruby-lang.org/ruby.git/commit/?id=dca6752fec From dca6752fecc6733575145185764d7b6a218cee96 Mon Sep 17 00:00:00 2001 From: Koichi Sasada <ko1@a...> Date: Mon, 21 Dec 2020 18:06:28 +0900 Subject: Introduce Ractor::IsolationError Ractor has several restrictions to keep each ractor being isolated and some operation such as `CONST="foo"` in non-main ractor raises an exception. This kind of operation raises an error but there is confusion (some code raises RuntimeError and some code raises NameError). To make clear we introduce Ractor::IsolationError which is raised when the isolation between ractors is violated. diff --git a/ractor.c b/ractor.c index 8c4f06a..b856419 100644 --- a/ractor.c +++ b/ractor.c @@ -18,18 +18,14 @@ https://github.com/ruby/ruby/blob/trunk/ractor.c#L18 #include "transient_heap.h" VALUE rb_cRactor; + +VALUE rb_eRactorUnsafeError; +VALUE rb_eRactorIsolationError; static VALUE rb_eRactorError; static VALUE rb_eRactorRemoteError; static VALUE rb_eRactorMovedError; static VALUE rb_eRactorClosedError; static VALUE rb_cRactorMovedObject; -VALUE rb_eRactorUnsafeError; - -VALUE -rb_ractor_error_class(void) -{ - return rb_eRactorError; -} static void vm_ractor_blocking_cnt_inc(rb_vm_t *vm, rb_ractor_t *r, const char *file, int line); @@ -2023,11 +2019,12 @@ void https://github.com/ruby/ruby/blob/trunk/ractor.c#L2019 Init_Ractor(void) { rb_cRactor = rb_define_class("Ractor", rb_cObject); - rb_eRactorError = rb_define_class_under(rb_cRactor, "Error", rb_eRuntimeError); - rb_eRactorRemoteError = rb_define_class_under(rb_cRactor, "RemoteError", rb_eRactorError); - rb_eRactorMovedError = rb_define_class_under(rb_cRactor, "MovedError", rb_eRactorError); - rb_eRactorClosedError = rb_define_class_under(rb_cRactor, "ClosedError", rb_eStopIteration); - rb_eRactorUnsafeError = rb_define_class_under(rb_cRactor, "UnsafeError", rb_eRactorError); + rb_eRactorError = rb_define_class_under(rb_cRactor, "Error", rb_eRuntimeError); + rb_eRactorIsolationError = rb_define_class_under(rb_cRactor, "IsolationError", rb_eRactorError); + rb_eRactorRemoteError = rb_define_class_under(rb_cRactor, "RemoteError", rb_eRactorError); + rb_eRactorMovedError = rb_define_class_under(rb_cRactor, "MovedError", rb_eRactorError); + rb_eRactorClosedError = rb_define_class_under(rb_cRactor, "ClosedError", rb_eStopIteration); + rb_eRactorUnsafeError = rb_define_class_under(rb_cRactor, "UnsafeError", rb_eRactorError); rb_cRactorMovedObject = rb_define_class_under(rb_cRactor, "MovedObject", rb_cBasicObject); rb_undef_alloc_func(rb_cRactorMovedObject); diff --git a/variable.c b/variable.c index b780446..74a7f59 100644 --- a/variable.c +++ b/variable.c @@ -349,7 +349,7 @@ rb_find_global_entry(ID id) https://github.com/ruby/ruby/blob/trunk/variable.c#L349 } if (UNLIKELY(!rb_ractor_main_p()) && (!entry || !entry->ractor_local)) { - rb_raise(rb_eRuntimeError, "can not access global variables %s from non-main Ractors", rb_id2name(id)); + rb_raise(rb_eRactorIsolationError, "can not access global variables %s from non-main Ractors", rb_id2name(id)); } return entry; @@ -814,7 +814,7 @@ rb_f_global_variables(void) https://github.com/ruby/ruby/blob/trunk/variable.c#L814 VALUE sym, backref = rb_backref_get(); if (!rb_ractor_main_p()) { - rb_raise(rb_eRuntimeError, "can not access global variables from non-main Ractors"); + rb_raise(rb_eRactorIsolationError, "can not access global variables from non-main Ractors"); } rb_id_table_foreach(rb_global_tbl, gvar_i, (void *)ary); @@ -847,7 +847,7 @@ rb_alias_variable(ID name1, ID name2) https://github.com/ruby/ruby/blob/trunk/variable.c#L847 struct rb_id_table *gtbl = rb_global_tbl; if (!rb_ractor_main_p()) { - rb_raise(rb_eRuntimeError, "can not access global variables from non-main Ractors"); + rb_raise(rb_eRactorIsolationError, "can not access global variables from non-main Ractors"); } entry2 = rb_global_entry(name2); @@ -907,14 +907,14 @@ IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(ID id) https://github.com/ruby/ruby/blob/trunk/variable.c#L907 { if (UNLIKELY(!rb_ractor_main_p())) { if (rb_is_instance_id(id)) { // check only normal ivars - rb_raise(rb_eRuntimeError, "can not access instance variables of classes/modules from non-main Ractors"); + rb_raise(rb_eRactorIsolationError, "can not access instance variables of classes/modules from non-main Ractors"); } } } #define CVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR() \ if (UNLIKELY(!rb_ractor_main_p())) { \ - rb_raise(rb_eRuntimeError, "can not access class variables from non-main Ractors"); \ + rb_raise(rb_eRactorIsolationError, "can not access class variables from non-main Ractors"); \ } static inline struct st_table * @@ -927,8 +927,7 @@ generic_ivtbl(VALUE obj, ID id, bool force_check_ractor) https://github.com/ruby/ruby/blob/trunk/variable.c#L927 UNLIKELY(!rb_ractor_main_p()) && UNLIKELY(rb_ractor_shareable_p(obj))) { - // TODO: RuntimeError? - rb_raise(rb_eRuntimeError, "can not access instance variables of shareable objects from non-main Ractors"); + rb_raise(rb_eRactorIsolationError, "can not access instance variables of shareable objects from non-main Ractors"); } return generic_iv_tbl_; } @@ -2552,7 +2551,7 @@ rb_const_get_0(VALUE klass, ID id, int exclude, int recurse, int visibility) https://github.com/ruby/ruby/blob/trunk/variable.c#L2551 if (c != Qundef) { if (UNLIKELY(!rb_ractor_main_p())) { if (!rb_ractor_shareable_p(c)) { - rb_raise(rb_eNameError, "can not access non-shareable objects in constant %"PRIsVALUE"::%s by non-main Ractor.", rb_class_path(klass), rb_id2name(id)); + rb_raise(rb_eRactorIsolationError, "can not access non-shareable objects in constant %"PRIsVALUE"::%s by non-main Ractor.", rb_class_path(klass), rb_id2name(id)); } } return c; @@ -3011,7 +3010,7 @@ rb_const_set(VALUE klass, ID id, VALUE val) https://github.com/ruby/ruby/blob/trunk/variable.c#L3010 } if (!rb_ractor_main_p() && !rb_ractor_shareable_p(val)) { - rb_raise(rb_eNameError, "can not set constants with non-shareable objects by non-main Ractors"); + rb_raise(rb_eRactorIsolationError, "can not set constants with non-shareable objects by non-main Ractors"); } check_before_mod_set(klass, id, val, "constant"); diff --git a/vm.c b/vm.c index ee28a2d..9ed358f 100644 --- a/vm.c +++ b/vm.c @@ -994,7 +994,6 @@ collect_outer_variable_names(ID id, VALUE val, void *ptr) https://github.com/ruby/ruby/blob/trunk/vm.c#L994 return ID_TABLE_CONTINUE; } -VALUE rb_ractor_error_class(void); VALUE rb_ractor_make_shareable(VALUE obj); static const rb_env_t * @@ -1015,7 +1014,7 @@ env_copy(const VALUE *src_ep, VALUE read_only_variables) https://github.com/ruby/ruby/blob/trunk/vm.c#L1014 if (id == src_env->iseq->body->local_table[j]) { VALUE v = src_env->env[j]; if (!rb_ractor_shareable_p(v)) { - rb_raise(rb_ractor_error_class(), + rb_raise(rb_eRactorIsolationError, "can not make shareable Proc because it can refer unshareable object %" PRIsVALUE" from variable `%s'", rb_inspect(v), rb_id2name(id)); } diff --git a/vm_core.h b/vm_core.h index 8540f8d..453c84e 100644 --- a/vm_core.h +++ b/vm_core.h @@ -2011,6 +2011,7 @@ extern void rb_reset_coverages(void); https://github.com/ruby/ruby/blob/trunk/vm_core.h#L2011 void rb_postponed_job_flush(rb_vm_t *vm); extern VALUE rb_eRactorUnsafeError; // ractor.c +extern VALUE rb_eRactorIsolationError; RUBY_SYMBOL_EXPORT_END diff --git a/vm_insnhelper.c b/vm_insnhelper.c index 6c7ff59..b1673db 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -1002,7 +1002,7 @@ vm_get_ev_const(rb_execution_context_t *ec, VALUE orig_klass, ID id, bool allow_ https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L1002 else { if (UNLIKELY(!rb_ractor_main_p())) { if (!rb_ractor_shareable_p(val)) { - rb_raise(rb_eNameError, + rb_raise(rb_eRactorIsolationError, "can not access non-shareable objects in constant %"PRIsVALUE"::%s by non-main ractor.", rb_class_path(klass), rb_id2name(id)); } } -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/