Skip to content

Commit 1435ea7

Browse files
committed
Add missing lock for Module#remove_instance_variable
We must take a lock to ensure another ractor isn't reading the ivars while we're moving them.
1 parent 52da5f8 commit 1435ea7

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

variable.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,9 +1537,13 @@ rb_ivar_delete(VALUE obj, ID id, VALUE undef)
15371537
{
15381538
rb_check_frozen(obj);
15391539

1540+
bool locked = false;
1541+
unsigned int lev = 0;
15401542
VALUE val = undef;
15411543
if (BUILTIN_TYPE(obj) == T_CLASS || BUILTIN_TYPE(obj) == T_MODULE) {
15421544
IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(id);
1545+
RB_VM_LOCK_ENTER_LEV(&lev);
1546+
locked = true;
15431547
}
15441548

15451549
shape_id_t old_shape_id = rb_obj_shape_id(obj);
@@ -1551,6 +1555,9 @@ rb_ivar_delete(VALUE obj, ID id, VALUE undef)
15511555
shape_id_t next_shape_id = rb_shape_transition_remove_ivar(obj, id, &removed_shape_id);
15521556

15531557
if (next_shape_id == old_shape_id) {
1558+
if (locked) {
1559+
RB_VM_LOCK_LEAVE_LEV(&lev);
1560+
}
15541561
return undef;
15551562
}
15561563

@@ -1600,6 +1607,10 @@ rb_ivar_delete(VALUE obj, ID id, VALUE undef)
16001607
}
16011608
rb_shape_set_shape_id(obj, next_shape_id);
16021609

1610+
if (locked) {
1611+
RB_VM_LOCK_LEAVE_LEV(&lev);
1612+
}
1613+
16031614
return val;
16041615

16051616
too_complex:
@@ -1630,6 +1641,11 @@ rb_ivar_delete(VALUE obj, ID id, VALUE undef)
16301641
}
16311642
}
16321643
}
1644+
1645+
if (locked) {
1646+
RB_VM_LOCK_LEAVE_LEV(&lev);
1647+
}
1648+
16331649
return val;
16341650
}
16351651

0 commit comments

Comments
 (0)