Reading an object's fields while another thread adds fields can return wrong values
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
- Domain
- operating-systems
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from HaxeFoundation/neko
-
Difficulty 4/5 3-5 days Newbie friendliness 42/100
HaxeFoundation/neko#334 · 2 comments ·
-
year 2038-problemMay be free again A pull request for this issue was closed without being merged. Open
Difficulty 3/5 1-2 days Newbie friendliness 52/100
HaxeFoundation/neko#319 ·
-
Incorrect api used for sys_read_dir on windowsPossibly taken @tobil4sk claimed this 335 days ago. Openbug
Difficulty 4/5 3-5 days Newbie friendliness 45/100
HaxeFoundation/neko#315 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
HaxeFoundation/neko#314 ·
-
JIT don't workOpen
Difficulty 4/5 3-5 days Newbie friendliness 25/100
HaxeFoundation/neko#301 · 1 comment ·
All issues in HaxeFoundation/neko
Similar issues
-
bug needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
awslabs/aws-c-common#1316 ·
-
parsedate: two-digit year "70" is read as 2070, so cookies deleted with expires=...01-Jan-70... are keptPossibly taken @lazerg claimed this today. Open
Difficulty 1/5 Under an hour Newbie friendliness 88/100
Maintainers usually reply within 1 day
-
bug game: 0100FF500E34A000 game: 0400FF500E34A001 mac packing
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 2 days
-
clang:frontend clang:openmp crash
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
llvm/llvm-project#245442 · 1 comment ·
Maintainers usually reply within 1 day
-
bug 🐞
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
valkey-io/valkey-glide#7848 ·
Maintainers usually reply within 1 day