镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Commit 04d46c5

Browse files
committed
fix(collections): make shared cursor dispatch one locked transaction (#643)
The unlocked collectionIteratorState probe in the shared next() dispatch raced peer threads' property-lock-guarded cursor-field stores on Linux CI once inlining materialized rare-state contents into the probe. Dispatch now runs a content-free atomic tag pre-check (immutable once published, so hot non-collection iterators never take a lock) and probes plus advances the cursor inside the same property-lock critical section that guards every field store. The ruling is documented on the advance helper: per-iterator property_lock for mutators, backing lock for concurrent trace reads, world-stopped relocation. Adds a no-GIL witness draining Map keys/values/entries and Set values/entries cursors from three threads across mutation rounds and forced GC. TSan: witness 9/9 runs and 19/19 iterator tests with zero race diagnostics.
1 parent 4740df5 commit 04d46c5

3 files changed

Lines changed: 86 additions & 6 deletions

File tree

‎src/context.zig‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27219,6 +27219,59 @@ test "parallel_js: Map iterator cursor advances atomically without GIL" {
2721927219
try std.testing.expect(result.asBool());
2722027220
}
2722127221

27222+
test "parallel_js: shared Map/Set cursors stay exact across kinds, concurrent mutation, and GC" {
27223+
if (builtin.single_threaded) return error.SkipZigTest;
27224+
const ctx = try Context.createWithTestingOptions(std.testing.allocator, .{
27225+
.enable_threads = true,
27226+
.enable_gc = true,
27227+
.parallel_gc = true,
27228+
.parallel_js = true,
27229+
});
27230+
defer ctx.destroy();
27231+
27232+
// Four threads race `next()` over every cursor kind while the main script
27233+
// mutates the live collections and forces young-gen compaction between
27234+
// rounds. Each drained result must be unique (one transaction per entry),
27235+
// exhausted cursors stay idempotent, and post-compaction iteration still
27236+
// observes only live entries.
27237+
const result = try ctx.evaluate(
27238+
\\if ($vm.useThreadGIL() !== false) throw new Error("parallel_js did not drop the thread GIL");
27239+
\\const map = new Map();
27240+
\\const set = new Set();
27241+
\\for (let i = 0; i < 512; i++) { map.set(i, i * 2); set.add(i); }
27242+
\\const cursors = [map.keys(), map.values(), map.entries(), set.values(), set.entries()];
27243+
\\globalThis.drained = [];
27244+
\\function drain(c) {
27245+
\\ let out = "";
27246+
\\ for (;;) {
27247+
\\ const step = c.next();
27248+
\\ if (step.done) break;
27249+
\\ out += step.value + ";";
27250+
\\ }
27251+
\\ return out;
27252+
\\}
27253+
\\function drainAll() {
27254+
\\ let packed = 0;
27255+
\\ for (let k = 0; k < cursors.length; k++) packed += drain(cursors[k]).length;
27256+
\\ return packed;
27257+
\\}
27258+
\\for (let round = 0; round < 3; round++) {
27259+
\\ const threads = [];
27260+
\\ for (let t = 0; t < 3; t++) threads.push(new Thread(drainAll));
27261+
\\ let total = 0;
27262+
\\ for (const thread of threads) total += thread.join();
27263+
\\ if (round === 0 && total <= 0) throw new Error("no concurrent drain output");
27264+
\\ map.set(1000 + round, round);
27265+
\\ set.add(1000 + round);
27266+
\\ if (round === 1) { map.delete(7); set.delete(7); }
27267+
\\ $vm.gc();
27268+
\\}
27269+
\\cursors.every((c) => c.next().done === true) &&
27270+
\\ map.get(1002) === 2 && !set.has(7) && set.has(1000)
27271+
);
27272+
try std.testing.expect(result.asBool());
27273+
}
27274+
2722227275
test "parallel_js: WeakMap and WeakSet identity indexes serialize disjoint mutation" {
2722327276
if (builtin.single_threaded) return error.SkipZigTest;
2722427277
const ctx = try Context.createWithTestingOptions(std.testing.allocator, .{

‎src/interpreter.zig‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34846,18 +34846,25 @@ fn arrayIteratorValue(self: *Interpreter, kind: usize, index: usize, elem: Value
3484634846
};
3484734847
}
3484834848

34849-
/// Map/Set iterator `next` over logical insertion serials. The iterator's
34850-
/// property lock makes concurrent `next()` calls one atomic cursor advance;
34851-
/// the collection's elements lock then snapshots one live result. Both locks
34852-
/// are released before allocating the observable IteratorResult/entry array.
34849+
/// Map/Set iterator `next` over logical insertion serials. The caller holds the
34850+
/// iterator's property lock for the entire inspect-and-advance transaction
34851+
/// (#643): a dispatch probe that read rare-state outside the lock raced peer
34852+
/// threads' locked cursor-field stores once codegen materialized those contents
34853+
/// into the probe. The collection's elements lock then snapshots one live
34854+
/// result under the same critical section, and both locks are released before
34855+
/// allocating the observable IteratorResult/entry array.
34856+
///
34857+
/// Synchronization ruling: every published cursor lives behind its owning
34858+
/// iterator object's `property_lock` (per-activation writers, peer iterators
34859+
/// unaffected); GC trace reads `source` through the backing lock and relocation
34860+
/// rewrites it only at a world-stopped commit.
3485334861
fn collectionCursorIterNext(self: *Interpreter, iterator: *value.Object) value.HostError!Value {
3485434862
var done = false;
3485534863
var kind: u8 = 0;
3485634864
var entry_key: Value = Value.undef();
3485734865
var entry_value: Value = Value.undef();
3485834866
var is_map = false;
3485934867

34860-
iterator.lockProperties();
3486134868
const cursor = iterator.collectionIteratorState() orelse {
3486234869
iterator.unlockProperties();
3486334870
return self.throwError("TypeError", "next called on an incompatible receiver");
@@ -34936,7 +34943,17 @@ fn cursorIterNext(ctx: *anyopaque, this: Value, args: []const Value) value.HostE
3493634943
const self: *Interpreter = @ptrCast(@alignCast(ctx));
3493734944
if (!this.isObject()) return self.throwError("TypeError", "next called on non-object");
3493834945
const o = this.asObj();
34939-
if (o.collectionIteratorState() != null) return collectionCursorIterNext(self, o);
34946+
// Inspecting and advancing a shared collection cursor is one atomic logical
34947+
// transaction (#643): the rare-state probe runs inside the same property
34948+
// lock critical section that guards every cursor-field store, so no
34949+
// toolchain can hoist a torn content read into the unlocked dispatch. The
34950+
// content-free tag pre-check keeps non-collection iterators (the hot array
34951+
// case) off the lock entirely; the tag is immutable once published.
34952+
if (o.hasCollectionIterator()) {
34953+
o.lockProperties();
34954+
if (o.collectionIteratorState() != null) return collectionCursorIterNext(self, o);
34955+
o.unlockProperties();
34956+
}
3494034957
// Brand check: a genuine Array/Map/Set/String iterator carries its `__src`
3494134958
// cursor slot as an OWN property. A bare object, a boxed primitive, the
3494234959
// iterator *prototype* itself, or `Object.create(anIterator)` (which only

‎src/value.zig‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3090,6 +3090,16 @@ pub const Object = struct {
30903090
return &cold.rare.collection_iterator;
30913091
}
30923092

3093+
/// Content-free cursor presence probe (#643). Reads only the atomically
3094+
/// published cold pointer and the immutable rare tag — never the cursor
3095+
/// fields themselves — so dispatch can skip the property-lock transaction
3096+
/// for non-collection iterators without re-introducing an unlocked read of
3097+
/// state that peer threads mutate behind that lock.
3098+
pub inline fn hasCollectionIterator(self: *const Object) bool {
3099+
const cold = self.coldState() orelse return false;
3100+
return cold.hasRare(.collection_iterator);
3101+
}
3102+
30933103
/// GC-visible Object edges out of the WebAssembly rare states (issue #141).
30943104
/// At most one wasm rare tag is active per object, so a single flat
30953105
/// snapshot covers all of them; null/empty fields mark nothing.

0 commit comments

Comments
 (0)