Repository navigation
Description
What is the bug?
A PPL query stopped by plugins.ppl.query.timeout is reported as an internal server error. Nothing in the response, the metrics or the log identifies it as a timeout, so a client cannot tell an enforced deadline from a backend failure, and the request is counted in PPL_FAILED_REQ_COUNT_SYS as a plugin system fault.
There is a second, related problem: the deadline is only observed when the execution thread happens to be blocked on I/O. Both timeout handlers interrupt the execution thread but never cancel the CancellableTask they hold, while the Calcite scan loop's own cancellation check tests cancellableTask.isCancelled(). A CPU-bound query can therefore run well past its deadline.
How can one reproduce the bug?
- Lower the timeout:
PUT _plugins/_query/settings { "persistent": { "plugins.ppl.query.timeout": "1s" } } - Index enough data that a query takes several seconds. About 200k small documents over 5 shards is plenty.
- Run a query that spends its time in the engine rather than in a single search, for example:
source=my_index | eval k = concat(host, msg) | stats dc(k) by level | sort level
Observed, reproducibly, at about 1.02s against a query that takes roughly 5s when the timeout is not in the way:
{
"error": {
"context": { "stage": "executing", "stage_description": "Running the query" },
"reason": "java.sql.SQLException: exception while executing query: The task is cancelled.",
"type": "RuntimeException",
"code": "UNKNOWN"
},
"status": 500
}What is the expected behavior?
A query that hits its configured deadline should report a timeout, not a server fault:
- status 504, matching
OpenSearchTimeoutException.status()in core OpenSearchTimeoutExceptionas the error type, so the cause is identifiable without parsing a nested message- counted as a client-side outcome rather than a plugin system fault
And the deadline should be enforced regardless of what the execution thread is doing at the moment it expires.
What is the root cause?
Three separate things combine.
1. The complex-worker path has no timeout detection at all. plugins.sql.complex_worker_pool.enabled defaults to true, so any plan containing scripts is dispatched there. ThreadPoolExecutionDispatcher.dispatchInternal schedules its own timeout that calls executionThread.interrupt(), and then:
} catch (Exception e) {
LOG.error("Exception during task execution on complex pool", e);
if (failureListener != null) {
failureListener.onFailure(e);
}
} finally {
timeoutHandle.cancel();
cancelPoller.cancel();
Thread.interrupted();
...
}The exception is forwarded to the listener unexamined, and finally clears the interrupt flag, so nothing downstream can recover the signal either. The reproduction above takes this path, which is why the reported type is a bare RuntimeException.
2. On the sql-worker path, the detection condition does not match what arrives. OpenSearchQueryManager.schedule has:
} catch (Exception e) {
timeoutTask.cancel();
if (Thread.interrupted() || e.getCause() instanceof InterruptedException) {
throw new OpenSearchTimeoutException("Query execution timed out after " + timeout);
}
throw e;
}The InterruptedException does not reach here. BackgroundSearchScanner.getCurrentResponse converts it first:
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new TaskCancelledException("The task is cancelled.");
}Avatica then wraps that in a SQLException and the engine in a RuntimeException, so e.getCause() is a SQLException.
3. Neither timeout handler cancels the task. OpenSearchIndexEnumerator.moveNext checks cancellation on every row:
if (cancellableTask != null && cancellableTask.isCancelled()) {
throw new TaskCancelledException("The task is cancelled.");
}Since a timeout only interrupts the thread, this check never fires for a timeout — it fires only for external cancellation, such as a client disconnect through RestCancellableNodeClient. The timeout in the reproduction above was observed only because the thread happened to be blocked in nextBatchFuture.get() at that moment. Work that is CPU-bound in Calcite, with no fetch in flight, has no comparable checkpoint: the remaining interrupt checks are in OpenSearchIndexScan and in InterruptibleRelRule, which runs during planning.
Suggested direction
Having the timeout cancel the CancellableTask, rather than only interrupting the thread, addresses all three at once and uses machinery that already exists:
- the per-row
isCancelled()check inOpenSearchIndexEnumeratorstarts working for timeouts, which covers the CPU-bound case ThreadPoolExecutionDispatcher.scheduleCancellationPolleralready implements cancelled-task-to-thread-interrupt, so the I/O-blocked case keeps working- detection becomes deterministic: the handler knows whether it was the one that cancelled, instead of inferring it from an interrupt flag or an exception type
The status then follows from the exception type, and the request should be bucketed as a client-side outcome rather than a plugin system fault.
What is your host/environment?
A 3-node cluster, 5 shards, roughly 200k documents, SQL/PPL plugin with the Calcite engine enabled (the default). The two timeout handlers and the scan-loop cancellation check quoted above are unchanged on main.
Do you have any additional context?
Core's OpenSearchTimeoutException overrides status() to return GATEWAY_TIMEOUT, which is why 504 is the natural status for this once the failure is correctly identified as a timeout.
Metadata
Metadata
Assignees
Type
Projects
- StatusShow more project fieldsDone
Activity
Stop a PPL query at its timeout and report it as one
Stop a PPL query at its timeout and report it as one
Stop a PPL query at its timeout and report it as one
Stop a PPL query at its timeout and report it as one