From d2143ff8137bd6dce450ca0cf98c4c6a60ee2f4e Mon Sep 17 00:00:00 2001 From: Anton Date: Fri, 27 Nov 2026 03:58:00 +0100 Subject: [PATCH] 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 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 --- src/core/pool.c | 6 +++ src/mem/heap.c | 10 ++++- src/mem/heap.h | 7 ++++ src/ops/group.c | 95 +++++++++++++++++++++++++++++++++++++---- src/ops/internal.h | 14 ++++++- test/test_aware.c | 102 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 224 insertions(+), 10 deletions(-) diff --git a/src/core/pool.c b/src/core/pool.c index 4f8871aee..1e2e105cc 100644 --- a/src/core/pool.c +++ b/src/core/pool.c @@ -160,7 +160,13 @@ static inline void pool_exec(ray_pool_t* pool, uint32_t worker_id, uint32_t tick } ray_pool_task_t* t = &pool->tasks[ticket & (pool->task_cap - 1)]; int64_t _qs_t0; uint32_t _qs_m = ray_qstats_task_begin(&_qs_t0); +#ifdef DEBUG + ray_test_in_pool_task++; +#endif t->fn(t->ctx, worker_id, t->start, t->end); +#ifdef DEBUG + ray_test_in_pool_task--; +#endif ray_qstats_task_end(_qs_m, worker_id, t->end - t->start, _qs_t0); atomic_fetch_sub_explicit(&pool->pending, 1, memory_order_acq_rel); } diff --git a/src/mem/heap.c b/src/mem/heap.c index 4cde92f45..3f537eaaf 100644 --- a/src/mem/heap.c +++ b/src/mem/heap.c @@ -2089,6 +2089,13 @@ void ray_alloc_fail_set_for_test(int64_t nth, int64_t count) { g_alloc_fail_at = count > 0 && nth > 0 ? nth : 0; g_alloc_fail_count = count; } +int64_t ray_alloc_fail_armed_for_test(void) { return g_alloc_fail_at > 0 ? g_alloc_fail_at : 0; } +/* Pool tasks the calling thread is running (pool.c raises it around each), + * and whether the armed failure skips their allocations: which tasks the + * calling thread steals varies from run to run, its own code does not. */ +_Thread_local int ray_test_in_pool_task; +static _Thread_local bool g_alloc_fail_skip_tasks; +void ray_alloc_fail_skip_tasks_for_test(bool skip) { g_alloc_fail_skip_tasks = skip; } static bool alloc_fails_for_test(void) { if (--g_alloc_fail_at > 0) return false; if (--g_alloc_fail_count > 0) g_alloc_fail_at = 1; /* the next one too */ @@ -2261,7 +2268,8 @@ static ray_t* heap_alloc_impl(size_t data_size, bool* fresh) { ray_t* ray_alloc(size_t data_size) { #ifdef DEBUG - if (RAY_UNLIKELY(g_alloc_fail_at > 0) && alloc_fails_for_test()) return NULL; + if (RAY_UNLIKELY(g_alloc_fail_at > 0) && + !(g_alloc_fail_skip_tasks && ray_test_in_pool_task) && alloc_fails_for_test()) return NULL; #endif bool fresh; ray_t* v = heap_alloc_impl(data_size, &fresh); diff --git a/src/mem/heap.h b/src/mem/heap.h index 39198dea9..89f7e280f 100644 --- a/src/mem/heap.h +++ b/src/mem/heap.h @@ -194,6 +194,13 @@ ray_t* ray_cow(ray_t* v); /* Test seam: ray_alloc on this thread returns NULL for `count` calls, * starting with the nth from now; nth 0 disarms. */ void ray_alloc_fail_set_for_test(int64_t nth, int64_t count); +/* How many allocations on this thread are still to come before the armed + * failure (0: it fired, or none is armed). */ +int64_t ray_alloc_fail_armed_for_test(void); +/* Pool tasks running on this thread (raised by pool.c), and: the armed + * failure skips allocations made inside one (true) or counts them. */ +extern _Thread_local int ray_test_in_pool_task; +void ray_alloc_fail_skip_tasks_for_test(bool skip); /* Test seam: the pools a heap may hold (0: RAY_MAX_POOLS), how many pools * share a size before the next ones double (0: the default), and the most * doublings (UINT32_MAX: the default; 0: standard pools only, the old diff --git a/src/ops/group.c b/src/ops/group.c index d96c35276..8ae87ea26 100644 --- a/src/ops/group.c +++ b/src/ops/group.c @@ -6065,9 +6065,10 @@ static bool group_ht_wire_wide(group_ht_t* ht, uint32_t nk) { #ifdef DEBUG /* Test seam (ray_group_test_ht_rows_fail): the nth partition-table init of - * the v2 radix route's phase 2 from now, on any thread, fails as a failed - * rows allocation does (slots allocated, rows NULL); with cancel, it also - * raises the cancel. t_ht_seam_site marks the inits it counts. */ + * a radix route's phase 2 (v2 or classic) from now, on any thread, fails + * as a failed rows allocation does (slots allocated, rows NULL); with + * cancel, it also raises the cancel. t_ht_seam_site marks the inits it + * counts. */ static _Atomic int64_t g_ht_rows_fail_for_test; static _Atomic bool g_ht_rows_fail_cancel_for_test; static _Thread_local bool t_ht_seam_site; @@ -8631,8 +8632,19 @@ static void radix_phase2_fn(void* ctx, uint32_t worker_id, int64_t start, int64_ uint32_t fe_target = total < 4096 ? total : 4096; while (fe_cap < fe_target) fe_cap <<= 1; uint32_t fe_blk = total < 256 ? total : 256; - if (!group_ht_init_sized(&c->part_hts[p], fe_cap, &c->layout, fe_blk)) +#ifdef DEBUG + t_ht_seam_site = true; +#endif + bool inited = group_ht_init_sized(&c->part_hts[p], fe_cap, &c->layout, fe_blk); +#ifdef DEBUG + t_ht_seam_site = false; +#endif + if (!inited) { + /* its groups would be missing from the answer: the caller + * answers `oom` (the same flag a failed grow sets) */ + c->part_hts[p].oom = 1; continue; + } /* Wide keys need source-column resolution during probe/rehash. */ if (c->layout.any_wide_key && c->key_data) { group_ht_set_key_data(&c->part_hts[p], c->key_data); @@ -15225,13 +15237,42 @@ static bool group_firstn_where_ok(ray_graph_t* g, ray_op_t* op, ray_t* tbl, int6 return agg_v2_admission(g, op, tbl) == AGG_V2_ADMITTED; } +#ifdef DEBUG +/* Test seam (ray_group_alloc_fail_for_test): the nth allocation of the + * calling thread's own code (not the pool tasks it runs) while the + * outermost group runs fails. */ +static _Thread_local int64_t g_group_fail_nth; +static _Thread_local bool g_group_fail_fired; +static _Thread_local int64_t g_group_fail_left; +static _Thread_local int g_group_fail_depth; +void ray_group_alloc_fail_for_test(int64_t nth) { g_group_fail_nth = nth; g_group_fail_fired = false; } +bool ray_group_alloc_fail_fired_for_test(void) { return g_group_fail_fired; } +/* The allocations the last armed group made on this thread, when its + * failure did not fire (armed past them). */ +int64_t ray_group_alloc_count_for_test(void) { return g_group_fail_nth - g_group_fail_left; } +#endif + ray_t* exec_group(ray_graph_t* g, ray_op_t* op, ray_t* tbl, int64_t group_limit) { uint32_t gid = op->id; if (g->firstn_where && !group_firstn_where_ok(g, op, tbl, group_limit)) { g->firstn_declined = true; return NULL; } - return group_retag_duration_means(g, gid, exec_group_core(g, op, tbl, group_limit)); +#ifdef DEBUG + bool arm = g_group_fail_nth && g_group_fail_depth++ == 0; + if (arm) { ray_alloc_fail_skip_tasks_for_test(true); ray_alloc_fail_set_for_test(g_group_fail_nth, 1); } +#endif + ray_t* r = group_retag_duration_means(g, gid, exec_group_core(g, op, tbl, group_limit)); +#ifdef DEBUG + if (g_group_fail_nth) g_group_fail_depth--; + if (arm) { + g_group_fail_left = ray_alloc_fail_armed_for_test(); + g_group_fail_fired = g_group_fail_left == 0; + ray_alloc_fail_set_for_test(0, 0); + ray_alloc_fail_skip_tasks_for_test(false); + } +#endif + return r; } /* ============================================================================ @@ -21597,6 +21638,23 @@ v2_done:; ray_pool_dispatch_n(pool, radix_phase2_fn, &p2ctx, radix_n_parts); scratch_free(p2_kp_hdr); CHECK_CANCEL_GOTO(pool, cleanup); + /* A partition that could not allocate (its init, or a grow while + * folding rows in) lost groups: the query's `oom`. Every partition + * is freed here, one whose init failed holding slots but no rows, + * which cleanup's rows-only free would keep. */ + { + bool p2_oom = false; + for (uint32_t p = 0; p < radix_n_parts && !p2_oom; p++) + p2_oom = part_hts[p].oom != 0; + if (p2_oom) { + for (uint32_t p = 0; p < radix_n_parts; p++) group_ht_free(&part_hts[p]); + scratch_free(part_hts_hdr); + part_hts = NULL; + part_hts_hdr = NULL; + result = ray_error("oom", NULL); + goto cleanup; + } + } if (radix_bufs) { size_t n_bufs_free = (size_t)n_total * radix_n_parts; @@ -22002,13 +22060,20 @@ v2_emit:; char* key_dsts[vla_keys]; int8_t key_out_types[vla_keys]; uint8_t key_esizes[vla_keys]; + /* Every key and aggregate column is written by phase 3, so one that + * is missing (a failed allocation, or a key left unread by one) is + * the query's `oom`, checked once both loops have set every slot. + * Skipping it wrote phase 3 through a NULL key column, or dropped + * the aggregate's column from the answer. */ + bool emit_oom = false; + ray_t* emit_err = NULL; /* the first allocation's own error, the answer */ for (uint32_t k = 0; k < n_keys; k++) { ray_t* src_col = key_vecs[k]; key_cols[k] = NULL; key_dsts[k] = NULL; key_out_types[k] = 0; key_esizes[k] = 0; - if (!src_col) continue; + if (!src_col) { emit_oom = true; continue; } uint8_t esz = ray_sym_elem_size(src_col->type, src_col->attrs); ray_t* new_col; if (src_col->type == RAY_SYM) { @@ -22032,7 +22097,12 @@ v2_emit:; } } else new_col = ray_vec_new(src_col->type, (int64_t)total_grps); - if (!new_col || RAY_IS_ERR(new_col)) continue; + if (!new_col || RAY_IS_ERR(new_col)) { + emit_oom = true; + if (new_col && !emit_err) emit_err = new_col; + else if (new_col) ray_error_free(new_col); + continue; + } new_col->len = (int64_t)total_grps; /* Seed HAS_NULLS from the source key column, exactly as the * gather-based emit does (agg_gather_key_col). radix_phase3_fn @@ -22109,6 +22179,9 @@ v2_emit:; if (!new_col || RAY_IS_ERR(new_col)) { agg_cols[a] = NULL; memset(&agg_outs[a], 0, sizeof(agg_outs[a])); + emit_oom = true; + if (new_col && !emit_err) emit_err = new_col; + else if (new_col) ray_error_free(new_col); continue; } /* SYM MIN/MAX/FIRST/LAST emit RAW cell ids accumulated from @@ -22129,6 +22202,14 @@ v2_emit:; }; } + if (emit_oom) { + for (uint32_t k = 0; k < n_keys; k++) if (key_cols[k]) ray_release(key_cols[k]); + for (uint32_t a = 0; a < n_aggs; a++) if (agg_cols[a]) ray_release(agg_cols[a]); + ray_release(result); + result = emit_err ? emit_err : ray_error("oom", NULL); + goto cleanup; + } + /* Pass 3: parallel key gather + agg result building from inline rows */ { radix_phase3_ctx_t p3ctx = { diff --git a/src/ops/internal.h b/src/ops/internal.h index 540b4c3fa..d1b639e24 100644 --- a/src/ops/internal.h +++ b/src/ops/internal.h @@ -61,6 +61,16 @@ * than ray_interrupted() does. */ #define RAY_CANCEL_ROWS ((int64_t)65536) +#ifdef DEBUG +/* Test seam (group.c): the nth allocation of the calling thread's own code + * (not the pool tasks it runs) while the outermost group (exec_group) runs + * fails; fired says whether it was reached, count how many the last + * unfired run made. nth 0 disarms. */ +void ray_group_alloc_fail_for_test(int64_t nth); +bool ray_group_alloc_fail_fired_for_test(void); +int64_t ray_group_alloc_count_for_test(void); +#endif + /* Test seam (DEBUG): a holistic aggregate's per-group buffers fail their * nth allocation on the calling thread. HOLISTIC_FAIL_ARM/_DISARM bound * the code that computes a median, quantile or mode column from its @@ -79,8 +89,8 @@ int64_t ray_holistic_fail_regions_for_test(void); #define HOLISTIC_FAIL_DISARM() do { } while (0) #endif #ifdef DEBUG -/* Test seam (group.c): the nth partition-table init of the v2 radix - * route's phase 2 from now, on any thread, fails as a failed rows +/* Test seam (group.c): the nth partition-table init of a radix route's + * phase 2 (v2 or classic) from now, on any thread, fails as a failed rows * allocation does (its slots allocated); with cancel it also raises the * cancel. nth 0 disarms. */ void ray_group_test_ht_rows_fail(int64_t nth, bool cancel); diff --git a/test/test_aware.c b/test/test_aware.c index 0a94d3688..45573547f 100644 --- a/test/test_aware.c +++ b/test/test_aware.c @@ -3683,6 +3683,107 @@ static test_result_t test_radix_cancel_keeps_nothing(void) { PASS(); } +/* An allocation that fails while a group on the radix route runs (an F64 + * key, alone or beside another) answers `oom` and keeps nothing, or the + * answer the group gives without the failure; never a crash or another + * answer. The radix emit skipped a key column it could not allocate + * (phase 3 then wrote through its NULL data: SIGSEGV) and an aggregate + * column (the answer lost that column), leaking the allocation's error; + * the classic route's phase 2 skipped a partition whose table it could not + * allocate (the answer lost its groups). The seam fails the nth + * allocation of the calling thread's own code while the outermost group + * runs (not of the pool tasks it steals, whose share varies from run to + * run), every n the group makes; a second seam fails the nth partition + * table init inside phase 2's tasks, on any thread. "Keeps nothing" is + * the process's allocation trace (every heap) over the run, the error's + * release and a GC. */ +static test_result_t test_group_alloc_fail(void) { + ray_pool_destroy(); + TEST_ASSERT_EQ_I(ray_pool_init_total(4), RAY_OK); + aw_eval_ok("(set F (table [k j v] (list (as 'F64 (% (* (til 262144) 7919) 3001)) (% (til 262144) 7) (til 262144))))"); + static const char* const qs[] = { + "(select {from: F by: k})", /* v2 radix */ + "(select {c: (count v) s: (sum v) from: F by: [k j]})", + "(select {m: (max v) from: F by: k})", /* classic radix */ + }; + int part_failed = 0; + for (size_t q = 0; q < sizeof qs / sizeof *qs; q++) { + char setq[200]; + snprintf(setq, sizeof setq, "(set R0 %s)", qs[q]); + aw_eval_ok(setq); + /* the route's allocations on this thread, armed past them */ + ray_group_alloc_fail_for_test((int64_t)1 << 40); + ray_t* r0 = aw_eval(qs[q]); + int64_t total = ray_group_alloc_count_for_test(); + ray_group_alloc_fail_for_test(0); + if (r0 && RAY_IS_ERR(r0)) ray_error_free(r0); else if (r0) ray_release(r0); + TEST_ASSERT_FMT(total > 0, "%s: the group made no allocation", qs[q]); + int failed = 0; + for (int64_t n = 1; n <= total; n++) { + ray_heap_gc(); + bool traced = ray_mem_trace_begin(); + ray_group_alloc_fail_for_test(n); + ray_t* got = aw_eval(qs[q]); + ray_group_alloc_fail_for_test(0); + if (got && RAY_IS_ERR(got)) { + char code[32]; + snprintf(code, sizeof code, "%s", ray_err_code(got)); + ray_error_free(got); + ray_heap_gc(); + ray_mem_trace_t tr = {0}; + if (traced) ray_mem_trace_end(&tr); + TEST_ASSERT_FMT(traced, "no allocation trace"); + TEST_ASSERT_FMT(strcmp(code, "oom") == 0, "%s, allocation %lld of %lld fails: %s, not oom", qs[q], (long long)n, (long long)total, code); + TEST_ASSERT_FMT(tr.net_bytes <= 0, "%s, allocation %lld of %lld fails: %lld bytes kept", qs[q], (long long)n, (long long)total, (long long)tr.net_bytes); + failed++; + continue; + } + if (traced) { ray_mem_trace_t t0; ray_mem_trace_end(&t0); } + TEST_ASSERT_NOT_NULL(got); + ray_env_set(ray_sym_intern("Q", 1), got); + ray_release(got); + ray_t* v = ray_eval_str("(and (== (count (ser Q)) (count (ser R0))) (all (== (ser Q) (ser R0))))"); + bool ok = v && !RAY_IS_ERR(v) && v->type == -RAY_BOOL && v->b8; + if (v && RAY_IS_ERR(v)) ray_error_free(v); else if (v) ray_release(v); + TEST_ASSERT_FMT(ok, "%s, allocation %lld of %lld fails: an answer, not the reference", qs[q], (long long)n, (long long)total); + } + TEST_ASSERT_FMT(failed > 0, "%s: no allocation failure answered oom", qs[q]); + /* inside phase 2's tasks: the nth partition-table init fails (its + * slots allocated, rows NULL), on whichever thread runs it */ + for (int64_t n = 1; n <= 32; n++) { + ray_heap_gc(); + bool traced = ray_mem_trace_begin(); + ray_group_test_ht_rows_fail(n, false); + ray_t* got = aw_eval(qs[q]); + ray_group_test_ht_rows_fail(0, false); + bool err = got && RAY_IS_ERR(got); + char code[32] = ""; + if (err) { snprintf(code, sizeof code, "%s", ray_err_code(got)); ray_error_free(got); } + ray_heap_gc(); + ray_mem_trace_t tr = {0}; + if (traced) ray_mem_trace_end(&tr); + if (!err) { + TEST_ASSERT_NOT_NULL(got); + ray_env_set(ray_sym_intern("Q", 1), got); + ray_release(got); + ray_t* v = ray_eval_str("(and (== (count (ser Q)) (count (ser R0))) (all (== (ser Q) (ser R0))))"); + bool ok = v && !RAY_IS_ERR(v) && v->type == -RAY_BOOL && v->b8; + if (v && RAY_IS_ERR(v)) ray_error_free(v); else if (v) ray_release(v); + TEST_ASSERT_FMT(ok, "%s, partition init %lld fails: an answer, not the reference", qs[q], (long long)n); + continue; + } + TEST_ASSERT_FMT(strcmp(code, "oom") == 0, "%s, partition init %lld fails: %s, not oom", qs[q], (long long)n, code); + TEST_ASSERT_FMT(tr.net_bytes <= 0, "%s, partition init %lld fails: %lld bytes kept", qs[q], (long long)n, (long long)tr.net_bytes); + part_failed++; + } + } + /* the v2 route falls back to the classic one on a failed init and + * answers; the classic route's failed init answers `oom` */ + TEST_ASSERT_FMT(part_failed > 0, "no partition init failure answered oom"); + aw_eval_ok("(set F 0)"); aw_eval_ok("(set R0 0)"); aw_eval_ok("(set Q 0)"); + PASS(); +} + /* #1780: a cancel that lands while a group runs answers `cancel`, never * `oom` and never an answer it did not finish. For each query, the nth * pool dispatch finds the query cancelled (mode 0: a cancel request; mode @@ -6801,6 +6902,7 @@ const test_entry_t aware_entries[] = { { "aware/sort_head_late_chunked", test_sort_head_late_chunked, aw_setup, aw_teardown }, { "aware/topk_cancel_at_dispatch", test_topk_cancel_at_dispatch, aw_setup, aw_teardown }, { "aware/group_cancel_status", test_group_cancel_status, aw_setup, aw_teardown }, + { "aware/group_alloc_fail", test_group_alloc_fail, aw_setup, aw_teardown }, { "aware/holistic_oom", test_holistic_oom, aw_setup, aw_teardown }, { "aware/radix_cancel_keeps_nothing", test_radix_cancel_keeps_nothing, aw_setup, aw_teardown }, { "aware/sel_compact_cancel_at_dispatch", test_sel_compact_cancel_at_dispatch, aw_setup, aw_teardown },