zhangstar333 commented on PR #71084:
URL: https://github.com/apache/doris/pull/71084#issuecomment-7299969663

   ### F-01 ยท Retire a borrow before a failed proxy close can recycle it
   
   - **Severity**: Major
   - **Where**: 
`fe/be-java-extensions/jdbc-scanner/src/main/java/org/apache/doris/jdbc/JdbcJniWriter.java:213-218`;
 same cleanup pattern in 
`fe/be-java-extensions/jdbc-scanner/src/main/java/org/apache/doris/jdbc/JdbcJniScanner.java:351-361`
   - **Category**: lifecycle / cross-borrower correctness
   
   ```java
                   conn.close();
                   conn = null;
                   preparedStatement = null;
               });
           }
           connectionUnusable |= failure != null;
   ```
   
   **What is wrong.** When commit and child closes succeed, connectionUnusable 
is false and the preceding eviction branch is skipped. If the first error comes 
from conn.close itself, the assignment after JdbcCleanup.attempt cannot stop 
Hikari from returning that entry to the pool. A different operation can then 
borrow the dead physical connection. Keeping minimumIdle=0 pools alive 
introduces this after-return reuse exposure: the base finally closed/removed 
that pool even when connection close failed. The same issue for minimumIdle>0 
predates this PR and is not claimed as new.
   
   **Why it happens.** The pinned dependency is HikariCP 6.0.0. Its 
[ProxyConnection.close/checkException](https://github.com/brettwooldridge/HikariCP/blob/HikariCP-6.0.0/src/main/java/com/zaxxer/hikari/pool/ProxyConnection.java#L240-L270)
 only marks selected fatal SQLSTATEs, SQLTimeoutException and vendor codes; its 
finally recycles even when an unrecognized reset SQLException escapes. 
[PoolEntry.recycle](https://github.com/brettwooldridge/HikariCP/blob/HikariCP-6.0.0/src/main/java/com/zaxxer/hikari/pool/PoolEntry.java#L77-L83)
 refreshes lastAccessed, and 
[HikariPool.getConnection](https://github.com/brettwooldridge/HikariCP/blob/HikariCP-6.0.0/src/main/java/com/zaxxer/hikari/pool/HikariPool.java#L155-L180)
 skips validation for entries used within its default 500ms window. The local 
late flag does not undo that recycle. The native writer already sets _closed 
before Java close 
(`be/src/format/transformer/vjni_format_transformer.cpp:100-110`), so direct 
Java retry tests do not provide
  a native retirement guarantee.
   
   **When it bites.** A concrete source-derived case uses SQLSERVER, 
connection_pool_min_size=0 and use_transaction=true. Commit and statement close 
succeed; Hikari then resets auto-commit to true. [mssql-jdbc v12.8.1 
setAutoCommit](https://github.com/microsoft/mssql-jdbc/blob/v12.8.1/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java#L4642-L4667)
 runs a server command for that reset. Azure SQL can return 40501, documented 
as severity 20 in the [Microsoft error 
reference](https://learn.microsoft.com/en-us/azure/azure-sql/database/troubleshoot-common-errors-issues?view=azuresql#list-of-transient-fault-error-codes).
 The driver's [database-error 
handling](https://github.com/microsoft/mssql-jdbc/blob/v12.8.1/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerException.java#L263-L294)
 closes the physical connection for that severity, but maps this error to Sxxxx 
(42000 in XOPEN mode), outside Hikari's fatal sets. Hikari's DriverDataSource 
uses Driver.connect, not SQLServ
 erPooledConnection; the driver's pooledConnectionParent is null, so the 
failure notification does not evict Hikari's entry. An operation borrowing 
immediately after the failed close returns can receive that closed physical 
connection. This is an inference from pinned library/driver control flow, not a 
reproduced Azure incident.
   
   ```text
   SQLSERVER writer: use_transaction=true, connection_pool_min_size=0
     commit OK -> statement.close OK -> connectionUnusable=false
     -> Hikari proxy.close -> reset setAutoCommit(true)
        -> Azure 40501 -> driver closes physical connection and throws Sxxxx
        -> Hikari does not mark eviction; finally recycles PoolEntry
     -> Doris records connectionUnusable=true, returns close error
     -> next borrow within 500ms skips validation -> dead physical connection
   ```
   
   **Suggested fix.** Preserve shared-pool ownership while ensuring reset/close 
failures retire the individual entry before it becomes available to another 
borrower. A conservative option is pre-eviction for reset-sensitive paths; 
retaining healthy reuse needs an ownership/retirement boundary in the 
connection or pool layer. Merely evicting in a catch after proxy.close still 
races its earlier recycle. The pinned Hikari6 exception override cannot force 
eviction for an unrecognized error, so a MUST_EVICT suggestion would not fix 
this version. This needs an ownership-level change rather than a safe one-line 
patch. Add a real-Hikari negative test injecting physical 
setAutoCommit(true)/reset failure and checking immediate replacement. The 
current fixture at 
`fe/be-java-extensions/jdbc-scanner/src/test/java/org/apache/doris/jdbc/JdbcPoolOwnershipTest.java:527-549`
 only checks existing closed state during reset and does not model this 
first-failure case; the unwrapped connection-close tests
  cannot model Hikari's recycle-before-throw.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to