Skip to content

Reading an object's fields while another thread adds fields can return wrong values #335

Open

Description

@Simn

Reading a field of an object while another thread adds a field to the same object can return a wrong value. Adding a field can reallocate the object's field table, and a concurrent reader can then see an inconsistent table.

This matters for Haxe-generated code: a declared instance field that the constructor doesn't assign only exists once it is first assigned. So code that shares an object with another thread and assigns one of its declared fields later can make that thread read wrong values, even for fields that never change.

Repro

import haxe.atomic.AtomicInt;
import sys.thread.Thread;

// One field is set in the constructor, the others are declared but only assigned later,
// by another thread, while the object is already shared.
class Shared {
	public var stable:String;
	public var a:Null<Int>;
	public var b:Null<Int>;
	public var c:Null<Int>;
	public var d:Null<Int>;
	public var e:Null<Int>;
	public var f:Null<Int>;
	public var g:Null<Int>;
	public var h:Null<Int>;

	public function new() {
		stable = "here";
	}
}

function main() {
	final objects = 200000;
	final shared:Array<Null<Shared>> = [for (_ in 0...objects) null];
	final published = new AtomicInt(0);
	final done = new AtomicInt(0);
	var badReads = 0;
	// The writer creates objects, publishes each one, then assigns its declared fields.
	Thread.create(() -> {
		for (i in 0...objects) {
			final o = new Shared();
			shared[i] = o;
			published.store(i + 1);
			o.a = 1;
			o.b = 2;
			o.c = 3;
			o.d = 4;
			o.e = 5;
			o.f = 6;
			o.g = 7;
			o.h = 8;
		}
		done.store(1);
	});
	// The reader keeps reading the constructor's field of the most recently published object.
	while (done.load() == 0) {
		final n = published.load();
		if (n > 0) {
			final o = shared[n - 1];
			if (o != null && o.stable != "here") {
				badReads++;
			}
		}
	}
	Sys.println('objects: $objects, bad reads: $badReads');
}

Build it with haxe --main Main --neko out.n and run it with neko out.n.

Results

With neko 2.4.1 on Windows x64, this reports between 188 and 232 bad reads in each of five runs. stable is only assigned in the constructor, so it should never read as anything but "here".

If the constructor also assigns the other fields, with a = b = c = d = e = f = g = h = null;, the result is 0 bad reads in all five runs. Once every field exists, later assignments only update existing slots, so the reader never sees a table being reallocated.

A variant that keeps adding fields to one shared object with Reflect.setField while another thread reads a field that never changes shows the same thing: about 120 bad reads in 1.4 million.

Context

We ran into this in hxcoro, where the thread pool shares continuation objects between threads. On CI, it showed up as an Invalid field access on macOS arm64 and Windows x64, and we also saw a segfault that is consistent with the same race. For now, hxcoro skips the affected test on neko and points here (HaxeFoundation/hxcoro#192).

We don't plan to change neko for this right now, but want it on record.

Possible ways around it:

  • In the Haxe neko generator: assign every declared instance field in the constructor, so that an object's set of fields is fixed once it's constructed.
  • In the VM: make adding a field safe for concurrent readers.

🤖 Generated with Claude Code

Activity

  1. tobil4sk commented on Nov 1, 2026

    @tobil4sk
    Member

    According to: #149 (comment)

    PS: if you're doing concurrent access to objects, this is not allowed in Neko

    In the VM: make adding a field safe for concurrent readers.

    It seems there would not be an easy way of making the field addition safe without having a mutex around each field access? The generator change sounds reasonable though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions