You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(mssql): never let a timeout cancel reach a reused connection - #42666
When a SQL Server query runs past its timeout, a timer cancels it on SQL Server from another thread (#42659). At the end of the query, the plugin stopped that timer. But stopping the timer could not stop a cancel that the timer had already started. So the connection could go back to the pool while that cancel was still being sent, and the late cancel could stop the next query that used the connection. That query can belong to another app or user of the same datasource.
This PR makes sure a cancel never reaches a connection after it goes back to the pool:
MssqlExecuteUtils.cancelAtDeadline now returns a DeadlineCancel. Its cancel and its stop() share a lock.
A cancel that has not started never runs after stop().
stop() waits for a cancel that is running, up to 5 seconds.
stop() returns false when a cancel ran, or is still running after that wait.
The connection is removed from the pool (HikariDataSource.evictConnection) and closed, instead of going back to it, when:
the timer's cancel ran or may still be on its way, or
the query passed its deadline, because the query thread may then have cancelled the query itself.
Its session on SQL Server is then ended at once with Connection.abort, before its statements are closed. So the session ends even if the pool skips its own close while shutting down, and closing the statements cannot wait on a server that stopped answering. If the pool is closed or cannot remove the connection, the session is still ended, and the connection goes back already closed.
An info log line names the pool each time a connection is removed this way.
Only queries that reach their timeout are affected. Their connection is replaced by a new one, which costs one login to SQL Server. Other queries are unchanged.
a cancel that is running makes stop() wait, then the connection is not reused;
a cancel the timer started, but that has not taken the lock yet, never runs after stop();
a cancel still running after the wait, or one that already ran, means the connection is not reused;
a timer stopped before the deadline never cancels, and the connection is reused;
a connection that is not reusable is removed from the pool and its session aborted, in that order, before its statement is closed;
when the pool is closed, cannot remove it, or the abort fails, the session is still ended.
MssqlDriverBehaviourTest (Testcontainers, SQL Server 2022):
a query cancelled at its timeout records its own session. The test checks on SQL Server that this session ends, so the connection was closed, not lent again. This test fails when only the change in MssqlPlugin is reverted.
after each timed-out query in the existing timeout test, the next query on the same pool succeeds.
With stop() changed back to the old behaviour, 4 of the new unit tests fail. With stop() changed to wait without a bound, the stuck-cancel test fails.
Full SQL Server plugin suite: 134 tests, 0 failures.
Suggested Cypress tags or specs: @tag.Datasource
Communication
Should the DevRel and Marketing teams inform users about this change?
Yes
No
Release note
A SQL Server query that ran past its timeout could, in rare cases, stop the next query that used the same database connection. Appsmith now closes the connection of a query that reached its timeout, instead of reusing it. The next query opens a new connection.
The timer that cancels a query at its deadline could start the cancel just
before the query ended. Stopping the timer could not stop that cancel, so
the connection could go back to the pool and the late cancel could stop the
next query on it.
- The cancel and its stop now share a lock. A cancel that has not started
never runs after the stop, and the stop waits up to 5 seconds for one
that is running.
- When a cancel ran, may still be running, or the query passed its
deadline, the connection is removed from the pool and closed instead of
reused. It is removed before its statements are closed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
No actionable comments were generated in the recent review.
Summary
Summary by CodeRabbit
Bug Fixes
Timed-out SQL Server queries no longer leave potentially affected connections available for reuse.
Connections affected by query cancellation are removed from service, helping prevent subsequent queries from encountering connection issues.
Follow-up queries can continue using the connection pool after a timeout, and successful queries return their expected results.
Timeout handling now covers both prepared and non-prepared queries, including delays while reading results.
The SQL Server plugin now stops deadline cancellation before connection cleanup. It reuses a connection only when cancellation did not run and the deadline has not passed. Otherwise, cleanup attempts to abort and evict the connection, with fallback handling for failures. Tests cover cancellation races, timed-out sessions, cleanup outcomes, and a subsequent query on the same pool.
sequenceDiagram
participant MssqlPlugin
participant DeadlineCancel
participant Statement
participant MssqlExecuteUtils
participant HikariDataSource
DeadlineCancel->>Statement: cancel when deadline expires
MssqlPlugin->>DeadlineCancel: stop timer and check cancellation status
DeadlineCancel-->>MssqlPlugin: return cancellation status
MssqlPlugin->>MssqlExecuteUtils: clean up with reusability decision
alt connection is not reusable
MssqlExecuteUtils->>HikariDataSource: attempt pool eviction
else connection is reusable
MssqlExecuteUtils->>MssqlExecuteUtils: use existing cleanup path
end
Loading
Possibly related PRs
appsmithorg/appsmith#42659: Both changes update SQL Server deadline cancellation. This change uses cancellation status to determine connection reusability.
The change makes SQL Server query timeouts discard the affected connection instead of reusing it, so a late cancellation cannot hit a later query. The previously flagged cleanup-ordering concern appears addressed by aborting the connection before evicting it. No remaining merge-blocking risk was identified.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title names the main change: a timeout cancel must not reach a reused SQL Server connection. It matches the changeset, which makes the timer cancel and stop() share a lock and discards connections…
Description check
The description follows the repository template. It has a Description section with motivation and context, and it names the root cause, the fix, and the cost of the change. It links the Hacktron findi…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title names the main change: a timeout cancel must not reach a reused SQL Server connection. It matches the changeset, which makes the timer cancel and stop() share a lock and discards connections that may have been cancelled. The title is specific and uses the conventional fix(mssql) prefix. It is 64 characters, within a typical length limit.
Full details: Description check
Explanation
The description follows the repository template. It has a Description section with motivation and context, and it names the root cause, the fix, and the cost of the change. It links the Hacktron finding and includes a release note. The Testing section lists the server unit tests as run and gives concrete results. The description does not include a 'Fixes #' issue reference, which the template asks for. The Communication section is answered. The Cypress bot comment and the '/ok-to-test' line are automation artifacts and do not affect completeness. The template's TL;DR tip is optional for a description of this length, but the author can add one.
Finishing Touches
Generate unit tests (beta)
Commit to this branch
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info
Automatic approval policy
Commit: 19d395c33dfc.
Setting
Effective value
Source
Automatic approvals
Enabled
Global configuration
Merge risk
Low
Global configuration
Security risk
Not set
Global configuration
Not set means no limit was configured after inheritance. Use @coderabbitai configuration to inspect the resolved settings and their sources.
128-128: Minor · Stability & Availability · Heavy lift
Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.
Do not drain JDBC results before the physical connection closes.
HikariCP 5.0.1 removes the pool entry immediately, but schedules the physical close on its close executor. If that executor has not closed the connection yet, this call can make mssql-jdbc 13.6.2 read the remaining response or wait for the cancellation acknowledgement. The entry is no longer borrowable, but the action can still wait on SQL Server before reporting its timeout. The mock ordering test only proves that eviction was called first. (raw.githubusercontent.com)
On the discard path, terminate the physical connection before explicit JDBC cleanup, or arrange for the discard operation to own that cleanup without draining results on the action thread. Keep the existing cleanup order for reusable connections.
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Learnt from: CR
Repo: appsmithorg/appsmith PR: 0
File: app/server/AGENTS.md:0-0
Timestamp: 2026-10-31T13:25:08.869Z
Learning: Applies to app/server/** : Source excerpt:
# Java tests
## Shape of a new test
- No `Thread.sleep`. Use `StepVerifier.withVirtualTime` or Awaitility.
Learnt from: CR
Repo: appsmithorg/appsmith PR: 0
File: app/server/AGENTS.md:0-0
Timestamp: 2026-10-31T13:25:08.869Z
Learning: Applies to app/server/** : Source excerpt:
# Java tests
## Shape of a new test
- Name: `should_<expectedBehavior>_when_<condition>`, for example `should_return401_when_unauthenticated`. Add `DisplayName` only when the name alone cannot carry the scenario, such as a GHSA identifier or a long precondition.
- When a connection must not be reused, end its session on SQL Server
with Connection.abort before the statements are closed. Hikari's own
close runs later on another thread, and can be skipped while the pool
shuts down. Closing the statements first could also make the driver
wait for the rest of the response.
- If the pool cannot remove the connection, it goes back already closed,
so the next borrower gets a connection error, never this session.
- Tests: the timeout test checks the timeout error before it checks that
the session ended. The race test starts the cancel itself, after it
holds the lock. The stuck-cancel test fails if stop() waits without a
bound.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai About the additional comment on MssqlExecuteUtils.java line 128 (closing the statements before the physical connection closes): fixed in 1d0b829. On the discard path, the session is now ended with Connection.abort before the result set and statements are closed, so closing them cannot wait for the rest of the response. A unit test checks the order: remove from the pool, abort, then close the statement. The path for reusable connections is unchanged.
The reason will be displayed to describe this comment to others. Learn more.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Learnt from: salevine
Repo: appsmithorg/appsmith PR: 42659
File: app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/utils/MssqlExecuteUtils.java:286-286
Timestamp: 2026-11-19T18:55:40.097Z
Learning: Within repository "appsmithorg/appsmith", only for files matching one of ["app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/utils/MssqlExecuteUtils.java"]: In app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/utils/MssqlExecuteUtils.java, a later SQL Server result read can block past the action timeout after an initial response has arrived. Deadline checks between reads cannot interrupt that blocked call, so deadline enforcement needs cancellation independent of the result-reading thread.
Learnt from: salevine
Repo: appsmithorg/appsmith PR: 42659
File: :0-0
Timestamp: 2026-11-19T19:10:33.120Z
Learning: Within repository "appsmithorg/appsmith", only for files matching one of ["app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/utils/MssqlExecuteUtilsTest.java"]: In app/server/appsmith-plugins/mssqlPlugin/src/test/java/com/external/plugins/utils/MssqlExecuteUtilsTest.java, the plugin test classpath has mockito-core and mockito-inline but lacks mockito-junit-jupiter. MockitoExtension is therefore unavailable without adding that dependency; Mockito.mock(...) can be used instead.
@salevine I checked commit 1d0b829867. The discard path attempts pool eviction, then aborts the connection before closing the result set and statements. The test verifies eviction, abort, and statement-close order. The reusable path retains the existing cleanup.
This addresses my ordering concern. I did not run the tests.
Learnings added
Learnt from: salevine
URL: https://github.com/appsmithorg/appsmith/pull/42666
Timestamp: 2026-11-20T22:33:21.084Z
Learning: Within repository "appsmithorg/appsmith", only for files matching one of ["app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/utils/MssqlExecuteUtils.java"]: In app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/utils/MssqlExecuteUtils.java, closing a result set or statement on a discarded SQL Server connection can wait for the remaining server response. End the SQL Server session before closing those resources on the discard path.
Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.
Learnings used
Learnt from: salevine
Repo: appsmithorg/appsmith PR: 42659
File: app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/utils/MssqlExecuteUtils.java:286-286
Timestamp: 2026-11-19T18:55:40.097Z
Learning: Within repository "appsmithorg/appsmith", only for files matching one of ["app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/utils/MssqlExecuteUtils.java"]: In app/server/appsmith-plugins/mssqlPlugin/src/main/java/com/external/plugins/utils/MssqlExecuteUtils.java, a later SQL Server result read can block past the action timeout after an initial response has arrived. Deadline checks between reads cannot interrupt that blocked call, so deadline enforcement needs cancellation independent of the result-reading thread.
Abort the driver's connection while it is still checked out, then remove
it from the pool. Hikari closes the connection again on its own thread
after the removal. Run at the same time, the two cleanups in mssql-jdbc
could release a timer that other connections share. When the driver's
connection cannot be reached, the abort goes through the pool's wrapper
after the removal, as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ok-to-testRequired label for CIrelease-note: securityInclude in the release notes under Security. Wording comes from the PR's release-note block.
2 participants
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When a SQL Server query runs past its timeout, a timer cancels it on SQL Server from another thread (#42659). At the end of the query, the plugin stopped that timer. But stopping the timer could not stop a cancel that the timer had already started. So the connection could go back to the pool while that cancel was still being sent, and the late cancel could stop the next query that used the connection. That query can belong to another app or user of the same datasource.
This PR makes sure a cancel never reaches a connection after it goes back to the pool:
MssqlExecuteUtils.cancelAtDeadlinenow returns aDeadlineCancel. Its cancel and itsstop()share a lock.stop().stop()waits for a cancel that is running, up to 5 seconds.stop()returns false when a cancel ran, or is still running after that wait.HikariDataSource.evictConnection) and closed, instead of going back to it, when:Connection.abort, before its statements are closed. So the session ends even if the pool skips its own close while shutting down, and closing the statements cannot wait on a server that stopped answering. If the pool is closed or cannot remove the connection, the session is still ended, and the connection goes back already closed.Only queries that reach their timeout are affected. Their connection is replaced by a new one, which costs one login to SQL Server. Other queries are unchanged.
Found by Hacktron (AIP-FD-39).
Automation
/ok-to-test tags="@tag.All"
Testing
Client unit tests
Server unit tests
Cypress
Playwright
Deploy preview
Not applicable
MssqlExecuteUtilsTest(mocked statements):stop()wait, then the connection is not reused;stop();MssqlDriverBehaviourTest(Testcontainers, SQL Server 2022):MssqlPluginis reverted.With
stop()changed back to the old behaviour, 4 of the new unit tests fail. Withstop()changed to wait without a bound, the stuck-cancel test fails.Full SQL Server plugin suite: 134 tests, 0 failures.
Suggested Cypress tags or specs:
@tag.DatasourceCommunication
Should the DevRel and Marketing teams inform users about this change?
Release note
🤖 Generated with Claude Code
Tip
🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
Workflow run: https://github.com/appsmithorg/appsmith/actions/runs/44832910240
Commit: 19d395c
Cypress dashboard.
Tags:
@tag.AllSpec:
Fri, 20 Nov 2026 23:36:33 UTC