From 693541c1820008e69eb726e865f47bf26cbe55d8 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 15 Apr 2026 16:14:21 +0300 Subject: [PATCH] cgen: fix v generated shared .so crash on dlclose (fixes #15657) --- vlib/v/gen/c/cgen.v | 6 ++++++ vlib/v/gen/c/testdata/boehm_shared_startup.c.must_have | 1 + vlib/v/gen/c/testdata/boehm_shared_startup.vv | 3 +++ 3 files changed, 10 insertions(+) create mode 100644 vlib/v/gen/c/testdata/boehm_shared_startup.c.must_have create mode 100644 vlib/v/gen/c/testdata/boehm_shared_startup.vv diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 4b5b0bb69..68cf40e11 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -9239,6 +9239,12 @@ fn (mut g Gen) write_init_function() { g.writeln('\tv__trace_calls__on_call(_S("_vinit"));') } + if g.pref.is_shared + && g.pref.gc_mode in [.boehm_full, .boehm_incr, .boehm_full_opt, .boehm_incr_opt, .boehm_leak] { + // Shared libraries can be unloaded while Boehm marker threads are still alive. + g.writeln('\tGC_set_markers_count(1);') + } + if g.use_segfault_handler && !g.pref.is_shared { if _ := g.table.fns['v_segmentation_fault_handler'] { // 11 is SIGSEGV. It is hardcoded here, to avoid FreeBSD compilation errors for trivial examples. diff --git a/vlib/v/gen/c/testdata/boehm_shared_startup.c.must_have b/vlib/v/gen/c/testdata/boehm_shared_startup.c.must_have new file mode 100644 index 000000000..532b2ea21 --- /dev/null +++ b/vlib/v/gen/c/testdata/boehm_shared_startup.c.must_have @@ -0,0 +1 @@ +GC_set_markers_count(1); diff --git a/vlib/v/gen/c/testdata/boehm_shared_startup.vv b/vlib/v/gen/c/testdata/boehm_shared_startup.vv new file mode 100644 index 000000000..7704bbf68 --- /dev/null +++ b/vlib/v/gen/c/testdata/boehm_shared_startup.vv @@ -0,0 +1,3 @@ +// vtest vflags: -gc boehm -shared +@[export: 'noop'] +pub fn noop() {} -- 2.39.5