mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 11:43:20 +00:00
fix(javascript): refuse member-mutation work beyond a fixed allowance (#11144)
* fix(javascript): refuse member-mutation work beyond a fixed allowance Member-mutation indexing retained every scheduled alias state until its query finished, with no bound on the phase. Conditional self-selections before one member write (#8294) multiplied those states until the worker heap was exhausted. Charge each scheduled alias state to a per-phase work meter. Past 524,288 states the phase is refused with a resource_constraint that names the fixed work allowance. The application and web bundle workers carry the reported limit to the parent, which rebuilds the refusal with that allowance instead of heap advice. Refs #8294 #6127 #6344 #7170 #7510 #8298 * test(javascript): prove the member-mutation meter spans the phase
This commit is contained in:
1 parent
4eafd9dee5
commit
d07ab79f9b
12 files changed
+371
-34
No files matched your search
@@ -0,0 +1,60 @@
|
||||
import { AnalysisResourceConstraintError } from "../analysisErrorCore.js";
|
||||
import {
|
||||
fixedAllowanceOptions,
|
||||
type FixedAllowance,
|
||||
} from "../fixedAllowanceRemediation.js";
|
||||
|
||||
/**
|
||||
* Alias states one source's member-mutation phase may schedule. Each state is
|
||||
* retained until its query finishes, at roughly 1–2 KiB, so the allowance stays
|
||||
* well below the default 1 GiB worker heap. Published bundles up to 395 KB
|
||||
* schedule at most about 123,000 states.
|
||||
*/
|
||||
export const JAVASCRIPT_MEMBER_MUTATION_WORK_ALLOWANCE = 524_288;
|
||||
|
||||
/** Reported-limit key that names this allowance across worker boundaries. */
|
||||
export const JAVASCRIPT_MEMBER_MUTATION_WORK_LIMIT =
|
||||
"javascript_member_mutation_alias_state_limit";
|
||||
|
||||
const ALLOWANCE_NAME = "The JavaScript member-mutation work allowance";
|
||||
|
||||
/** Fixed allowance facts for a refusal rebuilt from reported limits. */
|
||||
export const javaScriptSemanticWorkAllowance = (
|
||||
limits: Readonly<Record<string, unknown>> | null | undefined,
|
||||
):
|
||||
| { readonly retryable: false; readonly fixedAllowance: FixedAllowance }
|
||||
| undefined => {
|
||||
const limit = limits?.[JAVASCRIPT_MEMBER_MUTATION_WORK_LIMIT];
|
||||
return typeof limit === "number"
|
||||
? fixedAllowanceOptions(ALLOWANCE_NAME, "work", {
|
||||
limit,
|
||||
unit: "alias states",
|
||||
})
|
||||
: undefined;
|
||||
};
|
||||
|
||||
/**
|
||||
* Work meter for one analysis phase. Charging past the fixed allowance refuses
|
||||
* the phase before its retained state can exhaust the worker heap; a partial
|
||||
* mutation index would let later reads keep stale values.
|
||||
*/
|
||||
export class JavaScriptSemanticWorkMeter {
|
||||
#used = 0;
|
||||
|
||||
constructor(
|
||||
readonly allowance: number = JAVASCRIPT_MEMBER_MUTATION_WORK_ALLOWANCE,
|
||||
) {}
|
||||
|
||||
charge(units = 1): void {
|
||||
this.#used += units;
|
||||
if (this.#used <= this.allowance) return;
|
||||
const limits = { [JAVASCRIPT_MEMBER_MUTATION_WORK_LIMIT]: this.allowance };
|
||||
throw new AnalysisResourceConstraintError(
|
||||
"analyze_javascript_semantics",
|
||||
"cpu",
|
||||
`JavaScript member-mutation analysis scheduled more than ${String(this.allowance)} alias states for one source`,
|
||||
limits,
|
||||
javaScriptSemanticWorkAllowance(limits),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user