Repository navigation
Closed
Description
Config: UBSan.
Test:
iree-run-mlir --Xcompiler,iree-input-type=stablehlo --Xcompiler,iree-hal-target-device=local --Xcompiler,iree-hal-local-target-device-backends=vmvx /__w/iree/iree/tests/e2e/stablehlo_models/fragment_000.mlir
Stack:
#7 0x00005648f745b649 iree_abort /__w/iree/iree/runtime/src/iree/base/assert.h:26:63
#8 0x00005648f745b5c0 iree_vm_buffer_deinitialize /__w/iree/iree/runtime/src/iree/vm/buffer.c:79:3
#9 0x00005648f73810b3 iree_vm_bytecode_module_destroy /__w/iree/iree/runtime/src/iree/vm/bytecode/module.c:171:49
#10 0x00005648f74617b2 iree_vm_context_release_modules /__w/iree/iree/runtime/src/iree/vm/context.c:0:7
#11 0x00005648f745dfc7 iree_vm_context_destroy /__w/iree/iree/runtime/src/iree/vm/context.c:476:16
#12 0x00005648f709325d iree_tooling_run_module_with_data /__w/iree/iree/runtime/src/iree/tooling/run_module.c:476:7
The code at iree/runtime/src/iree/vm/buffer.c:79 is:
iree/runtime/src/iree/vm/buffer.c
Line 79 in fcbd569
| iree_atomic_ref_count_abort_if_uses(&buffer->ref_object.counter); |
So apparently we take the abort branch here:
iree/runtime/src/iree/base/internal/atomics.h
Lines 139 to 148 in fcbd569
| // Aborts the program if the given reference count value is not 1. | |
| // This should be avoided in all situations but those where continuing execution | |
| // would be invalid. If a reference object is allocated on the stack and the | |
| // parent function is about to return it *must* have a ref count of 1: anything | |
| // else that may be retaining the object will hold a pointer to (effectively) | |
| // uninitialized stack memory. | |
| #define iree_atomic_ref_count_abort_if_uses(count_ptr) \ | |
| if (IREE_UNLIKELY(iree_atomic_ref_count_load(count_ptr) != 1)) { \ | |
| iree_abort(); \ | |
| } |
Activity
bjacob commentedon Apr 30, 2026
FYI @ScottTodd
bjacob commentedon Apr 30, 2026
Anyone debugging this: try reproducing this locally by running many times the test command in the issue description. Try to reproduce this with IREE_ENABLE_TSAN=ON, build type RelWithDebInfo.
bjacob commentedon May 1, 2026
Claude-generated diagnostic
Symptom
Running
intermittently aborts with
iree_abortfromiree_atomic_ref_count_abort_if_usesiniree_vm_buffer_deinitializeat module-destroy time. Stack:Output already shows the correct numerical result before aborting — the abort is during teardown only.
Identifying the leaked buffer
Patched
iree_vm_buffer_deinitializeto print buffer details before aborting. Captured under chaos:size=192is the_constcomposite rodata (5×5×f32 = 100 bytes + 5×1×f32 = 20 bytes + padding = 192).head[0..3] = 0x405dc265 = 3.464990matches the first element of the 5×5 matrix constant.counter=2: under-release. Someone still holds a ref at module-destroy time.Root cause
Use-after-signal in the local-task queue's operation-completion path, not a refcount race.
iree_hal_task_queue_op_completeatruntime/src/iree/hal/drivers/local_task/task_queue.c:714-733:The
resource_setretains the command buffer and the binding-table buffers (here:@__constant_tensor_5x5xf32, the hal.buffer wrapping the 192-byte rodata) for the duration of the dispatch. The signal at step (2) wakes the main thread'shal.fence.await. The main thread then races through:Meanwhile the worker thread is still between step (2) and step (4):
resource_setis still alive@__constant_tensor_5x5xf32vm.bufferSo the
vm.buffer's counter is still 2 when the main thread looks.If the worker's step (4) wins the race, no abort. If the main thread's teardown wins, abort with
counter=2.iree_hal_task_queue_op_complete_with_epochat line 706 has the same pattern.Why TSan never reported a "data race"
Every access on the shared refcount goes through
iree_atomic_*ops, so it's properly synchronized at the memory-model level. The bug is a missing happens-before edge between "owning thread releases its retained resources" and "consumer thread observes completion" — the signal is published before the resources are released. TSan only reports unordered accesses to the same memory; here the ordering bug is at a higher abstraction level (resource ownership vs. semaphore signal), which TSan cannot detect.Suggested fix
Free
resource_set(and any other retained resource references) before signaling. Apply the same reorder toiree_hal_task_queue_op_complete_with_epoch.static void iree_hal_task_queue_op_complete(iree_hal_task_queue_op_t* operation) { iree_status_t status = iree_hal_task_queue_op_unmap_binding_mappings(operation, iree_ok_status()); + // Release retained resources before signaling so a thread waiting on the + // signal cannot tear down dependent objects (modules, rodata, etc.) while + // we still hold command-buffer / binding-table refs. + if (operation->resource_set) { + iree_hal_resource_set_free(operation->resource_set); + operation->resource_set = NULL; + } if (iree_status_is_ok(status)) { // Signal all semaphores to their new values. status = iree_hal_semaphore_list_signal(operation->signal_semaphores, /*frontier=*/NULL); } if (iree_status_is_ok(status)) { uint64_t epoch = iree_hal_task_queue_op_reserve_completion_epoch(operation); iree_hal_task_queue_op_advance_frontier(operation, epoch); iree_hal_task_queue_debug_record_complete(operation->queue, operation); iree_hal_task_queue_op_destroy(operation, status); } else { iree_hal_task_queue_op_fail(operation, status); } }Reproduction statistics
fprintf-instrumentedHigher parallelism (256 = 8× oversubscription on a 32-core machine) reliably reproduces under instrumentation. The high CPU contention varies thread schedules enough to expose the race window between the worker's
signal_semaphoresandop_destroy.