Skip to content

Prevent copying thread-attachment and GC-free-zone ownership guards #227

Closed

Description

@dimensionscape

hx::NativeAttach and hx::AutoGCFreeZone are scope owners but permit implicit copying and copy assignment. A copy duplicates the ownership flag without performing another attach or GC-free-zone entry, while both destructors still perform cleanup.

Regression coverage first: test/regression/GcScopeOwnership in the existing Main.hx/build.hxml/stdout.txt layout. Check copy construction and copy assignment through std::type_traits. These ownership guards should prevent accidental copying. Keep explicit move or ownership-transfer behavior a separate design choice.

Confirmed on Linux x86_64 with Haxe 4.3.3:

NativeAttach prevents copying: false
AutoGCFreeZone prevents copying: false

A separate native probe exercised a copied NativeAttach on the already-attached main thread:

int before = hx::GcGetThreadAttachedCount();
{
    hx::NativeAttach original;
    { hx::NativeAttach duplicate = original; }
    // Query attachment count while original is still alive.
}
// Query again after original is destroyed.

The observed counts were 1, 1, 0. They should have been 1, 2, 1 if the original guard's attachment remains owned until its destructor. The probe restored the missing attachment count before returning and did not perform a GC allocation while the counter was unbalanced.

include/hx/Native.h stores isAttached on NativeAttach. Its compiler-generated copy operations copy that flag, and both destructors call detach. On a temporarily attached native thread this can release registration while an outer guard is still alive, removing its stack from GC scanning too early. On an already-attached thread the same copy corrupts the nesting count.

include/hx/GC.h has the corresponding ownership problem in AutoGCFreeZone: copying locked allows multiple exits for one entry. That class's copyability was verified, but this report does not claim a reproduced zone-mismatch crash. Copy assignment can also overwrite an existing ownership state without releasing it.

Delete copy construction and copy assignment for both classes, or implement explicitly defined ownership semantics. If moves are supported, clear the source's ownership and test destruction after transfer. Keep normal nested independently constructed guards working, and verify exception unwinding restores attachment and GC-zone state.

This affects native integrations that copy these C++ helpers, not normal Haxe value assignment. No runtime implementation changes are included.

Activity

  1. added a commit that references this issue on Oct 20, 2026
    49cb161
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