Skip to content

fix(graph): an allocation failure while building a query plan answers oom, not a crash, nyi or memory kept - #1820

Open
singaraiona wants to merge 1 commit into
devfrom
fix/1817-group-build-oom
Open

singaraiona wants to merge 1 commit into
devfrom
fix/1817-group-build-oom

Conversation

@singaraiona

@singaraiona singaraiona commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

The defect

A query plan built while an allocation failed crashed, answered the wrong error, or kept memory (#1817; the review of #1812 found the first case, and the review of this pull request found the general one):

  • A NULL node read through. Every graph builder read the id and type of the input nodes it was given. A node that failed to build is NULL, so a node built over it was read through NULL:
    • SIGSEGV in ray_group_build (the parted per-partition group);
    • SIGSEGV in ray_join (inner-join);
    • SIGSEGV in ray_select_body at root->id, when ray_const_table failed under a where:;
    • the deferred where: read a take's head unchecked.
  • nyi instead of oom. A plan whose root is NULL answered nyi. The node allocators record their failure in g->compile_err, but ray_execute did not read it, and the builders' own scratch carves recorded nothing.
  • Memory kept. A failure the engine answers by another route kept the failure's error object, which ray_release does not free:
    • the v2 serial group's result table and its aggregate column (agg_run_one);
    • the expression bridge's extended table (exec_group_v2_exprs_run);
    • the per-partition group's sub, merge and trimmed tables, and its merge result;
    • the fused top-k's gathered column;
    • the span table: a failed ray_nfo_create was an error object, which the evaluator handed to the parser as its span table and kept. A span whose append failed left an error in the table.
  • A double release. The factorized group shortcut released a table that a failed ray_table_add_col had already consumed.

The fix: one rule for every builder

  • Every builder that takes input nodes refuses a NULL one and answers NULL. An array of n inputs is checked element by element (graph_ins_ok); a NULL array holds none.
    • The failure is the one already recorded, so a plan built under a failure ends in a NULL root.
    • ray_execute answers that root with the graph's compile error (oom), and nyi only when nothing was recorded, as before.
    • A builder whose own work reads a->out_type (most of the one-liners) checks before it. The others pass the input to make_unary, make_binary or make_cmp, which check it.
    • The builders that refuse a NULL input (117): make_unary, make_unary_param, make_binary, ray_neg, ray_abs, ray_not, ray_sqrt_op, ray_log_op, ray_exp_op, ray_sin_op, ray_asin_op, ray_cos_op, ray_acos_op, ray_tan_op, ray_atan_op, ray_reciprocal_op, ray_signum_op, ray_ceil_op, ray_floor_op, ray_round_op, ray_isnull, ray_cast, make_cmp, ray_binop, ray_add, ray_sub, ray_mul, ray_div, ray_idiv, ray_mod, ray_pow_op, ray_eq, ray_ne, ray_lt, ray_le, ray_gt, ray_ge, ray_and, ray_or, ray_min2, ray_max2, ray_in, ray_not_in, ray_if, ray_like, ray_ilike, ray_upper, ray_lower, ray_strlen, ray_trim_op, ray_str_find_op, ray_substr, ray_replace, ray_concat, ray_sum, ray_prod, ray_all, ray_any, ray_min_op, ray_max_op, ray_count, ray_avg, ray_first, ray_last, ray_count_distinct, ray_distinct_op, ray_asc_op, ray_desc_op, ray_reverse_op, ray_lag_op, ray_lead_op, ray_deltas_op, ray_ratios_op, ray_fills_op, ray_sums_op, ray_avgs_op, ray_mins_op, ray_maxs_op, ray_prds_op, ray_differ_op, ray_msum_op, ray_mavg_op, ray_mmin_op, ray_mmax_op, ray_mcount_op, ray_mvar_op, ray_mdev_op, ray_stddev, ray_stddev_pop, ray_var, ray_var_pop, ray_median, ray_filter, ray_sort_op, ray_group_build, ray_group, ray_distinct, ray_pivot_op, ray_join, ray_antijoin, ray_asof_join, ray_window_op, ray_select_op, ray_head, ray_tail, ray_extract, ray_date_trunc, ray_expand, ray_var_expand, ray_shortest_path, ray_dijkstra, ray_dfs, ray_random_walk, ray_astar, ray_k_shortest, ray_ann_rerank, ray_knn_rerank.
  • The leaf builders (scans, constants, ray_const_table, the whole-graph algorithms; no node inputs) already recorded a failed node or literal allocation: graph_alloc_node, ray_scan, ray_scan_sym, ray_const_f64, ray_const_i64, ray_const_bool, ray_const_str, ray_const_vec, ray_const_atom, ray_const_table, ray_scan_table, ray_pagerank, ray_connected_comp, ray_louvain, ray_degree_cent, ray_topsort, ray_wco_join, ray_cluster_coeff, ray_betweenness, ray_closeness, ray_mst, ray_graph_input_vec.
  • Each builder's scratch carve records its failure as the graph's compile error. These are the id buffers of the group, sort, pivot, join, antijoin, asof-join, window and select builders.
  • The callers. ray_select_body checks its root and the deferred where:'s head. A scan of every builder call outside graph.c for a dereference of its result before a NULL check finds no other.
  • The dropped errors above are freed, or answered as the failure. ray_nfo_create answers NULL (no spans) when a part fails, and a span whose append fails goes unrecorded, with its key taken back.

What the review's 640 bytes were

  • 18 to 21 (parse included): at those positions the error's trace keeps the parse frame (640 bytes) until the next request clears it, by design.
  • 1 to 6 and 17 to 18: real leaks, the span table's. They are fixed above.

The sweeps clear the trace before they measure.

Tests

aw_sweep_eval fails the nth allocation of an evaluation, every n it reaches, in three modes: the parsed query on the calling thread, the same with its pool tasks failing too, and the text with the parse included. Every run must answer the unfailed answer (grouped ones in key order: a failed dense route falls back in first-seen order, #1819) or oom. Either way it must keep nothing by the process's allocation trace: an answered run is released before it is measured.

  • aware/plan_alloc_fail (new):
    • by: k and by: [k j];
    • a sort with take:;
    • keyless max/avg;
    • a running and moving window (sums, mavg);
    • inner-join and left-join;
    • a group over an inner-join.
  • aware/group_build_alloc_fail: its eleven in-memory and parted shapes now run through the same sweep, so leaks and pool tasks are checked too.
  • aware/group_parted_alloc_fail: the group seam over three per-partition groups.
  • aware/group_alloc_fail: also sweeps the slice-group route and the keyless whole-table route.
  • where: shapes (where: + by:, sorted, keyless and window selects under a where:, on in-memory, splayed, compressed and parted tables) are swept in sel_compact hands back the whole table when its row index fails to allocate, and some callers answer from the dropped rows #1818, stacked on this. Its own fixes (the row-selection builder's abort, the "every row" selection) are needed before a where: survives a failure. Without this pull request's root check, that sweep takes the ray_select_body SIGSEGV.

Each fix is needed. Each was undone in turn, and the sweeps then fail:

  • without the builder rule: plan_alloc_fail takes a SIGSEGV;
  • without the agg-engine fixes: the group over a join keeps 64 bytes at allocation 75;
  • without the span-table fixes: the first allocation keeps 64 bytes on an answered run;
  • without the fused top-k fix: the sort keeps 128 bytes at allocation 37;
  • without the group fixes: the expression group keeps 64 bytes at allocation 56, and the parted group at 61 and 122.

The factorized shortcut's double release, and the sibling agg_run_one exit, are fixed by reading; no sweep reaches them.

Cost

Callgrind, release (gcc), -c 1, against dev (84408f0), with table setup subtracted. Eight queries over 1,048,576 rows and a 200,000 × 50,000-row join: by [k j], keyless, median by j, sort and take, window, inner join, left join, group over join.

dev this branch
Instructions 1,177,915,537 1,177,916,796

That is +1,259 instructions in all: a NULL check per builder call while planning. The answers are byte for byte the same as dev's.

Checks

  • gcc release and debug builds are clean.
  • make -j4 test TEST_ARGS="-j 4": 5,900 of 5,904 passed, 4 skipped.
  • ASan + UBSan: the four sweeps pass; filter cancel: 87 of 88 passed, 1 skipped; no sanitizer report.
  • clang-23 -fsyntax-only on every changed C file, with and without -DDEBUG: clean.

Fixes #1817

@singaraiona
singaraiona force-pushed the fix/1817-group-build-oom branch from 9354fe9 to 4e05e20 Compare November 27, 2026 07:33

@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 4e05e20: NEEDS CHANGES

The parted per-partition crash from #1812 is fixed, and a recorded failure never reaches a later query. Plan building for where: + by: and for joins still crashes under a single allocation failure, and a plain by: keeps 640 bytes on early failures.

Method

I added a local seam, never pushed: the nth ray_alloc of the whole query fails on any thread, every position swept once. The table is 3,000 rows with 4 pool threads, and the parted copy has 3 partitions.

  • Each failed run must answer oom with net_bytes <= 0 (ray_mem_trace, every heap, after release and GC), or an answer identical to the unfailed one.
  • After every run, the same query runs again unfailed on the same thread and must equal the reference. That is the check that a recorded failure is cleared.

Findings

  1. where: + by: still crashes in the select planner. (select {c: (sum v) from: F where: (> v 100) by: j}) takes a SIGSEGV at query.c:29216: uint32_t where_input_id = root->id; with root NULL. The table node (ray_const_table) failed to allocate, and ray_select_body uses it before ray_execute could answer the recorded oom. This is the NULL-root crash I listed in the #1809 review; the planner needs the same check on root that ray_execute_inner now has. The same query over the parted table crashes the same way.
  2. A join crashes on a failed input node. (inner-join F G ['j]) takes a SIGSEGV in ray_join (called from join_exec, query.c:41486) with left_table == NULL. Only ray_group_build gained the NULL-input refusal. ray_join, ray_antijoin, ray_asof_join, ray_sort_op, ray_window_op, ray_select_op and ray_pivot_op now record a failed id carve, but they still read keys[i]->id and the table inputs without a NULL check. I did not sweep the left join or the sort after this crash.
  3. A plain by: keeps 640 bytes on early failures. In (select {c: (sum v) from: F by: j}), 18 of the 30 oom positions (from n=7 on) keep 640 bytes by the trace. The parted by: j and the two-key by: [j s] show the same (18 and 21 positions). This is before the group runs; the #1809 review saw the same 576-640 bytes in the whole-query sweep. Not introduced here, but it is in plan building, which this PR is about.

What is right

  • Parted per-partition group (select … from: P by: j, 136 positions): every failure answers oom or the unfailed answer, apart from the bytes in item 3. No crash and no nyi; #1812's SIGSEGV in ray_group_build is gone.
  • by: j and by: [j s] in memory: oom or the unfailed answer, no crash, no nyi (bytes as in item 3).
  • The record is cleared. It is per graph (g->compile_err, freed in the graph's free, graph.c:178) and handed over by ray_execute_inner. In all three sweeps the healthy re-run after every failed one answered the reference: 0 stale.
  • Uninjected: release builds of this head and dev 84408f0, -c 1, 200,000 rows: by:, where: + by:, two keys, inner-join, a group over a join, and max by an F64 key. The ser of all results is identical. Callgrind totals are 1,110,219,265 against 1,110,219,042 (+223 over the script).
  • Runs:
    • gcc make -j4 test TEST_ARGS="-j 4": 5899 of 5903 passed, 4 skipped, 0 failed;
    • ASan+UBSan: the aware tests this head touches (group_build_alloc_fail, group_parted_alloc_fail, group_alloc_fail, group_fallback_order, group_sym_key_nulls_flag, holistic_oom, radix_cancel_keeps_nothing, sel_compact_cancel_at_dispatch) and -f group/ 98/98, no sanitizer report.

Not covered (about 30 minutes)

  • The left join, the group over a join, and the sort-only select: the join crash stopped my sweep before them.
  • Keyless selects and windows.
  • TSan.
  • Whether item 3's 640 bytes are the plan or the parse: I did not trace which allocation keeps them.

… oom, not a crash, nyi or memory kept

A plan built while an allocation failed crashed, answered nyi, or kept
memory:

- The graph builders read the id and type of every input node they
  were given.  A node that failed to build is NULL, so a group, join,
  filter, sort, window or select node over it was read through NULL:
  the parted per-partition group took a SIGSEGV in ray_group_build,
  inner-join one in ray_join.  ray_select_body read the root that
  ray_const_table gave unchecked, and the deferred where: read a
  take's head unchecked.
- A plan whose root is NULL answered nyi: the node allocators record
  their failure in g->compile_err, but ray_execute did not read it,
  and the builders' own scratch carves recorded none.
- A failure the engine answers by another route kept the failure's
  error object, which ray_release does not free: the v2 serial group's
  result table and aggregate column, the expression bridge's extended
  table, the per-partition group's sub, merge and trimmed tables and
  its merge result, and the fused top-k's gathered column.  A failed
  span table (ray_nfo_create) was an error the evaluator handed to the
  parser and kept, and a span whose append failed left an error in the
  table.  The factorized group shortcut released a table that a failed
  ray_table_add_col had already consumed.

Every graph builder refuses a NULL input node and answers NULL (a NULL
array holds none): the failure is the one already recorded, and a
plan built under it ends in a NULL root.  The builders record a failed
scratch carve, and ray_execute answers a NULL root with the graph's
compile error when there is one.  The select's root and the deferred
where:'s head are checked.  Each dropped error is freed or answered,
ray_nfo_create answers NULL (no spans) when a part fails, and a span
whose append fails is not recorded.

aware/plan_alloc_fail and aware/group_build_alloc_fail fail the nth
allocation of the evaluation, every n, of eighteen select and join
shapes: on the calling thread, then on its pool tasks too, then with
the parse included.  Every run answers the unfailed answer (grouped
ones in key order) or oom, keeping nothing either way.
aware/group_parted_alloc_fail sweeps the group seam over three
per-partition groups, and aware/group_alloc_fail now also sweeps the
slice-group route and the keyless whole-table route.

Fixes #1817
@singaraiona
singaraiona force-pushed the fix/1817-group-build-oom branch from 4e05e20 to ffcb87c Compare November 27, 2026 08:02
@singaraiona singaraiona changed the title fix(graph): an allocation failure while building a group node answers oom, not a crash or nyi fix(graph): an allocation failure while building a query plan answers oom, not a crash, nyi or memory kept Nov 27, 2026
@singaraiona

Copy link
Copy Markdown
Collaborator Author

Thanks. The rule is now general, in this pull request, at ffcb87c:

  • Every graph builder that takes input nodes refuses a NULL one and answers NULL. That is 117 builders; the description lists them. An array of n inputs is checked element by element. A plan built under any failure ends in a NULL root, and ray_execute answers it with the recorded oom.
  • The 22 leaf builders (scans, constants, ray_const_table and the whole-graph algorithms) already recorded their own failed allocation.
  • The callers. I scanned every builder call outside graph.c for a dereference of its result before a NULL check. It found one, the deferred where: reading a take's head, now checked. The select's root from ray_const_table is checked too.
  • The crashes you hit:
  • The 640 bytes at 18 to 21 on plain by: were the parse error's trace frame. It holds the error's location until the next request clears it, by design.
    • With the sweep counting the parse, the real leaks were elsewhere. A failed span table (ray_nfo_create) was an error object that the evaluator kept, at failures 1 to 6. A failed span append left an error in the table, at 17 to 18. Both are fixed.
    • Measuring answered runs too turned up more errors dropped on the way to another route: the v2 serial group, the expression bridge, the per-partition group's tables and merge, and the fused top-k. All are fixed.
    • A double release in the factorized group shortcut was also fixed.
  • The sweep (aw_sweep_eval) runs every position in three modes: calling thread only, pool tasks too, and parse included. Every run must give the unfailed answer, or oom, and keep nothing either way. It covers:
    • by: and multi-key by:;
    • a sort with take:;
    • keyless and window selects;
    • inner and left join, and a group over a join;
    • and, through aware/group_build_alloc_fail, eleven in-memory and parted shapes.
  • Cost: +1,259 instructions over eight queries against dev, and the answers are identical. Suite and ASan are green.

This branch has not been deployed

No deployments
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 while building a group node crashes the parted group or answers nyi, not oom

1 participant