Skip to content

Commit

Permalink
Make WeakKeyMap safe for compaction during allocation
Browse files Browse the repository at this point in the history
During allocation, the table may not have been allocated yet which would
crash in the st_foreach.
  • Loading branch information
peterzhu2118 committed Dec 12, 2023
1 parent 33cf8f6 commit 185b7e9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 4 additions & 0 deletions test/ruby/test_weakkeymap.rb
Expand Up @@ -123,6 +123,10 @@ def test_compaction
end;
end

def test_gc_compact_stress
EnvUtil.under_gc_compact_stress { ObjectSpace::WeakKeyMap.new }
end

private

def assert_weak_include(m, k, n = 100)
Expand Down
4 changes: 3 additions & 1 deletion weakmap.c
Expand Up @@ -609,7 +609,9 @@ wkmap_compact(void *ptr)
{
struct weakkeymap *w = ptr;

st_foreach_with_replace(w->table, wkmap_compact_table_i, wkmap_compact_table_replace, (st_data_t)0);
if (w->table) {
st_foreach_with_replace(w->table, wkmap_compact_table_i, wkmap_compact_table_replace, (st_data_t)0);
}
}

static const rb_data_type_t weakkeymap_type = {
Expand Down

0 comments on commit 185b7e9

Please sign in to comment.