Skip to content

Commit e9b538b

Browse files
byroottagomoris
authored andcommitted
Fix namespace_initialize to not crash on boot
``` /opt/rubies/head-namespaces/bin/ruby(sigsegv+0x84) [0x104e897e8] /usr/lib/system/libsystem_platform.dylib(_sigtramp+0x38) [0x18de56de4] /opt/rubies/head-namespaces/bin/ruby(object_id+0x80) [0x104d7d420] /opt/rubies/head-namespaces/bin/ruby(object_id+0x80) [0x104d7d420] /opt/rubies/head-namespaces/bin/ruby(rb_initialize_main_namespace+0xe4) [0x104ddaa20] /opt/rubies/head-namespaces/bin/ruby(ruby_opt_init+0x120) [0x104e7f524] /opt/rubies/head-namespaces/bin/ruby(ruby_process_options+0x1370) [0x104e7e31c] /opt/rubies/head-namespaces/bin/ruby(ruby_options+0xb0) [0x104d69844] /opt/rubies/head-namespaces/bin/ruby(main+0x64) [0x104ca8d54] ``` I'm not too sure why `rb_obj_id` crashes, but I suspect it's called too early. Either way I don't think generating an object_id for namespaces is a good idea. If all we need is a unique number we can do that for much cheaper.
1 parent b132322 commit e9b538b

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

namespace.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,20 @@ rb_current_namespace_details(VALUE opt)
378378
return str;
379379
}
380380

381+
static long namespace_id_counter = 0;
382+
383+
static long
384+
namespace_generate_id(void)
385+
{
386+
long id;
387+
RB_VM_LOCK_ENTER();
388+
{
389+
id = ++namespace_id_counter;
390+
}
391+
RB_VM_LOCK_LEAVE();
392+
return id;
393+
}
394+
381395
static void
382396
namespace_entry_initialize(rb_namespace_t *ns)
383397
{
@@ -527,7 +541,7 @@ namespace_initialize(VALUE namespace)
527541
ns = get_namespace_struct_internal(entry);
528542

529543
ns->ns_object = namespace;
530-
ns->ns_id = NUM2LONG(rb_obj_id(namespace));
544+
ns->ns_id = namespace_generate_id();
531545
ns->load_path = rb_ary_dup(GET_VM()->load_path);
532546
ns->is_user = true;
533547
rb_define_singleton_method(ns->load_path, "resolve_feature_path", rb_resolve_feature_path, 1);
@@ -981,7 +995,7 @@ rb_initialize_main_namespace(void)
981995
main_ns = rb_class_new_instance_pass_kw(0, NULL, rb_cNamespace);
982996
ns = rb_get_namespace_t(main_ns);
983997
ns->ns_object = main_ns;
984-
ns->ns_id = NUM2LONG(rb_obj_id(main_ns));
998+
ns->ns_id = namespace_generate_id();
985999
ns->is_builtin = false;
9861000
ns->is_user = true;
9871001
ns->is_optional = false;

0 commit comments

Comments
 (0)