This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new a3f3e8e64296 CAMEL-23360: stop JPA consumer before final assertion to 
fix deadlock (#30147)
a3f3e8e64296 is described below

commit a3f3e8e642962478c35b718f4f58d0f30c99f75c
Author: Guillaume Nodet <[email protected]>
AuthorDate: Tue Nov 17 14:08:51 2026 +0100

    CAMEL-23360: stop JPA consumer before final assertion to fix deadlock 
(#30147)
    
    Deadlock between the test thread and the JPA consumer polling thread:
    - Test thread holds OpenJPA StateManagerImpl lock (via entity field access)
      and waits for BrokerImpl lock
    - Consumer thread holds BrokerImpl lock (in detachAll() during clear())
      and waits for StateManagerImpl lock
    
    Fix: stop the consumer after the first await() (which confirms the exchange
    was received) and before the final transaction-based assertion block. The
    consumer has already delivered its message (captured as a defensive copy),
    so stopping it prevents any further poll() cycles from calling
    entityManager.clear() concurrently with the test thread accessing entity
    fields.
    
    This fix applies to JpaWithNamedQueryTest and all subclasses that inherit
    the same test: JpaWithQueryTest, JpaWithNativeQueryTest,
    JpaWithNativeQueryWithResultClassTest.
    
    Co-authored-by: Claude Sonnet 4.6 <[email protected]>
---
 .../org/apache/camel/component/jpa/JpaWithNamedQueryTest.java  | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git 
a/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaWithNamedQueryTest.java
 
b/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaWithNamedQueryTest.java
index c1ed8921e866..157cb3e7dab9 100644
--- 
a/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaWithNamedQueryTest.java
+++ 
b/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaWithNamedQueryTest.java
@@ -105,6 +105,16 @@ public class JpaWithNamedQueryTest {
         await().atMost(10, TimeUnit.SECONDS)
                 .untilAsserted(() -> assertReceivedResult(receivedExchange));
 
+        // Stop the consumer before the final assertion to prevent a deadlock 
between the test
+        // thread (reading entity fields) and the consumer polling thread 
(calling
+        // entityManager.clear()), which both contend on OpenJPA internal 
BrokerImpl and
+        // StateManagerImpl locks. See CAMEL-23360.
+        ServiceHelper.stopService(consumer);
+        // Wait for any in-flight poll to complete: stopService() sends a 
cancel signal but does
+        // not block until the poll thread exits. Without this, a poll already 
running when we
+        // stop can still reach entityManager.clear() concurrently with the 
assertion block below.
+        await().atMost(10, TimeUnit.SECONDS).until(() -> !((JpaConsumer) 
consumer).isPolling());
+
         // lets now test that the database is updated.
         // the consumer updates the row from within its own transaction, so we 
poll until that
         // transaction has committed - we cannot read it while that 
transaction is still active

Reply via email to