Skip to content

Commit 312185c

Browse files
committed
fix(test): stop budget-env poisoning from the backpressure test
cbm_mem_init is init-once (guarded by g_initialized), so the backpressure test's setenv(CBM_MEM_BUDGET_MB=1) + re-init 'restore' was a silent no-op: whichever init ran first fixed the budget for the whole process. In the full runner this test's init won, locking a 1 MB budget that leaked into every later budget consumer — mem_over_budget_low_rss red on every platform, and all subsequent pipeline tests silently running with backpressure permanently engaged. Add cbm_mem_set_budget_for_tests(): writes the budget directly without touching the init guard. The test now saves the caller-visible budget, sets 1 MB via the hook, and restores before asserting. Env dance removed. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent fea266f commit 312185c

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

src/foundation/mem.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ size_t cbm_mem_budget(void) {
234234
return g_budget;
235235
}
236236

237+
void cbm_mem_set_budget_for_tests(size_t bytes) {
238+
g_budget = bytes;
239+
}
240+
237241
bool cbm_mem_over_budget(void) {
238242
size_t rss = cbm_mem_rss();
239243
check_pressure(rss);

src/foundation/mem.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ size_t cbm_mem_peak_rss(void);
2929
/* Total budget in bytes. */
3030
size_t cbm_mem_budget(void);
3131

32+
/* TEST HOOK: overwrite the budget directly, bypassing cbm_mem_init's
33+
* init-once guard (a setenv+re-init dance in tests is a silent no-op once
34+
* some earlier init won the guard — the poisoned budget then leaks into
35+
* every later budget consumer in the process). Does NOT flip the init
36+
* guard: a later cbm_mem_init still initializes normally. Callers must
37+
* save cbm_mem_budget() first and restore it before their assertions.
38+
* Never call from production code. */
39+
void cbm_mem_set_budget_for_tests(size_t bytes);
40+
3241
/* Returns true if current RSS exceeds the budget. */
3342
bool cbm_mem_over_budget(void);
3443

tests/test_pipeline.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6577,9 +6577,16 @@ TEST(pipeline_backpressure_futile_nap_disengages) {
65776577
fclose(f);
65786578
}
65796579

6580-
/* 1 MB budget: over-budget on every pull, unreclaimable by napping. */
6581-
cbm_setenv("CBM_MEM_BUDGET_MB", "1", 1);
6582-
cbm_mem_init(0);
6580+
/* 1 MB budget: over-budget on every pull, unreclaimable by napping.
6581+
* Set via the test hook, NOT setenv + cbm_mem_init: the init-once guard
6582+
* makes any re-init keep whatever budget the FIRST in-process init
6583+
* computed. The old env dance either failed to apply the 1 MB budget
6584+
* (some earlier test's init won the guard) or applied it permanently
6585+
* (this test's init won) — the "restore" re-init was then a silent
6586+
* no-op and the 1 MB budget leaked into every later budget consumer
6587+
* in the runner (mem_over_budget_low_rss went red suite-order-wide). */
6588+
size_t saved_budget = cbm_mem_budget();
6589+
cbm_mem_set_budget_for_tests((size_t)1024 * 1024);
65836590
ASSERT_TRUE(cbm_mem_budget() > 0);
65846591
ASSERT_TRUE(cbm_mem_over_budget());
65856592

@@ -6589,9 +6596,8 @@ TEST(pipeline_backpressure_futile_nap_disengages) {
65896596
int rc = cbm_pipeline_run(p);
65906597
long cycles = cbm_pp_bp_nap_cycles();
65916598

6592-
/* Restore the tests' default budget-off state BEFORE asserting. */
6593-
cbm_unsetenv("CBM_MEM_BUDGET_MB");
6594-
cbm_mem_init(0);
6599+
/* Restore the caller-visible budget BEFORE asserting. */
6600+
cbm_mem_set_budget_for_tests(saved_budget);
65956601
cbm_pipeline_free(p);
65966602
teardown_test_repo();
65976603

0 commit comments

Comments
 (0)