From bb381132403f4bd611085831419b7823de35f321 Mon Sep 17 00:00:00 2001 From: Stacey Levine Date: Thu, 19 Nov 2026 10:01:39 -0500 Subject: [PATCH 1/3] fix(mssql): stop SQL Server queries at their timeout The action timeout never stopped a SQL Server query. The plugin runs the query as a blocking call inside the reactive chain, before the timer of .timeout() starts, so the timer never fired. A query that ran longer than its timeout succeeded late, and a query with endless results, such as SELECT 1; WHILE 1 = 1 SELECT 1, held a worker thread and a pooled connection forever. This was true before the driver upgrade (#42639) too. #42639 moved the endless reading into the plugin's own loop, which reads later results to find their errors. - Set the JDBC query timeout from the action timeout, rounded up to whole seconds. The driver cancels the query on SQL Server when it runs out. - Check the same deadline while reading rows and later results. When it passes, cancel the query and fail with a timeout error that says statements that already ran may have been saved. - Report the driver's timeout during that reading as a timeout, not as a failed statement. Behaviour change: a SQL Server query that runs longer than its timeout (default 10 seconds, at most 60) now fails. Raise the query's timeout to allow it more time. Co-Authored-By: Claude Opus 5.5 --- .../com/external/plugins/MssqlPlugin.java | 14 +- .../exceptions/MssqlErrorMessages.java | 3 + .../plugins/utils/MssqlExecuteUtils.java | 65 +++++++++- .../plugins/MssqlDriverBehaviourTest.java | 70 ++++++++++ .../plugins/utils/MssqlExecuteUtilsTest.java | 122 ++++++++++++++++++ 5 files changed, 269 insertions(+), 5 deletions(-) create mode 100644 app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/utils/MssqlExecuteUtilsTest.java diff --git a/app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/MssqlPlugin.java b/app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/MssqlPlugin.java index 7adbc317728..daf0f8e16d0 100644 --- a/app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/MssqlPlugin.java +++ b/app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/MssqlPlugin.java @@ -69,6 +69,7 @@ import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.concurrent.TimeUnit; import java.util.stream.IntStream; import static com.appsmith.external.constants.ActionConstants.ACTION_CONFIGURATION_BODY; @@ -268,9 +269,17 @@ public Mono executeCommon( MessageFormat.format("Before executing Mssql query [{0}]", query), hikariDSConnection); ; + // The reactive timeout below cannot interrupt this blocking call: it reports the timeout only + // after the call returns. So the query gets the same timeout here. The driver cancels the + // query on SQL Server when it runs out, and the plugin stops reading results at the deadline. + final int timeoutMillis = actionConfiguration.getTimeoutInMillisecond(); + final int queryTimeoutSeconds = MssqlExecuteUtils.queryTimeoutSeconds(timeoutMillis); + final long deadlineNanos = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeoutMillis); + try { if (FALSE.equals(preparedStatement)) { statement = sqlConnectionFromPool.createStatement(); + statement.setQueryTimeout(queryTimeoutSeconds); isResultSet = statement.execute(query); resultSet = statement.getResultSet(); } else { @@ -289,6 +298,7 @@ public Mono executeCommon( parameters.get(i).getKey(), parameters.get(i).getValue()))); + preparedQuery.setQueryTimeout(queryTimeoutSeconds); isResultSet = preparedQuery.execute(); resultSet = preparedQuery.getResultSet(); } @@ -300,7 +310,9 @@ public Mono executeCommon( isResultSet, preparedStatement, statement, - preparedQuery); + preparedQuery, + timeoutMillis, + deadlineNanos); } catch (SQLException e) { return Mono.error(new AppsmithPluginException( diff --git a/app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/exceptions/MssqlErrorMessages.java b/app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/exceptions/MssqlErrorMessages.java index d364b6b699f..a51cea6cbe7 100644 --- a/app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/exceptions/MssqlErrorMessages.java +++ b/app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/exceptions/MssqlErrorMessages.java @@ -11,6 +11,9 @@ public class MssqlErrorMessages extends BasePluginErrorMessages { public static final String QUERY_EXECUTION_FAILED_ERROR_MSG = "Your query failed to execute. Please check more information in the error details."; + public static final String QUERY_TIMEOUT_ERROR_MSG = "The query ran longer than its timeout of %d ms, so Appsmith" + + " stopped it. Statements in the query that already ran may have been saved."; + public static final String QUERY_PREPARATION_FAILED_ERROR_MSG = "Query preparation failed while inserting value: %s" + " for binding: {{%s}}. Please check the query again."; diff --git a/app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/utils/MssqlExecuteUtils.java b/app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/utils/MssqlExecuteUtils.java index 2975123d2af..eeeec372688 100644 --- a/app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/utils/MssqlExecuteUtils.java +++ b/app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/utils/MssqlExecuteUtils.java @@ -1,5 +1,6 @@ package com.external.plugins.utils; +import com.external.plugins.exceptions.MssqlErrorMessages; import microsoft.sql.DateTimeOffset; import org.apache.commons.lang3.ObjectUtils; @@ -8,6 +9,7 @@ import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; +import java.sql.SQLTimeoutException; import java.sql.Statement; import java.text.MessageFormat; import java.time.OffsetDateTime; @@ -82,9 +84,13 @@ public static void populateRowsAndColumns( boolean isResultSet, Boolean preparedStatement, Statement statement, - PreparedStatement preparedQuery) + PreparedStatement preparedQuery, + int timeoutMillis, + long deadlineNanos) throws SQLException { + Statement executed = FALSE.equals(preparedStatement) ? statement : preparedQuery; + if (!isResultSet) { Object updateCount = FALSE.equals(preparedStatement) ? ObjectUtils.defaultIfNull(statement.getUpdateCount(), 0) @@ -97,6 +103,8 @@ public static void populateRowsAndColumns( columnsList.addAll(getColumnsListForJdbcPlugin(metaData)); while (resultSet.next()) { + stopAtDeadline(executed, timeoutMillis, deadlineNanos); + // Use `LinkedHashMap` here so that the column ordering is preserved in the response. Map row = new LinkedHashMap<>(colCount); @@ -150,7 +158,43 @@ public static void populateRowsAndColumns( } } - drainRemainingResults(FALSE.equals(preparedStatement) ? statement : preparedQuery); + drainRemainingResults(executed, timeoutMillis, deadlineNanos); + } + + /** + * Returns the JDBC query timeout, in whole seconds, for an action timeout in milliseconds. Rounds up, so the + * driver never stops a query before the action's own timeout. JDBC treats 0 as no timeout, so the result is at + * least 1. + */ + public static int queryTimeoutSeconds(int timeoutMillis) { + return (int) Math.max(1, (timeoutMillis + 999L) / 1000); + } + + /** + * Cancels the query on SQL Server and fails it when the action's timeout has passed. The driver's own query + * timeout should stop the query first. This check does not depend on it. + */ + private static void stopAtDeadline(Statement statement, int timeoutMillis, long deadlineNanos) throws SQLException { + if (System.nanoTime() - deadlineNanos < 0) { + return; + } + DeadlineReached timeout = new DeadlineReached(timeoutMillis); + if (statement != null) { + try { + statement.cancel(); + } catch (SQLException e) { + // Still report the timeout. Closing the statement afterwards ends the query. + timeout.addSuppressed(e); + } + } + throw timeout; + } + + /** The plugin's own timeout error, so that it is not reported as a failed statement. */ + private static final class DeadlineReached extends SQLTimeoutException { + private DeadlineReached(int timeoutMillis) { + super(String.format(MssqlErrorMessages.QUERY_TIMEOUT_ERROR_MSG, timeoutMillis)); + } } /** @@ -159,27 +203,40 @@ public static void populateRowsAndColumns( * {@code UPDATE ...; INSERT INTO missing_table ...} reported success. Other statements may already have run: the * ones before the error, and, because SQL Server continues after many statement errors, some after it. Without an * explicit transaction they are committed. So the error says so. + * + *

A query can return results without end, for example {@code WHILE 1 = 1 SELECT 1}. So the reading stops at + * the action's timeout: the query is cancelled on SQL Server and fails with a timeout error. */ - private static void drainRemainingResults(Statement statement) throws SQLException { + private static void drainRemainingResults(Statement statement, int timeoutMillis, long deadlineNanos) + throws SQLException { if (statement == null) { return; } try { while (true) { + stopAtDeadline(statement, timeoutMillis, deadlineNanos); if (statement.getMoreResults()) { // A later SELECT can fail while its rows are read, for example on a divide by zero in row 5. // Step through its rows (they are not returned) so that error surfaces too. try (ResultSet laterRows = statement.getResultSet()) { while (laterRows != null && laterRows.next()) { - // Nothing to keep. + stopAtDeadline(statement, timeoutMillis, deadlineNanos); } } } else if (statement.getUpdateCount() == -1) { break; } } + } catch (DeadlineReached e) { + throw e; } catch (SQLException e) { + if (System.nanoTime() - deadlineNanos >= 0) { + // The driver's own query timeout stopped the query. Report a timeout, not a failed statement. + DeadlineReached timeout = new DeadlineReached(timeoutMillis); + timeout.initCause(e); + throw timeout; + } throw new SQLException( "A statement in this query failed. Other statements in the query may already have run." + (e.getMessage() == null ? "" : " " + e.getMessage()), diff --git a/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/MssqlDriverBehaviourTest.java b/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/MssqlDriverBehaviourTest.java index a8f9121ced1..a278969f978 100644 --- a/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/MssqlDriverBehaviourTest.java +++ b/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/MssqlDriverBehaviourTest.java @@ -27,6 +27,7 @@ import java.io.IOException; import java.io.InputStream; import java.sql.SQLException; +import java.time.Duration; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; @@ -497,4 +498,73 @@ public void connectionUrl_turnsOffTheNativeVectorType() { connectionPool.close(); } } + + private static Stream endlessLaterResults() { + return Stream.of(true, false) + .flatMap(prepared -> Stream.of( + // Endless result sets after the first. + Arguments.of( + "SELECT 1 AS first_result; WHILE 1 = 1 SELECT 1 AS later_result", + prepared, + "ran longer than its timeout of 3000 ms"), + // Endless update counts after the first. + Arguments.of( + "SELECT 1 AS first_result; WHILE 1 = 1 UPDATE driver_dml SET name = name", + prepared, + "ran longer than its timeout of 3000 ms"), + // One statement that runs longer than the timeout. The driver's query timeout stops it. + Arguments.of("WAITFOR DELAY '00:00:20'", prepared, "The query has timed out"))); + } + + // AIP-FD-38: a query that runs past its timeout must not hold a pooled connection and a worker thread. The + // reactive timeout cannot interrupt the blocking JDBC call, so before the fix such a query ran on: the endless + // ones forever, because the plugin reads the later results to find errors (APP-16536). + @ParameterizedTest(name = "{0}, prepared statement {1}") + @MethodSource("endlessLaterResults") + public void endlessLaterResults_stopAtTheQueryTimeout(String sql, boolean preparedStatement, String expectedError) + throws InterruptedException { + HikariDataSource connectionPool = mssqlPluginExecutor + .datasourceCreate(createDatasourceConfiguration(container)) + .block(); + assertNotNull(connectionPool); + + try { + ActionConfiguration actionConfiguration = new ActionConfiguration(); + actionConfiguration.setBody(sql); + actionConfiguration.setTimeoutInMillisecond("3000"); + actionConfiguration.setPluginSpecifiedTemplates( + List.of(new Property("preparedStatement", String.valueOf(preparedStatement)))); + + // Before the fix, this call never returned: the reactive timeout cannot interrupt the blocking read. + Instant started = Instant.now(); + ActionExecutionResult result = mssqlPluginExecutor + .executeParameterized( + connectionPool, + new ExecuteActionDTO(), + createDatasourceConfiguration(container), + actionConfiguration) + .block(Duration.ofSeconds(30)); + Duration took = Duration.between(started, Instant.now()); + + assertNotNull(result); + assertFalse(result.getIsExecutionSuccess(), () -> "the query should fail: " + result.getBody()); + assertTrue( + String.valueOf(result.getBody()).contains(expectedError), + () -> "expected \"" + expectedError + "\" in: " + result.getBody()); + assertTrue(took.compareTo(Duration.ofSeconds(10)) < 0, () -> "the query stopped only after " + took); + + // The connection goes back to the pool shortly after the timeout. + Instant giveUp = Instant.now().plusSeconds(10); + while (connectionPool.getHikariPoolMXBean().getActiveConnections() > 0 + && Instant.now().isBefore(giveUp)) { + Thread.sleep(200); + } + assertEquals( + 0, + connectionPool.getHikariPoolMXBean().getActiveConnections(), + "the query still holds its connection after the timeout"); + } finally { + connectionPool.close(); + } + } } diff --git a/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/utils/MssqlExecuteUtilsTest.java b/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/utils/MssqlExecuteUtilsTest.java new file mode 100644 index 00000000000..2e38db8d841 --- /dev/null +++ b/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/utils/MssqlExecuteUtilsTest.java @@ -0,0 +1,122 @@ +package com.external.plugins.utils; + +import com.external.plugins.exceptions.MssqlErrorMessages; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.mockito.Mockito; + +import java.sql.SQLException; +import java.sql.SQLTimeoutException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import static java.lang.Boolean.FALSE; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class MssqlExecuteUtilsTest { + + private static final int TIMEOUT_MILLIS = 3000; + + private static final String TIMEOUT_MESSAGE = + String.format(MssqlErrorMessages.QUERY_TIMEOUT_ERROR_MSG, TIMEOUT_MILLIS); + + @ParameterizedTest + @CsvSource({"1, 1", "999, 1", "1000, 1", "1001, 2", "10000, 10", "60000, 60", "0, 1"}) + void should_roundUpToWholeSeconds_when_convertingTheActionTimeout(int timeoutMillis, int expectedSeconds) { + // Given + // An action timeout in milliseconds. + + // When + int seconds = MssqlExecuteUtils.queryTimeoutSeconds(timeoutMillis); + + // Then + assertThat(seconds).isEqualTo(expectedSeconds); + } + + @Test + void should_cancelAndFailWithTheTimeout_when_laterResultsDoNotEndBeforeTheDeadline() throws SQLException { + // Given + // An update count first, then results without end, and a deadline that has passed. + Statement statement = Mockito.mock(Statement.class); + when(statement.getUpdateCount()).thenReturn(1); + when(statement.getMoreResults()).thenReturn(true); + long deadlinePassed = System.nanoTime() - TimeUnit.SECONDS.toNanos(1); + + // When + Throwable error = catchThrowable(() -> populate(statement, deadlinePassed)); + + // Then + assertThat(error).isInstanceOf(SQLTimeoutException.class).hasMessage(TIMEOUT_MESSAGE); + verify(statement).cancel(); + } + + @Test + void should_reportATimeout_when_theDriverStopsTheQueryAfterTheDeadline() throws SQLException { + // Given + // The driver's own query timeout ends the query while the later results are read. + Statement statement = Mockito.mock(Statement.class); + when(statement.getUpdateCount()).thenReturn(1); + long deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(50); + when(statement.getMoreResults()).thenAnswer(invocation -> { + Thread.sleep(100); + throw new SQLException("The query has timed out.", "HY008"); + }); + + // When + Throwable error = catchThrowable(() -> populate(statement, deadline)); + + // Then + assertThat(error).isInstanceOf(SQLTimeoutException.class).hasMessage(TIMEOUT_MESSAGE); + } + + @Test + void should_reportTheFailedStatement_when_aLaterStatementFailsBeforeTheDeadline() throws SQLException { + // Given + Statement statement = Mockito.mock(Statement.class); + when(statement.getUpdateCount()).thenReturn(1); + when(statement.getMoreResults()).thenThrow(new SQLException("Invalid object name 'missing'.", "S0002")); + long deadline = System.nanoTime() + TimeUnit.MINUTES.toNanos(1); + + // When + Throwable error = catchThrowable(() -> populate(statement, deadline)); + + // Then + assertThat(error) + .isNotInstanceOf(SQLTimeoutException.class) + .hasMessageStartingWith("A statement in this query failed.") + .hasMessageContaining("Invalid object name 'missing'."); + verify(statement, never()).cancel(); + } + + @Test + void should_readEveryResult_when_theyEndBeforeTheDeadline() throws SQLException { + // Given + // Two more update counts, then the end of the results. + Statement statement = Mockito.mock(Statement.class); + when(statement.getMoreResults()).thenReturn(false); + when(statement.getUpdateCount()).thenReturn(1, 2, 3, -1); + long deadline = System.nanoTime() + TimeUnit.MINUTES.toNanos(1); + + // When + List> rows = populate(statement, deadline); + + // Then + assertThat(rows).containsExactly(Map.of("affectedRows", 1)); + verify(statement, never()).cancel(); + } + + private static List> populate(Statement statement, long deadlineNanos) throws SQLException { + List> rows = new ArrayList<>(); + MssqlExecuteUtils.populateRowsAndColumns( + rows, new ArrayList<>(), null, false, FALSE, statement, null, TIMEOUT_MILLIS, deadlineNanos); + return rows; + } +} From f08ce355321d88180526a551cc2752c5ea79ada3 Mon Sep 17 00:00:00 2001 From: Stacey Levine Date: Thu, 19 Nov 2026 12:12:47 -0500 Subject: [PATCH 2/3] fix(mssql): cancel SQL Server queries at their timeout from a timer Review of the timeout fix found two ways a query could still run past its timeout: - After SQL Server sends a first response early, the driver no longer runs its own query timeout. A later statement that waits, such as WAITFOR, then blocked a read of the results, and the checks between reads could not interrupt it. A timer now cancels the query at the deadline, on its own virtual thread so that a slow cancel does not delay others. The timer is stopped before the connection goes back to the pool, and a stopped timer leaves the queue at once. - An ordinary SQL error after the deadline was reported as a timeout without cancelling the query. It now cancels the query first. Any error after the deadline is reported with the plugin's timeout message. Co-Authored-By: Claude Opus 5.5 --- .../com/external/plugins/MssqlPlugin.java | 22 ++++- .../plugins/utils/MssqlExecuteUtils.java | 89 ++++++++++++++++--- .../plugins/MssqlDriverBehaviourTest.java | 12 ++- .../plugins/utils/MssqlExecuteUtilsTest.java | 64 +++++++++++++ 4 files changed, 171 insertions(+), 16 deletions(-) diff --git a/app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/MssqlPlugin.java b/app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/MssqlPlugin.java index daf0f8e16d0..1f2f5a4cac0 100644 --- a/app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/MssqlPlugin.java +++ b/app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/MssqlPlugin.java @@ -69,6 +69,7 @@ import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import java.util.stream.IntStream; @@ -270,16 +271,20 @@ public Mono executeCommon( ; // The reactive timeout below cannot interrupt this blocking call: it reports the timeout only - // after the call returns. So the query gets the same timeout here. The driver cancels the - // query on SQL Server when it runs out, and the plugin stops reading results at the deadline. + // after the call returns. So the query gets the same timeout here: + // - the driver's query timeout, while SQL Server has not answered yet; + // - a timer that cancels the query at the deadline, while a read of later results blocks; + // - checks between reads, which stop a query that returns results without end. final int timeoutMillis = actionConfiguration.getTimeoutInMillisecond(); final int queryTimeoutSeconds = MssqlExecuteUtils.queryTimeoutSeconds(timeoutMillis); final long deadlineNanos = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeoutMillis); + ScheduledFuture cancelAtDeadline = null; try { if (FALSE.equals(preparedStatement)) { statement = sqlConnectionFromPool.createStatement(); statement.setQueryTimeout(queryTimeoutSeconds); + cancelAtDeadline = MssqlExecuteUtils.cancelAtDeadline(statement, deadlineNanos); isResultSet = statement.execute(query); resultSet = statement.getResultSet(); } else { @@ -299,6 +304,7 @@ public Mono executeCommon( parameters.get(i).getValue()))); preparedQuery.setQueryTimeout(queryTimeoutSeconds); + cancelAtDeadline = MssqlExecuteUtils.cancelAtDeadline(preparedQuery, deadlineNanos); isResultSet = preparedQuery.execute(); resultSet = preparedQuery.getResultSet(); } @@ -315,13 +321,23 @@ public Mono executeCommon( deadlineNanos); } catch (SQLException e) { + // After the deadline, the error comes from the cancel or the driver's query timeout. + String errorMessage = MssqlExecuteUtils.isPastDeadline(deadlineNanos) + ? String.format(MssqlErrorMessages.QUERY_TIMEOUT_ERROR_MSG, timeoutMillis) + : e.getMessage(); return Mono.error(new AppsmithPluginException( MssqlPluginError.QUERY_EXECUTION_FAILED, MssqlErrorMessages.QUERY_EXECUTION_FAILED_ERROR_MSG, - e.getMessage(), + errorMessage, "SQLSTATE: " + e.getSQLState())); } finally { + // Stop the timer before the connection goes back to the pool, so that it never cancels + // a statement on a connection another query is using. + if (cancelAtDeadline != null) { + cancelAtDeadline.cancel(false); + } + // Log HikariCP status logHikariCPStatus( MessageFormat.format("After executing Mssql query [{0}]", query), diff --git a/app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/utils/MssqlExecuteUtils.java b/app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/utils/MssqlExecuteUtils.java index eeeec372688..66e29e1f573 100644 --- a/app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/utils/MssqlExecuteUtils.java +++ b/app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/utils/MssqlExecuteUtils.java @@ -20,6 +20,9 @@ import java.util.Locale; import java.util.Map; import java.util.Set; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; import static com.appsmith.external.helpers.PluginUtils.getColumnsListForJdbcPlugin; import static com.appsmith.external.helpers.PluginUtils.safelyCloseSingleConnectionFromHikariCP; @@ -170,24 +173,89 @@ public static int queryTimeoutSeconds(int timeoutMillis) { return (int) Math.max(1, (timeoutMillis + 999L) / 1000); } + /** Returns true when the action's timeout has passed. */ + public static boolean isPastDeadline(long deadlineNanos) { + return System.nanoTime() - deadlineNanos >= 0; + } + + /** + * Cancels the statement on SQL Server at the deadline, from another thread. A read of the results can block + * inside the driver, for example while a later statement runs {@code WAITFOR}, and the driver's own query + * timeout no longer runs once the first response has arrived. The checks between reads cannot interrupt such a + * read. Cancelling ends it with an error. + * + *

The caller must cancel the returned task before it closes the statement and returns the connection to the + * pool. + */ + public static ScheduledFuture cancelAtDeadline(Statement statement, long deadlineNanos) { + long delayNanos = Math.max(0, deadlineNanos - System.nanoTime()); + // The timer thread only starts the cancel. Sending it to a stalled server can block, and that must not + // delay the cancels of other queries. + return CANCEL_TIMER.schedule( + () -> CANCEL_THREADS.start(() -> { + try { + if (!statement.isClosed()) { + statement.cancel(); + } + } catch (SQLException e) { + // The statement finished or was closed meanwhile. Nothing is left to cancel. + } + }), + delayNanos, + TimeUnit.NANOSECONDS); + } + /** - * Cancels the query on SQL Server and fails it when the action's timeout has passed. The driver's own query - * timeout should stop the query first. This check does not depend on it. + * One daemon thread that starts the cancels at their deadlines. A task stopped before its deadline leaves the + * queue at once, so finished queries do not pile up in it. */ + private static final ScheduledThreadPoolExecutor CANCEL_TIMER = newCancelTimer(); + + /** Each cancel runs on its own virtual thread. */ + private static final Thread.Builder CANCEL_THREADS = Thread.ofVirtual().name("mssql-query-cancel-", 0); + + private static ScheduledThreadPoolExecutor newCancelTimer() { + ScheduledThreadPoolExecutor timer = new ScheduledThreadPoolExecutor(1, task -> { + Thread thread = new Thread(task, "mssql-query-cancel-timer"); + thread.setDaemon(true); + return thread; + }); + timer.setRemoveOnCancelPolicy(true); + return timer; + } + + /** The number of cancels waiting for their deadline. For tests. */ + static int pendingCancels() { + return CANCEL_TIMER.getQueue().size(); + } + + /** Fails the query with a timeout when the action's timeout has passed. */ private static void stopAtDeadline(Statement statement, int timeoutMillis, long deadlineNanos) throws SQLException { - if (System.nanoTime() - deadlineNanos < 0) { - return; + if (isPastDeadline(deadlineNanos)) { + throw cancelForTimeout(statement, timeoutMillis, null); } + } + + /** + * Cancels the query on SQL Server and returns the timeout error. Closing the statement afterwards would otherwise + * read every remaining result, which can go on without end. + * + * @param cause the error the driver raised after the deadline, or null + */ + private static DeadlineReached cancelForTimeout(Statement statement, int timeoutMillis, SQLException cause) { DeadlineReached timeout = new DeadlineReached(timeoutMillis); + if (cause != null) { + timeout.initCause(cause); + } if (statement != null) { try { statement.cancel(); } catch (SQLException e) { - // Still report the timeout. Closing the statement afterwards ends the query. + // Still report the timeout. timeout.addSuppressed(e); } } - throw timeout; + return timeout; } /** The plugin's own timeout error, so that it is not reported as a failed statement. */ @@ -231,11 +299,10 @@ private static void drainRemainingResults(Statement statement, int timeoutMillis } catch (DeadlineReached e) { throw e; } catch (SQLException e) { - if (System.nanoTime() - deadlineNanos >= 0) { - // The driver's own query timeout stopped the query. Report a timeout, not a failed statement. - DeadlineReached timeout = new DeadlineReached(timeoutMillis); - timeout.initCause(e); - throw timeout; + if (isPastDeadline(deadlineNanos)) { + // A cancel at the deadline, or any other error after it. Some errors do not end the query, so + // cancel it before reporting the timeout. + throw cancelForTimeout(statement, timeoutMillis, e); } throw new SQLException( "A statement in this query failed. Other statements in the query may already have run." diff --git a/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/MssqlDriverBehaviourTest.java b/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/MssqlDriverBehaviourTest.java index a278969f978..e0cd842380d 100644 --- a/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/MssqlDriverBehaviourTest.java +++ b/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/MssqlDriverBehaviourTest.java @@ -512,8 +512,16 @@ private static Stream endlessLaterResults() { "SELECT 1 AS first_result; WHILE 1 = 1 UPDATE driver_dml SET name = name", prepared, "ran longer than its timeout of 3000 ms"), - // One statement that runs longer than the timeout. The driver's query timeout stops it. - Arguments.of("WAITFOR DELAY '00:00:20'", prepared, "The query has timed out"))); + // One statement that runs longer than the timeout. The driver's query timeout, or the timer, + // stops it. + Arguments.of("WAITFOR DELAY '00:00:20'", prepared, "ran longer than its timeout of 3000 ms"), + // A later statement that waits, after a first result the server sends at once. The wait + // happens inside a read of the later results. + Arguments.of( + "SELECT 1 AS first_result; RAISERROR('flush', 0, 1) WITH NOWAIT;" + + " WAITFOR DELAY '00:00:20'; SELECT 2 AS later_result", + prepared, + "ran longer than its timeout of 3000 ms"))); } // AIP-FD-38: a query that runs past its timeout must not hold a pooled connection and a worker thread. The diff --git a/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/utils/MssqlExecuteUtilsTest.java b/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/utils/MssqlExecuteUtilsTest.java index 2e38db8d841..39d04b65844 100644 --- a/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/utils/MssqlExecuteUtilsTest.java +++ b/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/utils/MssqlExecuteUtilsTest.java @@ -12,12 +12,14 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import static java.lang.Boolean.FALSE; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.catchThrowable; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -75,6 +77,68 @@ void should_reportATimeout_when_theDriverStopsTheQueryAfterTheDeadline() throws // Then assertThat(error).isInstanceOf(SQLTimeoutException.class).hasMessage(TIMEOUT_MESSAGE); + assertThat(error.getCause()).hasMessage("The query has timed out."); + // Some errors do not end the query, so it is cancelled before the timeout is reported. + verify(statement).cancel(); + } + + @Test + void should_cancelTheStatement_when_theDeadlinePassesDuringABlockedRead() throws Exception { + // Given + Statement statement = Mockito.mock(Statement.class); + long deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(50); + + // When + MssqlExecuteUtils.cancelAtDeadline(statement, deadline); + + // Then + // The cancel runs on its own thread, shortly after the deadline. + verify(statement, timeout(5000)).cancel(); + } + + @Test + void should_notCancelTheStatement_when_theTimerIsStoppedBeforeTheDeadline() throws Exception { + // Given + Statement statement = Mockito.mock(Statement.class); + long deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(200); + + // When + MssqlExecuteUtils.cancelAtDeadline(statement, deadline).cancel(false); + Thread.sleep(400); + + // Then + verify(statement, never()).cancel(); + } + + @Test + void should_removeTheTaskFromTheQueue_when_theTimerIsStoppedBeforeTheDeadline() { + // Given + Statement statement = Mockito.mock(Statement.class); + long farDeadline = System.nanoTime() + TimeUnit.MINUTES.toNanos(1); + ScheduledFuture task = MssqlExecuteUtils.cancelAtDeadline(statement, farDeadline); + int queued = MssqlExecuteUtils.pendingCancels(); + + // When + task.cancel(false); + + // Then + // A finished query does not stay in the queue until its deadline. + assertThat(MssqlExecuteUtils.pendingCancels()).isEqualTo(queued - 1); + } + + @Test + void should_notCancelTheStatement_when_itIsAlreadyClosedAtTheDeadline() throws Exception { + // Given + Statement statement = Mockito.mock(Statement.class); + when(statement.isClosed()).thenReturn(true); + long deadline = System.nanoTime(); + + // When + MssqlExecuteUtils.cancelAtDeadline(statement, deadline); + + // Then + verify(statement, timeout(5000)).isClosed(); + verify(statement, never()).cancel(); } @Test From b5a9220a73f724b4e6b27a206c0abeef6c8725c0 Mon Sep 17 00:00:00 2001 From: Stacey Levine Date: Thu, 19 Nov 2026 14:02:13 -0500 Subject: [PATCH 3/3] test(mssql): follow the server test conventions in the timeout tests - The timeout integration test uses StepVerifier, AssertJ and Awaitility, is package-private, and has a should_..._when_... name with Given, When and Then. - The driver-timeout unit test now waits until the deadline has passed before the simulated driver error, and checks that the read happened and that the error is the timeout's cause. It no longer passes without reaching that path. - No Thread.sleep in the new tests. The unused stub is gone, and the failed-statement test asserts the exact SQLException type. - Add Awaitility to the SQL Server plugin's test dependencies. Co-Authored-By: Claude Opus 5.5 --- .../appsmith-plugins/mssqlPlugin/pom.xml | 6 ++ .../plugins/MssqlDriverBehaviourTest.java | 58 +++++++++---------- .../plugins/utils/MssqlExecuteUtilsTest.java | 25 +++++--- 3 files changed, 49 insertions(+), 40 deletions(-) diff --git a/app/server/appsmith-plugins/mssqlPlugin/pom.xml b/app/server/appsmith-plugins/mssqlPlugin/pom.xml index e71a258aac8..025a97d8007 100644 --- a/app/server/appsmith-plugins/mssqlPlugin/pom.xml +++ b/app/server/appsmith-plugins/mssqlPlugin/pom.xml @@ -49,6 +49,12 @@ + + org.awaitility + awaitility + test + + diff --git a/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/MssqlDriverBehaviourTest.java b/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/MssqlDriverBehaviourTest.java index e0cd842380d..972e58843c0 100644 --- a/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/MssqlDriverBehaviourTest.java +++ b/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/MssqlDriverBehaviourTest.java @@ -23,6 +23,7 @@ import org.testcontainers.containers.MSSQLServerContainer; import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; +import reactor.test.StepVerifier; import java.io.IOException; import java.io.InputStream; @@ -37,6 +38,8 @@ import static com.external.plugins.MssqlTestDBContainerManager.createDatasourceConfiguration; import static com.external.plugins.MssqlTestDBContainerManager.mssqlPluginExecutor; import static com.external.plugins.MssqlTestDBContainerManager.runSQLQueryOnMssqlTestDB; +import static org.assertj.core.api.Assertions.assertThat; +import static org.awaitility.Awaitility.await; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -529,48 +532,41 @@ private static Stream endlessLaterResults() { // ones forever, because the plugin reads the later results to find errors (APP-16536). @ParameterizedTest(name = "{0}, prepared statement {1}") @MethodSource("endlessLaterResults") - public void endlessLaterResults_stopAtTheQueryTimeout(String sql, boolean preparedStatement, String expectedError) - throws InterruptedException { + void should_failAndFreeTheConnection_when_theQueryRunsPastItsTimeout( + String sql, boolean preparedStatement, String expectedError) { + // Given HikariDataSource connectionPool = mssqlPluginExecutor .datasourceCreate(createDatasourceConfiguration(container)) .block(); - assertNotNull(connectionPool); + assertThat(connectionPool).isNotNull(); + ActionConfiguration actionConfiguration = new ActionConfiguration(); + actionConfiguration.setBody(sql); + actionConfiguration.setTimeoutInMillisecond("3000"); + actionConfiguration.setPluginSpecifiedTemplates( + List.of(new Property("preparedStatement", String.valueOf(preparedStatement)))); try { - ActionConfiguration actionConfiguration = new ActionConfiguration(); - actionConfiguration.setBody(sql); - actionConfiguration.setTimeoutInMillisecond("3000"); - actionConfiguration.setPluginSpecifiedTemplates( - List.of(new Property("preparedStatement", String.valueOf(preparedStatement)))); - + // When // Before the fix, this call never returned: the reactive timeout cannot interrupt the blocking read. - Instant started = Instant.now(); - ActionExecutionResult result = mssqlPluginExecutor - .executeParameterized( + Duration took = StepVerifier.create(mssqlPluginExecutor.executeParameterized( connectionPool, new ExecuteActionDTO(), createDatasourceConfiguration(container), - actionConfiguration) - .block(Duration.ofSeconds(30)); - Duration took = Duration.between(started, Instant.now()); - - assertNotNull(result); - assertFalse(result.getIsExecutionSuccess(), () -> "the query should fail: " + result.getBody()); - assertTrue( - String.valueOf(result.getBody()).contains(expectedError), - () -> "expected \"" + expectedError + "\" in: " + result.getBody()); - assertTrue(took.compareTo(Duration.ofSeconds(10)) < 0, () -> "the query stopped only after " + took); + actionConfiguration)) + // Then + .assertNext(result -> { + assertThat(result.getIsExecutionSuccess()).isFalse(); + assertThat(String.valueOf(result.getBody())).contains(expectedError); + }) + .expectComplete() + .verify(Duration.ofSeconds(30)); + assertThat(took).isLessThan(Duration.ofSeconds(10)); // The connection goes back to the pool shortly after the timeout. - Instant giveUp = Instant.now().plusSeconds(10); - while (connectionPool.getHikariPoolMXBean().getActiveConnections() > 0 - && Instant.now().isBefore(giveUp)) { - Thread.sleep(200); - } - assertEquals( - 0, - connectionPool.getHikariPoolMXBean().getActiveConnections(), - "the query still holds its connection after the timeout"); + await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> assertThat( + connectionPool.getHikariPoolMXBean().getActiveConnections()) + .as("connections the query still holds after its timeout") + .isZero()); } finally { connectionPool.close(); } diff --git a/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/utils/MssqlExecuteUtilsTest.java b/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/utils/MssqlExecuteUtilsTest.java index 39d04b65844..69d508ea89c 100644 --- a/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/utils/MssqlExecuteUtilsTest.java +++ b/app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/utils/MssqlExecuteUtilsTest.java @@ -9,6 +9,7 @@ import java.sql.SQLException; import java.sql.SQLTimeoutException; import java.sql.Statement; +import java.time.Duration; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -18,6 +19,8 @@ import static java.lang.Boolean.FALSE; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.catchThrowable; +import static org.awaitility.Awaitility.await; +import static org.mockito.Mockito.after; import static org.mockito.Mockito.never; import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.verify; @@ -49,7 +52,6 @@ void should_cancelAndFailWithTheTimeout_when_laterResultsDoNotEndBeforeTheDeadli // An update count first, then results without end, and a deadline that has passed. Statement statement = Mockito.mock(Statement.class); when(statement.getUpdateCount()).thenReturn(1); - when(statement.getMoreResults()).thenReturn(true); long deadlinePassed = System.nanoTime() - TimeUnit.SECONDS.toNanos(1); // When @@ -58,26 +60,31 @@ void should_cancelAndFailWithTheTimeout_when_laterResultsDoNotEndBeforeTheDeadli // Then assertThat(error).isInstanceOf(SQLTimeoutException.class).hasMessage(TIMEOUT_MESSAGE); verify(statement).cancel(); + // The deadline check stopped the reading before any later result was read. + verify(statement, never()).getMoreResults(); } @Test void should_reportATimeout_when_theDriverStopsTheQueryAfterTheDeadline() throws SQLException { // Given - // The driver's own query timeout ends the query while the later results are read. + // The driver's own query timeout ends the query while the later results are read: the read waits until the + // deadline has passed, then fails. Statement statement = Mockito.mock(Statement.class); when(statement.getUpdateCount()).thenReturn(1); - long deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(50); + long deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(500); + SQLException driverTimeout = new SQLException("The query has timed out.", "HY008"); when(statement.getMoreResults()).thenAnswer(invocation -> { - Thread.sleep(100); - throw new SQLException("The query has timed out.", "HY008"); + await().atMost(Duration.ofSeconds(5)).until(() -> MssqlExecuteUtils.isPastDeadline(deadline)); + throw driverTimeout; }); // When Throwable error = catchThrowable(() -> populate(statement, deadline)); // Then + verify(statement).getMoreResults(); assertThat(error).isInstanceOf(SQLTimeoutException.class).hasMessage(TIMEOUT_MESSAGE); - assertThat(error.getCause()).hasMessage("The query has timed out."); + assertThat(error.getCause()).isSameAs(driverTimeout); // Some errors do not end the query, so it is cancelled before the timeout is reported. verify(statement).cancel(); } @@ -104,10 +111,10 @@ void should_notCancelTheStatement_when_theTimerIsStoppedBeforeTheDeadline() thro // When MssqlExecuteUtils.cancelAtDeadline(statement, deadline).cancel(false); - Thread.sleep(400); // Then - verify(statement, never()).cancel(); + // Still not cancelled well after the deadline. + verify(statement, after(400).never()).cancel(); } @Test @@ -154,7 +161,7 @@ void should_reportTheFailedStatement_when_aLaterStatementFailsBeforeTheDeadline( // Then assertThat(error) - .isNotInstanceOf(SQLTimeoutException.class) + .isExactlyInstanceOf(SQLException.class) .hasMessageStartingWith("A statement in this query failed.") .hasMessageContaining("Invalid object name 'missing'."); verify(statement, never()).cancel();