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

Open
#335 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
c

Research direction

Start by reproducing the race with the provided Haxe program, compiled using haxe --main Main --neko out.n and run with neko out.n. The issue identifies concurrent reads during field table reallocation in the Neko VM and suggests either changing the Haxe Neko generator or making VM field addition safe. Determine which approach maintainers want before estimating completion; done means the reproduction no longer reports incorrect reads.

Written by the indexing model from the issue text.

Description

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

Dominant language
C
Stars
592
Forks
111
Avg merge
5d 1h
Merged PRs (30d)
1

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from HaxeFoundation/neko

All issues in HaxeFoundation/neko

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.