Skip to content

fix(group): an allocation failure on the dense-array group route answers oom, not a table short of a column or a write through NULL - #1812

Merged
singaraiona merged 1 commit into
devfrom
fix/1811-da-route-oom
Nov 27, 2026
Merged

singaraiona merged 1 commit into
devfrom
fix/1811-da-route-oom

Conversation

@singaraiona

@singaraiona singaraiona commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Beyond the title

Besides the dense-array route of #1811, this pull request fixes three defects on the radix route's sequential fallback, the path a group takes when the radix buffer table radix_bufs fails to allocate:

  1. The fallback's emit on a second failure. After radix_bufs failed, a failed key or aggregate column was skipped, so the answer was short of a column (raised in the review of 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).
    • Tested by the two-failure sweep below.
    • With the key skip restored it fails at allocation 20; with the aggregate skip restored, at allocation 21 ("an answer, not the reference").
  2. A 64-byte leak on a part-way group_ht_init. The fallback's table init kept the slots it had allocated when a later allocation failed.
    • Tested by the same sweep's allocation trace.
    • Without the fix it fails at allocation 4 ("64 bytes kept").
  3. An endless probe loop after a failed rehash. The two-slot fallback table filled, and the next probe for a new key never ended.
    • Tested by the same sweep.
    • With the probe and rehash functions as on dev, the sweep hangs (killed at 90 s). With the fix it finishes in 1.5 s.
    • The rehash-order change has the timing check in Cost below: within run-to-run noise.

The defect

An allocation that failed on the dense-array group route, or on the radix route's sequential fallback, answered a wrong result, kept memory or hung (#1811, found while fixing #1808):

  • A column short. The route's key-column loop skipped a key it could not allocate. 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 object was dropped.
  • The radix route's sequential fallback (taken when its buffer table radix_bufs fails to allocate) had the same two skips in its own emit: a key column and an aggregate column. This was raised in the review of 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.
  • A leak on that fallback. When group_ht_init failed part-way it kept the slots it had already allocated (64 bytes).
  • A hang on that fallback. A failed group_ht_rehash kept the old slot table. The table was resized only after the insert that took it past half full, and the fallback table starts at two slots, so that insert filled it. The next probe for a new key then found no empty slot and looped forever.
  • A write through NULL. The route used its dense_* scratch buffers unchecked, and wrote through NULL when one failed to allocate (SIGSEGV).

The fix

  • The dense-array emit answers each failure through one exit (da_emit_done), which frees its scratch, the route's shared buffers and the result: a failed key column (its own error), a failed ray_table_add_col, or any scratch buffer the route asked for and did not get (oom). The scratch headers are declared ahead of the key loop, so that exit can free them from any point.

  • emit_agg_columns makes *result the failed column's own error, releasing the table. Six of its seven callers return result right away, as before. The whole-table route recomputes columns after it, so it takes that error as its recompute_err, which skips the recomputes and answers it.

  • The sequential fallback's emit answers a failed key or aggregate column with that column's error. On the aggregate exit, it releases the holistic columns that are not yet attached.

  • The fallback's init zeroes single_ht first and frees it when the init fails.

  • The two probes that insert a group (group_probe_entry, group_merge_row) rehash before the insert that would take the table past half full, then probe again in the new table. A failed rehash marks the table oom, as a failed grow of its rows already did. The probes then take no more groups, the table stays at most half full, and the callers answer oom (they already checked the flag for the grow).

The keys-only select-by result build

#1808 listed it as keeping 128 bytes on a few oom exits. A seam over that build (ray_select_keys_alloc_fail_for_test: first rows, the table, the gathered columns of ray_select_body) shows every position answering oom keeping nothing, or the reference. The 128 bytes were the radix emit's dropped error, which #1809 fixed. Nothing changes in that code; the seam stays as coverage.

Test

aware/group_alloc_fail now also:

  • sweeps the dense-array route, (select {s: (sum v) n: (min v) from: F by: j}) with the v2 engine off (it would take the shape otherwise), with the group seam of 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, every position;

  • sweeps the keys-only result build of (select {from: F by: k}) and (select {from: F by: j}), every position.

  • sweeps two failures: a DEBUG seam (ray_group_test_radix_bufs_fail) fails the radix route's buffer table, sending the group to its sequential fallback, and then the group seam fails the nth allocation, every position. The queries are (select {m: (max v) from: G by: k}) (F64 key) and (select {m: (max v) s: (sum v) from: G by: [k j]}), over 70,000 rows.

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

Without the fix:

  • with group.c as on dev: (select {s: (sum v) n: (min v) from: F by: j}), allocation 14 of 19 fails: an answer, not the reference;
  • with only the scratch-buffer check removed: SIGSEGV;
  • with only the fallback's key-column skip restored: (select {m: (max v) from: G by: k}), buffers and allocation 20 fail: an answer, not the reference; with only its aggregate-column skip restored: the same at allocation 21;
  • without the fallback init's free: buffers and allocation 4 fail: 64 bytes kept;
  • with the three probe/rehash functions as on dev: the sweep hangs (killed at 90 s). With the rehash moved but no oom mark, it passes only because the seam fails one allocation, and the retry succeeds. Under a lasting failure that probe would retry the rehash forever.

aw_teardown now also turns the v2 engine back on, in case a failing test turned it off.

Cost

Callgrind, release (gcc), -c 1, 1,048,576 rows, against #1809's head, which is dev's group code (setup alone +22):

Shape #1809 this branch
(select {s: (sum v) n: (min v) from: F by: j}) 116,582,433 116,582,455
(select {s: (sum v) a: (avg v) from: F}) 90,933,067 90,933,100
(count (select {from: F by: j})) 102,022,722 102,022,744

That is +0, +11 and +0 instructions once setup is taken out. Nothing is added per row; the seams are DEBUG only.

The rehash now runs before the insert instead of after, at the same fill, so a group does the same number of rehashes. Release (gcc), 20,000,000 rows, 5,000,000 groups, wall time of three runs each, against dev's probe and rehash:

Shape -c before (ms) after (ms)
(select {s: (sum v) from: T by: a}) 1 178–180 180–181
(select {s: (sum v) c: (count v) from: T by: [a b]}) 1 2,237–2,250 2,228–2,239
(select {s: (sum v) from: T by: a}) 4 61.4–62.7 61.5–63.5
(select {s: (sum v) c: (count v) from: T by: [a b]}) 4 576–580 577–579

That is within run-to-run noise.

Checks

  • gcc release and debug builds are clean. make -j4 test TEST_ARGS="-j 4": 5,893 of 5,897 passed, 4 skipped.
  • ASan + UBSan: aware/group_alloc_fail passes; filter cancel: 87 of 88 passed (1 skipped); no sanitizer report.
  • clang-23 -fsyntax-only on group.c and query.c (with and without -DDEBUG) and test_aware.c (-DDEBUG): clean.
  • The two-failure sweep takes 1.5 s in the suite and 5.2 s under ASan + UBSan.

Fixes #1811

…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
singaraiona force-pushed the fix/1811-da-route-oom branch from 7bbcfb1 to 80ce008 Compare November 27, 2026 06:19

@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 80ce008: READY

My #1809 finding is closed. Every route I injected into answers the unfailed result or oom with nothing kept, and the rehash change is right as a hash-table change. Two observations below are for separate issues, not this PR.

Injection (local seam, never pushed)

The seam fails the nth allocation process-wide, on any thread, while exec_group runs. One mode then fails every following allocation too (a lasting failure). Every position was swept, except one cell noted below. Each run must give the unfailed answer byte for byte (ser), or oom with net_bytes <= 0 by ray_mem_trace (every heap) after release and GC. The table is 70,000 rows with 4 pool threads, and each query runs with v2 on and v2 off:

Shapes Outcome
dense route, (select {s: (sum v) n: (min v) from: F by: j}) (19 positions with v2 off) and sum/avg/max/count by j same or oom, 0 kept
keyless sum/avg and max/count same or oom, 0 kept
by: d (DATE), by: w (I64 over 50,000 values: 34 positions v2 on, 106 v2 off) same or oom, 0 kept
where: + by: j same or oom, 0 kept
two failures on the radix fallback (ray_group_test_radix_bufs_fail, then every position): max by k, max/sum by [k j], min/max by [k d] 33, 41 and 45 positions: same or oom, 0 kept, no short answer
lasting failure (every allocation from the nth on fails): the fallback max by k, the dense route, by: w with v2 on and off always oom (or same before the first failure), 0 kept, no hang

The lasting-failure rows exercise the new oom mark: a failed rehash no longer leaves a full table that a probe can loop in.

The rehash change, read as a hash-table change

  • Same fill. The threshold (grp_count + 1) * 2 > ht_cap before the insert is dev's grp_count * 2 > ht_cap after it, so a group does the same number of rehashes.
  • Restarting the probe. After a successful rehash the probe restarts in the new table, which is now at most half full, so it always meets an empty slot.
  • Keys and tombstones. An existing (duplicate) key is found before the empty-slot branch, so the change never touches it. There are no tombstones: groups are never deleted.
  • After a failed rehash the table keeps its old slots and is marked oom. The new-key branch then refuses further groups, so the table stays at most half full.
    • Every caller checks the flag: the phase-2 and v2 worker loops after each probe, single_ht.oom after group_rows_range (group.c:22642), and seq->oom in the pivot path.
  • The flag only goes up on failure. It is cleared in group_ht_init (ht->oom = 0) and set only on a failed grow or rehash.
  • Other group_ht_init callers after a part-way failure: the v2 worker tables are freed unconditionally (#1806) and pivot_ingest_free frees on slots or rows, so the fallback was the only site that leaked.

Uninjected results and cost

Release builds of this head and the base 4b60b75, -c 1, 1,048,576 rows: a 500,000-group key, a 16-group key and both together (by: [hi lo]).

  • The ser of the results is identical.
  • Callgrind totals are exactly equal: 815,816,036 on both.

Runs (gcc)

  • make -j4 test TEST_ARGS="-j 4": 5893 of 5897 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.

Observations for separate issues (pre-existing, outside this PR's routes)

  1. The parted per-partition group crashes on an allocation failure. (select {s: (sum v) c: (count v) from: P by: j}) over a 3-partition table takes a SIGSEGV in ray_group_build at graph.c:1128 (key_ids[i] = keys[i]->id, keys[i] NULL) from exec_group_per_partition (group.c ~14322). A scan node that failed to allocate is passed on unchecked. This code is untouched here; it is the graph builder of that route.
  2. The group order can change under an injected failure. On where: + by: j (v2 on and off) and on by: w with v2 off, a few failure positions give an answer with the same groups and the same values, but in first-seen order instead of key order. The fallback emits a hash table, where the unfailed run takes the dense or sorted route. My byte-for-byte check counts these as "different". The content is complete and right. Whether group order is part of the contract is the owner's call; if it is, the fallback should sort as the route it replaces does.

Not covered (about 45 minutes)

  • exec_group_slices and the whole-table recompute route by an injection known to reach them: my queries may not have taken every one of the seven emit_agg_columns callers.
  • Wall-time timing of the rehash move at 20 million rows (instruction counts only).
  • SYM-keyed and parted routes beyond the crash above.
  • Two failures outside the fallback.

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 on the dense-array group route answers a table short of a column, or writes through NULL, instead of oom

1 participant