mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(stepfunctions): bound JSONata evaluation by time and depth (#2697)
JSONata evaluation ran unbounded. A recursive expression with no base case pinned a core and left the execution RUNNING for ever: the library optimises the tail call, so it loops rather than overflowing the stack and no Error ever reaches the executor. Deep non-tail recursion did throw, but a StackOverflowError the evaluator's catch cannot see. Bound each expression with Frame#setRuntimeBounds, which the library has and Floci never called. Either bound raises a JSONata error, so the existing catch turns it into States.QueryEvaluationError, and the state fails through the same path as any other evaluation error: catchable by Catch, retryable by Retry. Five seconds and 500 levels sit deliberately above AWS. Measured with test-state, AWS refuses a non-tail-recursive function at a nesting depth of 125 and accepts it at 95, and the widest sequence it accepts evaluates here in about 100 ms, so no expression AWS runs can reach either bound. Closes #2667 Co-authored-by: Facundo Paredes <25681464+facuparedes@users.noreply.github.com>
This commit is contained in:
1 parent
ac94232910
commit
395401807b
3 files changed
+120
-3
No files matched your search
+69
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.NullNode;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.Timeout;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@@ -550,6 +551,74 @@ class JsonataEvaluatorTest {
|
||||
assertEquals("[1,2]", result.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* The expression from issue #2667. JSONata optimises the tail call, so it loops instead of
|
||||
* overflowing the stack: nothing is thrown, and before the time bound the execution stayed
|
||||
* RUNNING for ever with a core pinned. The evaluator is built with a 250 ms bound rather than
|
||||
* the shipped {@link JsonataEvaluator#EVALUATION_TIMEOUT_MILLIS} so the suite stays fast, and
|
||||
* the method timeout makes an unbounded evaluation fail the test instead of blocking the run.
|
||||
*/
|
||||
@Test
|
||||
@Timeout(30)
|
||||
void recursionWithNoBaseCaseFailsOnTheTimeBound() {
|
||||
JsonataEvaluator shortlyBounded =
|
||||
new JsonataEvaluator(objectMapper, 250, JsonataEvaluator.MAX_EVALUATION_DEPTH);
|
||||
JsonNode statesVar = objectMapper.createObjectNode();
|
||||
|
||||
AslExecutor.FailStateException ex = assertThrows(AslExecutor.FailStateException.class,
|
||||
() -> shortlyBounded.evaluate("{% ($f := function($x){ $f($x+1) }; $f(1)) %}", statesVar));
|
||||
|
||||
assertEquals("States.QueryEvaluationError", ex.error);
|
||||
assertEquals("Expression evaluation timeout: Check for infinite loop", ex.cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursion the library cannot optimise grows the evaluation nesting instead of looping, and
|
||||
* without a depth bound it ends in a StackOverflowError, which is an Error and so escapes the
|
||||
* evaluator's catch. The shipped bound is what turns it into a state failure.
|
||||
*
|
||||
* <p>The cause is AWS's own string for this failure, followed by the {@code Depth=N max=M}
|
||||
* suffix that names the bound that was hit. AWS writes one space after the full stop where
|
||||
* the library writes two.
|
||||
*/
|
||||
@Test
|
||||
@Timeout(30)
|
||||
void nonTailRecursionFailsOnTheDepthBound() {
|
||||
JsonNode statesVar = objectMapper.createObjectNode();
|
||||
|
||||
AslExecutor.FailStateException ex = assertThrows(AslExecutor.FailStateException.class,
|
||||
() -> evaluator.evaluate(
|
||||
"{% ($f := function($x){ $x <= 0 ? 0 : 1 + $f($x-1) }; $f(1000000)) %}", statesVar));
|
||||
|
||||
assertEquals("States.QueryEvaluationError", ex.error);
|
||||
assertEquals("Stack overflow error: Check for non-terminating recursive"
|
||||
+ " function. Consider rewriting as tail-recursive. Depth=501 max=500", ex.cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* The boundary from below: the risk of a bound is that it fails a working state machine, so
|
||||
* these must all still evaluate. Measured with {@code aws stepfunctions test-state}, AWS
|
||||
* returns 30 for the first and 800000 for the second, which makes them the ceiling of what
|
||||
* AWS itself accepts on each axis: it refuses the recursion at n=40 (nesting depth 125) and
|
||||
* the sequence at 900,000 elements.
|
||||
*
|
||||
* <p>The last two AWS refuses on its memory limit and Floci evaluates, which is where the
|
||||
* bounds are deliberately looser than AWS rather than tighter.
|
||||
*/
|
||||
@Test
|
||||
@Timeout(60)
|
||||
void expensiveButLegitimateExpressionsStayWithinTheShippedBounds() {
|
||||
JsonNode statesVar = objectMapper.createObjectNode();
|
||||
String recursionHead = "{% ($f := function($x){ $x <= 0 ? 0 : 1 + $f($x-1) }; $f(";
|
||||
|
||||
assertEquals(30, evaluator.evaluate(recursionHead + "30)) %}", statesVar).asInt());
|
||||
assertEquals(800000, evaluator.evaluate("{% [1..800000] ~> $count() %}", statesVar).asInt());
|
||||
|
||||
assertEquals(160, evaluator.evaluate(recursionHead + "160)) %}", statesVar).asInt());
|
||||
assertEquals(40000200000L,
|
||||
evaluator.evaluate("{% $sum($map([1..200000], function($v){$v * 2})) %}", statesVar).asLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
void string_writesAWholeNumberInFullBelowTheExponentBoundary() {
|
||||
// dashjoin stops writing digits at Long.MAX_VALUE, 9.223372036854776E18, and prints
|
||||
|
||||
Reference in new issue
Block a user