Skip to content

fix(group): an allocation failure in a group on the radix route answers oom, not a crash or an answer short of a column or groups - #1809

Merged
singaraiona merged 1 commit into
devfrom
fix/1808-radix-alloc-fail
Nov 27, 2026
Merged

singaraiona merged 1 commit into
devfrom
fix/1808-radix-alloc-fail

Conversation

@singaraiona

@singaraiona singaraiona commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

The defect

An allocation that failed while a group ran on the radix route could crash the process or answer a wrong result (#1808, found by the reviewer of #1806):

  • Crash. The radix emit of exec_group_run skipped a key column it could not allocate, or whose source column was missing. Phase 3 then wrote that key through its NULL data: SIGSEGV in radix_phase3_fn (write_col_i64, data=0x0).
  • A column short. The emit skipped an aggregate column it could not allocate the same way. The answer lacked that column.
  • Groups short. The classic radix route's phase 2 skipped a partition whose table it could not initialise. Nothing after it checked a partition's oom (also set when a table cannot grow while folding rows in), so the answer lacked that partition's groups.
  • Memory kept. The emit dropped the failed ray_vec_new's error object: 128 bytes each (error objects are not reference-counted).

The fix

  • The emit records a missing key or aggregate column. Once both loops have set every slot, it discards the built columns and the result, and answers the allocation's own error (oom; any further error is freed).
  • Phase 2 (classic) marks a partition whose init failed as oom, as a failed grow does. After the dispatch, any such partition makes the route free every partition table and answer oom. A partition whose init failed has its slots but no rows, which cleanup's rows-only free would keep.
  • The v2 route already falls back to the classic route on a failed phase-2 init, and answers correctly.

Test

Two DEBUG seams:

  • ray_group_alloc_fail_for_test(n) fails the nth allocation of the calling thread's own code while the outermost group (exec_group) runs, and reports how many it made. The allocator skips allocations the thread makes inside pool tasks it steals (pool_exec marks them, ray_alloc_fail_skip_tasks_for_test). Which tasks the calling thread steals varies from run to run, its own code does not, so the sweep covers every position deterministically.
  • ray_group_test_ht_rows_fail(n, cancel), from fix(group): a cancelled group on the v2 radix route frees its worker and partition hash tables #1806, now also counts the classic route's phase-2 partition inits, on any thread. It is the coverage for allocations inside the pool tasks.

aware/group_alloc_fail (test/test_aware.c) runs a v2-radix query (by: k, k an F64 column), a two-key query with count and sum, and a classic-radix query (max). It fails:

  • each allocation of the group's own code in turn, every position;
  • each of the first 32 phase-2 partition inits.

Each run must answer byte for byte the unfailed answer, or oom keeping nothing by the process's allocation trace (every heap) over the run, the error's release and a GC.

Reverting each fix on its own, with the seams kept:

  • the key-column skip: aware/group_alloc_fail takes a SIGSEGV;
  • the aggregate-column skip: (select {c: (count v) s: (sum v) from: F by: [k j]}), allocation 11 of 12 fails: an answer, not the reference;
  • the emit's error not freed: (select {from: F by: k}), allocation 9 of 10 fails: 128 bytes kept;
  • the classic phase-2 init skip: (select {m: (max v) from: F by: k}), partition init 1 fails: an answer, not the reference.

Not changed (listed in #1808)

  • The dense-array route (an I64 key over a small range) has the same key-column skip, and uses its dense_* scratch buffers unchecked.
  • ray_select_body's keys-only result build keeps 128 bytes on a few oom exits.

Both are outside the radix route; the test's queries do not take them.

Cost

Release behaviour is unchanged when nothing fails. The seams are DEBUG only, including the allocator's check. The emit adds one flag per column. The route adds one check per partition after phase 2: a few hundred instructions per query, nothing per row. Callgrind, release (gcc), -c 1, 1,048,576 rows, against its base, #1806's head (now on dev): every shape is +22 instructions, exactly the setup's own difference.

Checks

  • gcc release and debug builds are clean. make -j4 test TEST_ARGS="-j 4": 5,890 of 5,894 passed, 4 skipped.
  • ASan + UBSan: aware/group_alloc_fail passes; filter cancel: 87 of 88 passed (1 skipped); no sanitizer report.
  • TSan (filter cancel): 87 of 88 passed (1 skipped), no report.
  • clang-23 -fsyntax-only on group.c, heap.c and pool.c (with and without -DDEBUG) and test_aware.c: clean.

Fixes #1808

…rs oom, not a crash or an answer short of a column or groups

The radix emit of exec_group_run skipped a key column it could not
allocate, or whose source column was missing, and phase 3 then wrote
that key through its NULL data: SIGSEGV in radix_phase3_fn.  It skipped
an aggregate column it could not allocate the same way, and the answer
lacked that column; the failed allocation's error was dropped (error
objects are not reference counted).  The classic radix route's phase 2
skipped a partition whose table it could not initialise, and nothing
after it checked a partition's oom (also set when a table cannot grow
while folding rows in), so the answer lacked that partition's groups.

The emit now records a missing key or aggregate column, and once both
loops have set every slot it discards the columns and the result and
answers the allocation's own error (`oom`).  Phase 2 marks a partition
whose init failed as oom, as a failed grow does; after the dispatch any
such partition frees every partition table (one whose init failed has
its slots but no rows) and answers `oom`.

Two DEBUG seams test it.  ray_group_alloc_fail_for_test fails the nth
allocation of the calling thread's own code while the outermost group
runs; the allocator skips those the thread makes inside pool tasks it
steals (pool.c marks them), whose share varies from run to run, so the
sweep is deterministic.  The partition-init seam of #1806 now also
counts the classic route's phase 2.  aware/group_alloc_fail sweeps both
over a v2-radix, a two-key and a classic-radix query.

Fixes #1808

@singaraiona singaraiona left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of d2143ff: NEEDS CHANGES

The radix route itself now holds under every single allocation failure I injected, on any thread. One instance of the same column skip is left on a path the radix route falls back to (item 1). The sweep was time-boxed; what it did and did not cover is stated below.

Finding

  1. The sequential emit that the radix route falls back to still skips a column it cannot allocate. When radix_bufs cannot be allocated, exec_group_run takes goto sequential_fallback. That emit (group.c ~22605-22612) then has both skips:

    • a key column if (!src_col) continue; / if (!new_col || RAY_IS_ERR(new_col)) continue;;
    • an aggregate column if (!new_col || RAY_IS_ERR(new_col)) continue; (~22883).

    These are the skips this PR removes from the radix emit, and they drop the failed ray_vec_new's 128-byte error the same way. A query on the radix route that meets a second failure in that emit (the first being radix_bufs) would answer short a column. The same emit serves the hash-table path for groups that never go radix.

    I found this by reading. I did not reproduce it: a single injected failure is consumed by radix_bufs itself, so the fallback's emit allocations then succeed, and my time-boxed small-table run did not complete. The fix is the same as the radix emit's: flag, discard, answer the allocation's own error.

What the sweep covered (on this head)

I added a probe seam to my local tree only, never pushed. It fails the nth allocation process-wide, on whichever thread makes it, while exec_group runs (a counter exec_group raises). It is optionally restricted to allocations inside pool tasks, which covers the worker threads and the tasks the calling thread steals that the PR's seam skips. Every position n from 1 to the run's count was swept once. Because which thread allocates where varies between runs, this is a randomised cover of the worker allocations, not a deterministic one.

Each run had to give the unfailed answer byte for byte (ser), or oom with net_bytes <= 0 by ray_mem_trace over every heap, after the error's release and a GC.

Query, 262,144 rows, 4 threads Any thread Pool tasks only
by: k (F64, v2) 5,130 positions: same 5,124, oom 9 5,120: same 5,123, oom 0 (v2 falls back and answers)
sum/avg by k 5,131: same 5,124, oom 10 5,120: all same
count/sum by [k j] 8,364: same 8,357, oom 10 8,352: all same
max by k (classic) 3,594: same 2,556, oom 1,041 3,586: same 2,541, oom 1,048
sum by k under where: 5,129: same 5,124, oom 8 all same
min/max by [k s] 4,374: same 2,646, oom 1,731 4,484: same 2,711, oom 1,776
by: j (I64), i (I32), d (DATE), s (SYM) 14-43 each: all same or oom the same

Across about 57,000 injected failures: no crash, no short or different answer, no error class other than oom, and 0 bytes kept.

  • The base a6779b7 crashes on the first query of the same sweep (the SIGSEGV in radix_phase3_fn).
  • A random multiple-failure mode (about 0.1% of allocations failing) ran only one cell before the time-box: by: k, 150 runs, same 136, oom 14, nothing kept.

Not covered: two or more failures per run beyond that one cell, which is the route into item 1. The whole-query scope, beyond the group, was not swept to completion.

Outside the group, for the record: a whole-query run, stopped partway, shows faults before the group starts, so not this PR:

  • 576-640 bytes kept at an early allocation;
  • one injected failure answers nyi instead of oom;
  • a SIGSEGV in ray_select_body (query.c:28688, root->id with root NULL after a graph allocation failed) for the where: + by: query.

These deserve their own issue.

The PR's own seam: what it cannot reach

ray_group_alloc_fail_for_test skips allocations inside pool tasks, so it never fails a worker's allocation or a stolen task's. That leaves the phase-1 buffers, the worker HT grows, the v2 worker HTs and phase-3 work. ray_group_test_ht_rows_fail covers one site inside tasks, the phase-2 partition init. The rest is covered only by an any-thread injection like the one above. I would add one to the test, scoped to the group as the PR's seam is, for example a process-wide atomic countdown, and accept that positions vary between runs.

The author's two "not fixed" claims: both real

I confirmed both by reading.

  • Dense-array route.
    • Its emit (group.c ~9692) has the same aggregate-column continue, and the key loop too.
    • Its dense_* scratch buffers (~20147-20162) are scratch_alloc'd without a check. The per-aggregate copies are guarded by if (dense_sum) and the like, so a failed one silently drops that aggregate's values. dense_counts[gi] = … is written unconditionally.
    • My by: w (I64 over 50 values) single-failure runs answered right or oom, so I did not reproduce a fault there.
  • Keys-only result build. It sits in ray_select_body, outside exec_group. The whole-query run above did show bytes kept there.

Uninjected behaviour and cost

  • Release output: seven queries over the 262,144-row table (by: k, sum/avg, [k j], max, [k s], by: w, by: s). The ser of all the results is byte-identical between this head and a6779b7.
  • Callgrind (-c 1, the whole script): 1,756,806,079 instructions against 1,756,806,010, which is +69 for the seven queries. Nothing per row.

Runs (gcc)

  • Release build clean. make -j4 test TEST_ARGS="-j 4": 5890 of 5894 passed, 4 skipped, 0 failed.
  • ASan+UBSan test binary: aware/group_alloc_fail 1/1, -f cancel 87/88 (1 skip), -f group/ 98/98, no sanitizer report.
  • TSan: aware/group_alloc_fail 1/1 and -f cancel 87/88 (1 skip), 0 reports.

singaraiona added a commit that referenced this pull request Nov 27, 2026
…ers oom, not a table short of a column or a write through NULL

The dense-array route of exec_group_run skipped a key column it could
not allocate, and emit_agg_columns, which builds the aggregate columns
of this and several other routes, skipped an aggregate column the same
way: the answer lacked that column, and the failed allocation's error
was dropped.  The route used its dense_* scratch buffers unchecked and
wrote through NULL when one failed to allocate.

The route now answers the allocation's failure through one exit that
frees its scratch, its shared buffers and the result: a failed key
column, a failed ray_table_add_col, or a missing scratch buffer.
emit_agg_columns makes *result the failed column's own error; its
callers return the result as before, and the whole-table route, which
recomputes columns after it, takes it as its recompute error.

The sequential fallback the radix route takes when its buffer table
fails to allocate had the same two skips in its own emit, a key column
and an aggregate column; both now answer the failed column's error.
Its group_ht_init failing part-way kept the slots it had allocated;
single_ht is zeroed first and freed on that exit.  A failed
group_ht_rehash kept the old slot table, which was resized only after
the insert that took it past half full: the fallback table starts at
two slots, so that insert filled it, and the next probe for a new key
found no empty slot and looped forever.  The two probes that insert
(group_probe_entry and group_merge_row) now rehash before that insert
and probe again, and a failed rehash marks the table oom, as a failed
grow of its rows does, so it takes no more groups and the group answers
oom.

A DEBUG seam, ray_select_keys_alloc_fail_for_test, fails each allocation
of the keys-only select-by result build in ray_select_body;
aware/group_alloc_fail sweeps it, and the group seam of #1809 now also
sweeps the dense-array route, and, with a second DEBUG seam
(ray_group_test_radix_bufs_fail) failing the radix buffer table, the
sequential fallback.

Fixes #1811
@singaraiona

Copy link
Copy Markdown
Collaborator Author

On the review's point (the sequential fallback the radix route takes when radix_bufs fails): it was right, and #1812 did not cover it before. #1812 fixed emit_agg_columns and the dense route's key column, but the fallback has its own emit, which still skipped a key column and an aggregate column it could not allocate.

I added the fix and a two-failure test to #1812, now at 7bbcfb1. This pull request is unchanged.

The test:

  • Seam. A DEBUG seam, ray_group_test_radix_bufs_fail, fails the radix buffer table as a failed calloc would, so the group takes the fallback.
  • Sweep. The group seam then fails the nth allocation, every position, over (select {m: (max v) from: G by: k}) (F64 key) and (select {m: (max v) s: (sum v) from: G by: [k j]}).
  • Pass condition. Each run must give the unfailed answer, or oom with nothing kept.
  • Without each fix:
    • with the key skip restored: allocation 20 gives an answer short of the reference;
    • with the aggregate skip restored: the same at allocation 21.

The sweep found two more defects on that fallback, both also fixed in #1812:

  • Leak. group_ht_init failing part-way kept 64 bytes of slots.
  • Hang. A failed group_ht_rehash left the two-slot fallback table full, so the next probe for a new key looped forever. The probes now rehash before the insert that would take the table past half full. A failed rehash marks the table oom, as a failed row grow already does.

The timing is the same before and after (within run-to-run noise): 20M rows with 5M groups, at -c 1 and -c 4.

@singaraiona

Copy link
Copy Markdown
Collaborator Author

The lead accepts this pull request as it is. The latest review's one point (the radix route's sequential fallback emit could still skip a column on a second allocation failure) was found by reading, not reproduced, and everything the review ran passed. The author has fixed that fallback, with a two-failure test, in the stacked #1812 (issuecomment-7211176320), which is reviewed next. No other finding against this change is open, so it goes to the merge queue.

@singaraiona
singaraiona added this pull request to the merge queue Nov 27, 2026
Merged via the queue into dev with commit 4b60b75 Nov 27, 2026
11 checks passed
singaraiona added a commit that referenced this pull request Nov 27, 2026
…ers oom, not a table short of a column or a write through NULL

The dense-array route of exec_group_run skipped a key column it could
not allocate, and emit_agg_columns, which builds the aggregate columns
of this and several other routes, skipped an aggregate column the same
way: the answer lacked that column, and the failed allocation's error
was dropped.  The route used its dense_* scratch buffers unchecked and
wrote through NULL when one failed to allocate.

The route now answers the allocation's failure through one exit that
frees its scratch, its shared buffers and the result: a failed key
column, a failed ray_table_add_col, or a missing scratch buffer.
emit_agg_columns makes *result the failed column's own error; its
callers return the result as before, and the whole-table route, which
recomputes columns after it, takes it as its recompute error.

The sequential fallback the radix route takes when its buffer table
fails to allocate had the same two skips in its own emit, a key column
and an aggregate column; both now answer the failed column's error.
Its group_ht_init failing part-way kept the slots it had allocated;
single_ht is zeroed first and freed on that exit.  A failed
group_ht_rehash kept the old slot table, which was resized only after
the insert that took it past half full: the fallback table starts at
two slots, so that insert filled it, and the next probe for a new key
found no empty slot and looped forever.  The two probes that insert
(group_probe_entry and group_merge_row) now rehash before that insert
and probe again, and a failed rehash marks the table oom, as a failed
grow of its rows does, so it takes no more groups and the group answers
oom.

A DEBUG seam, ray_select_keys_alloc_fail_for_test, fails each allocation
of the keys-only select-by result build in ray_select_body;
aware/group_alloc_fail sweeps it, and the group seam of #1809 now also
sweeps the dense-array route, and, with a second DEBUG seam
(ray_group_test_radix_bufs_fail) failing the radix buffer table, the
sequential fallback.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

an allocation failure in a group on the radix route crashes in radix_phase3_fn or answers a column or groups short, instead of oom

1 participant