From dca6752fecc6733575145185764d7b6a218cee96 Mon Sep 17 00:00:00 2001 From: Koichi Sasada 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. --- ractor.c | 21 +++++++++------------ variable.c | 17 ++++++++--------- vm.c | 3 +-- vm_core.h | 1 + vm_insnhelper.c | 2 +- 5 files changed, 20 insertions(+), 24 deletions(-) diff --git a/ractor.c b/ractor.c index 8c4f06a6b0..b8564199d5 100644 --- a/ractor.c +++ b/ractor.c @@ -18,18 +18,14 @@ #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 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 b780446f19..74a7f59790 100644 --- a/variable.c +++ b/variable.c @@ -349,7 +349,7 @@ rb_find_global_entry(ID id) } 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) 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) 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) { 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) 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) 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) } 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 ee28a2d4ad..9ed358f73d 100644 --- a/vm.c +++ b/vm.c @@ -994,7 +994,6 @@ collect_outer_variable_names(ID id, VALUE val, void *ptr) 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) 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 8540f8d1de..453c84e394 100644 --- a/vm_core.h +++ b/vm_core.h @@ -2011,6 +2011,7 @@ extern void rb_reset_coverages(void); 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 6c7ff594c2..b1673db067 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_ 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 v1.2.3