ruanwenjun opened a new issue, #7826:
URL: https://github.com/apache/kyuubi/issues/7826

   ### Code of Conduct
   
   - [x] I agree to follow this project's Code of Conduct.
   
   ### Search before asking
   
   - [x] I searched the existing issues and did not find this deadlock.
   
   ### Describe the bug
   
   `Log4j2DivertAppender` can deadlock when formatting a log event waits for 
work performed by another thread that logs through the same appender.
   
   The appender extends Log4j's `AbstractWriterAppender`. Its 
`super.append(event)` acquires the inherited read lock **before** evaluating 
the layout. The override then acquires the corresponding write lock to drain 
the shared `CharArrayWriter`.
   
   This creates a circular wait:
   
   1. Thread A holds the appender read lock while formatting an event, and 
waits for an asynchronous task on thread B.
   2. Thread B emits a log event. After formatting its event, it waits for the 
appender write lock.
   3. Thread B cannot finish until A releases the read lock; A cannot finish 
until B completes.
   
   This does not require a slow log destination. The wait is between 
formatting, asynchronous work, and the appender's shared lock.
   
   ### Observed trigger and impact
   
   We observed this while a Spark SQL statement failed analysis because its 
target table did not exist:
   
   - Kyuubi's `SparkOperation.onError` logs the `AnalysisException` while 
holding the operation lock.
   - Rendering the exception calls `AnalysisException.getMessage -> 
logicalPlan.toString -> DataSourceV2Relation.name -> HoloTable.name -> 
SparkHoloUtil.chooseBestMode -> CompletableFuture.get`.
   - The Hologres worker needed by that future blocks while logging through 
`Log4j2DivertAppender`.
   - The engine never reaches the subsequent ERROR state transition.
   - The engine's CancelOperation handler waits for the operation lock held by 
the formatting thread.
   - The server marks its local operation CANCELED, then waits indefinitely for 
the engine's cancellation response while holding its own operation/client locks.
   - Later, the server's single session timeout-checker blocks in 
`operation.close()` waiting for that operation lock. Its periodic checks stop.
   - The engine session remains open and the Spark application remains running. 
In this incident the session persisted for more than 40 hours.
   
   The server-side wait amplifies the issue, but this report and the proposed 
focused fix concern the appender's circular wait.
   
   ### Affects Version(s)
   
   Observed with a downstream Kyuubi 1.9.1-based build (1.9.1.2), Spark 3.3.2, 
Log4j 2.17.2, and JDK 8u472.
   
   The same `super.append(event)` / inherited `writeLock.lock()` implementation 
is still present in upstream master at 
`0fd62e5b0cdb7689f8e458f26c3d229418e64ba3`. A regression test against master 
will accompany the PR; this report does not claim a production test of master.
   
   ### Kyuubi Engine Log Output
   
   Relevant engine thread dump excerpts, with production identifiers omitted:
   
   ```text
   query formatting thread:
     CompletableFuture.get
     SparkHoloUtil.chooseBestMode
     HoloTable.name
     DataSourceV2Relation.simpleString
     AnalysisException.getMessage
     Throwable.printStackTrace
     ThrowablePatternConverter.format
     PatternLayout.toSerializable
     AbstractWriterAppender.append
     Log4j2DivertAppender.append:74
     SparkOperation.onError:199
   
   connector worker:
     ReentrantReadWriteLock$WriteLock.lock
     Log4j2DivertAppender.append:75
     Log4jLogger.info
     com.alibaba.hologres.client.impl.Worker.run
   
   engine cancellation handler:
     ReentrantLock.lock
     AbstractOperation.withLockRequired
     SparkOperation.cleanup
     SparkOperation.cancel
     TFrontendService.CancelOperation
   ```
   
   Spark UI explicitly identifies the cancellation handler as blocked by the 
query formatting thread's operation lock.
   
   ### Kyuubi Server Log Output
   
   Relevant server thread dump excerpts:
   
   ```text
   engine RPC executor:
     SocketInputStream.socketRead0
     TCLIService$Client.recv_CancelOperation
     KyuubiSyncThriftClient.cancelOperation
   
   cancellation handler (holds operation lock and client lock):
     FutureTask.get
     KyuubiSyncThriftClient.withLockAcquiredAsyncRequest
     KyuubiSyncThriftClient.cancelOperation
     KyuubiOperation.cancel
   
   session timeout-checker:
     ReentrantLock.lock
     AbstractOperation.withLockRequired
     KyuubiOperation.close
     AbstractSession.closeExpiredOperations
     SessionManager...run
   ```
   
   The timeout-checker is waiting for the same operation lock owned by the 
server cancellation handler.
   
   ### Reproduction and expected behavior
   
   A regression test can reproduce the appender dependency without Spark or 
Hologres: format an event whose formatting waits for an executor task; have 
that task append a second event to the same appender. Coordinate the two 
threads with latches and bounded waits.
   
   Both events should complete and be routed to their current operation logs. 
Formatting must not hold a shared appender lock needed by the asynchronous 
logging task. Concurrent events must not mix messages between operation logs.
   
   ### Additional context
   
   [Hologres connectors 1.6.2 release 
notes](https://github.com/aliyun/alibabacloud-hologres-connectors/blob/master/RELEASE_NOTE.md#162)
 mention a fix for `HoloTable.name()` deadlocking in a logging-appender 
context. That addresses the connector trigger; Kyuubi's shared-lock formatting 
pattern can also be exercised by other layouts/messages/throwables with 
cross-thread dependencies.
   
   Relevant source:
   - 
[Log4j2DivertAppender](https://github.com/apache/kyuubi/blob/0fd62e5b0cdb7689f8e458f26c3d229418e64ba3/kyuubi-common/src/main/scala/org/apache/kyuubi/operation/log/Log4j2DivertAppender.scala#L66-L83)
   - 
[SparkOperation.onError](https://github.com/apache/kyuubi/blob/0fd62e5b0cdb7689f8e458f26c3d229418e64ba3/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/operation/SparkOperation.scala)
   
   No raw production logs or credentials are attached.
   
   ### Are you willing to submit PR?
   
   - [x] Yes. I would be willing to submit a PR with guidance from the Kyuubi 
community to fix.
   


-- 
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