Impact
A single request to POST /verify/math can keep the server busy for minutes or longer. The cost bounds added for #353 (src/qwed_new/core/safe_parser.py) assume that a power whose base or exponent names a symbol stays lazy in SymPy. That is not true when the symbol cancels while the expression is evaluated. Separately, simplify has no cost bound of its own.
expression |
Time on main @ 4d0e4845 |
|---|---|
2+2 = 4 (control) |
0.01s |
2**((10000*x/x)**9) = 1 |
did not return within 30s |
(9**9999*x)**9999 = 1 |
did not return within 30s |
(x/x*9**9999)**9999 |
did not return within 30s (inside the parse) |
(x+1)**1000 = 1 |
about 20s, in simplify |
(x+1)**3000 = 1 |
about 75s, in simplify |
(x+1)**9999 = 1 |
did not return within 30s |
The /verify/math handler is async def and calls the parser and simplify directly, not in a thread. Reading the code, a request therefore blocks that worker's event loop, so the worker serves no other request until it finishes. Rate limiting counts requests, not CPU time.
Causes
- Cancellation.
x/xevaluates to 1 while the expression is parsed, so(10000*x/x)**9becomes10000**9, and SymPy expands2**(10000**9)exactly.- SymPy also distributes an integer power over a product, so
(9**9999*x)**9999raises a 9,543-digit integer to the power 9999. - The static gates read both as symbolic:
_static_valuereturns None for any name;_check_pow_costreturns early when the base is not static (line 198) or the exponent is not (line 208);_check_caret_chain_costdoes the same for^chains (line 237).
- These cases hang inside
safe_parse_expritself, so every caller of the parser is affected.
simplifycost.- A symbolic power within the exponent bound (10,000) is accepted.
simplify(left - right)(core/math_identity.py:50) andsimplify(parsed)(api/main.py:1079) then expand it. The time grows much faster than the degree: 0.9s at 100, 20s at 1,000, 75s at 3,000.
Affected
qwed (PyPI) through 8.0.0. safe_parser.py is unchanged since v8.0.0. Reproduced on main @ 4d0e4845.
Reachable with any valid API key from:
POST /verify/math, in both thea = bform and the plain-expression form;- math items in
POST /verify/batch(core/batch.py:376and:384). These run in a worker thread, so each one holds a thread rather than the event loop; - any local caller of
safe_parse_expr(cause 1).
Reproduction
import time
from qwed_new.core.math_identity import math_identity_result # what /verify/math calls for a = b
for expression in ["2+2 = 4", "(x+1)**1000 = 1", "2**((10000*x/x)**9) = 1"]:
start = time.perf_counter()
print(expression, math_identity_result(expression).status, round(time.perf_counter() - start, 1))
The last line does not return.
Patches
Fixed in 9.0.0 (413a8d3). Upgrade to 9.0.0 or later.
/verify/mathand batch math items run the parse andsimplifyin a pool of worker processes. A job pastQWED_MATH_TIMEOUT_SECONDS(default 10) isUNVERIFIABLE(math.timeout), and its worker is killed and replaced. When every worker is busy and the wait list is full, a request is refused at once (math.capacity). On Linux each worker has a memory limit (QWED_MATH_WORKER_MEMORY_MB, default 2048). Batch math items share a per-job budget (QWED_BATCH_MATH_BUDGET_SECONDS, default 120).- The handler waits for the pool on threads of its own, so a slow job no longer holds the event loop or the thread pool other routes share.
- The cost pre-gate parses with the parser's own namespace (see the addendum below).
- A math setting that is not a finite number in its range stops the server at start, with an error naming the setting.
On 9.0.0, with the default settings, the four expressions in the table that did not return within 30s are UNVERIFIABLE (math.timeout) after about 10s.
The time limit is on these two routes. safe_parse_expr itself, called directly or from other routes that use SymPy in the API process (natural-language math, consensus, the statistics and matrix helpers), has no time limit of its own.
Workarounds
Upgrade to 9.0.0. On older versions, restrict /verify/math and /verify/batch to trusted API keys, and run workers under a supervisor that restarts a worker that stops responding.
Source
Internal review, October 2026. It was found while handling an automated review comment on PR #558, which adds diff() and closes the same gap for derivatives only. Not previously disclosed.
Addendum: the cost pre-gate parses with SymPy's default namespace (re-checked on main @ b74ac00)
_check_unevaluated_sympy_cost (src/qwed_new/core/safe_parser.py:543-553) calls parse_expr(..., evaluate=False) with SymPy's default global namespace. The comment there explains why: a restricted global_dict makes evaluate=False raise. The cost of that choice is that every public SymPy callable is in scope during the pre-gate, and evaluate=False does not stop a call written in the input from running.
Probe, with sympy.test replaced by a recorder:
expression |
Recorder calls | Result |
|---|---|---|
test() + 2 y |
1 | SafeParserError afterwards |
test() + 2*y (control) |
0 | SafeParserError |
- Input that is valid Python goes through the AST allowlist first and never reaches this call with an unknown name.
- Implicit multiplication (
2 y) makesast.parsefail, so the input reaches the pre-gate, which then callstest. Unpatched, that issympy.test, which starts SymPy's own test suite on the worker. (This probe replaced it with a recorder and did not run it.) - No string-construction primitive is reachable through this namespace (none was found), so this is a cost and availability issue, not code execution. It belongs with the other "the cost bound does not hold" causes in this advisory.
Fixed in 9.0.0: the pre-gate parses with the parser's own names, plus the eight classes evaluate=False rewrites into (Symbol, Function, Integer, Float, Rational, Add, Mul, Pow), and empty builtins. On 9.0.0 both probe rows are refused as an unknown function before anything is called (0 recorder calls).
