You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2026/10/10 18:11:20 UTC
(cayenne) branch master updated: CAY-3045 Race condition: ClassCastException resolving a to-one fault concurrently on a shared DataContext
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cayenne.git
The following commit(s) were added to refs/heads/master by this push:
new ade5ca66e CAY-3045 Race condition: ClassCastException resolving a to-one fault concurrently on a shared DataContext
ade5ca66e is described below
commit ade5ca66e3b1fdb8a7767be6cf0d9a2ceb3776f0
Author: Andrus Adamchik <an...@objectstyle.com>
AuthorDate: Sat Oct 10 13:38:50 2026 -0400
CAY-3045 Race condition: ClassCastException resolving a to-one fault concurrently on a shared DataContext
---
RELEASE-NOTES.txt | 1 +
.../org/apache/cayenne/access/DataContext.java | 4 +-
.../org/apache/cayenne/access/DataRowUtils.java | 5 +-
.../cayenne/access/flush/PostprocessVisitor.java | 11 +--
.../org/apache/cayenne/reflect/ArcProperty.java | 25 +++++--
.../generic/PersistentObjectToManyProperty.java | 3 +-
.../generic/PersistentObjectToOneProperty.java | 5 +-
.../apache/cayenne/util/DeepMergeOperation.java | 14 ++--
.../access/DataContextResolveRelationshipIT.java | 84 ++++++++++++++++++++++
9 files changed, 128 insertions(+), 24 deletions(-)
diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt
index 60d940118..be28aaa52 100644
--- a/RELEASE-NOTES.txt
+++ b/RELEASE-NOTES.txt
@@ -67,6 +67,7 @@ CAY-3031 Expression parameter names may conflict with function names
CAY-3032 ColumnSelect: an entity column over a to-many relationship filters out root rows with no related objects
CAY-3033 Selects with subqueries produce conflicting cache keys
CAY-3044 Paginated query with a joint to-many prefetch returns duplicate ids and unresolved Integer elements
+CAY-3045 Race condition: ClassCastException resolving a to-one fault concurrently on a shared DataContext
----------------------------------
Release: 5.0-M3
diff --git a/cayenne/src/main/java/org/apache/cayenne/access/DataContext.java b/cayenne/src/main/java/org/apache/cayenne/access/DataContext.java
index b5170f64d..08c4645ec 100644
--- a/cayenne/src/main/java/org/apache/cayenne/access/DataContext.java
+++ b/cayenne/src/main/java/org/apache/cayenne/access/DataContext.java
@@ -668,8 +668,10 @@ public class DataContext implements ObjectContext {
relationshipName, sourceId.getEntityName(), sourceId);
}
+ // decide on a single read of the property: another thread may be resolving the same fault concurrently,
+ // replacing it with the target object between two reads
Object related = arc.readPropertyDirectly(source);
- if (arc.isFault(source)) {
+ if (arc.isFaultValue(related)) {
// a NEW object is unknown to the parent channels, so its unresolved relationship must be empty
if (source.getPersistenceState() == PersistenceState.NEW) {
diff --git a/cayenne/src/main/java/org/apache/cayenne/access/DataRowUtils.java b/cayenne/src/main/java/org/apache/cayenne/access/DataRowUtils.java
index aad3ff7db..510c0b78e 100644
--- a/cayenne/src/main/java/org/apache/cayenne/access/DataRowUtils.java
+++ b/cayenne/src/main/java/org/apache/cayenne/access/DataRowUtils.java
@@ -235,11 +235,12 @@ class DataRowUtils {
return false;
}
- if (property.isFault(object)) {
+ Object value = property.readPropertyDirectly(object);
+ if (property.isFaultValue(value)) {
return false;
}
- Persistent toOneTarget = (Persistent) property.readPropertyDirectly(object);
+ Persistent toOneTarget = (Persistent) value;
ObjectId currentId = (toOneTarget != null) ? toOneTarget.getObjectId() : null;
// if ObjectId is temporary, target is definitely modified...
diff --git a/cayenne/src/main/java/org/apache/cayenne/access/flush/PostprocessVisitor.java b/cayenne/src/main/java/org/apache/cayenne/access/flush/PostprocessVisitor.java
index 9c9f6ab3b..a4f48e76a 100644
--- a/cayenne/src/main/java/org/apache/cayenne/access/flush/PostprocessVisitor.java
+++ b/cayenne/src/main/java/org/apache/cayenne/access/flush/PostprocessVisitor.java
@@ -90,8 +90,11 @@ class PostprocessVisitor implements DbRowOpVisitor<Void> {
// must resolve faults... hopefully for to-one this will not cause extra fetches...
Object source = arc.readProperty(dbRow.getObject());
- if (source != null && !reverseArc.isFault(source)) {
- remapTarget(reverseArc, source, dbRow.getObject());
+ if (source != null) {
+ Object map = reverseArc.readPropertyDirectly(source);
+ if (!reverseArc.isFaultValue(map)) {
+ remapTarget(reverseArc, map, dbRow.getObject());
+ }
}
}
}
@@ -116,9 +119,9 @@ class PostprocessVisitor implements DbRowOpVisitor<Void> {
return updatedSnapshots == null ? Collections.emptyMap() : updatedSnapshots;
}
- private void remapTarget(ToManyMapProperty property, Object source, Object target) {
+ private void remapTarget(ToManyMapProperty property, Object resolvedMap, Object target) {
@SuppressWarnings("unchecked")
- Map<Object, Object> map = (Map<Object, Object>) property.readProperty(source);
+ Map<Object, Object> map = (Map<Object, Object>) resolvedMap;
Object newKey = property.getMapKey(target);
Object currentValue = map.get(newKey);
diff --git a/cayenne/src/main/java/org/apache/cayenne/reflect/ArcProperty.java b/cayenne/src/main/java/org/apache/cayenne/reflect/ArcProperty.java
index 0c2d1c2be..d5f7d26ef 100644
--- a/cayenne/src/main/java/org/apache/cayenne/reflect/ArcProperty.java
+++ b/cayenne/src/main/java/org/apache/cayenne/reflect/ArcProperty.java
@@ -78,13 +78,26 @@ public interface ArcProperty extends PropertyDescriptor {
ClassDescriptor getTargetDescriptor();
/**
- * Returns whether a target node connected to a given object is an
- * unresolved fault.
- *
- * @param source
- * an object that is a source object of the relationship.
+ * Returns whether a target node connected to a given object is an unresolved fault.
+ *
+ * @param source an object that is a source object of the relationship.
+ * @deprecated in favor of {@link #isFaultValue(Object)}, that lets the caller check and use a single read of the
+ * property.
+ */
+ @Deprecated(since = "5.0", forRemoval = true)
+ default boolean isFault(Object source) {
+ return isFaultValue(readPropertyDirectly(source));
+ }
+
+ /**
+ * Returns whether a value of this property, as returned by {@link #readPropertyDirectly(Object)}, is an unresolved
+ * fault. Unlike {@link #isFault(Object)}, this lets the caller decide on a single read of the property, which
+ * matters when another thread may resolve the fault between two reads of the same object.
+ *
+ * @param value a value of this property read from a source object.
+ * @since 5.0
*/
- boolean isFault(Object source);
+ boolean isFaultValue(Object value);
/**
* Turns a property of an object into a fault.
diff --git a/cayenne/src/main/java/org/apache/cayenne/reflect/generic/PersistentObjectToManyProperty.java b/cayenne/src/main/java/org/apache/cayenne/reflect/generic/PersistentObjectToManyProperty.java
index b50015f65..4c93b6933 100644
--- a/cayenne/src/main/java/org/apache/cayenne/reflect/generic/PersistentObjectToManyProperty.java
+++ b/cayenne/src/main/java/org/apache/cayenne/reflect/generic/PersistentObjectToManyProperty.java
@@ -102,8 +102,7 @@ class PersistentObjectToManyProperty extends PersistentObjectBaseProperty implem
}
@Override
- public boolean isFault(Object source) {
- Object value = readPropertyDirectly(source);
+ public boolean isFaultValue(Object value) {
return value instanceof Fault || value instanceof ToManyHolder<?> holder && holder.isFault();
}
diff --git a/cayenne/src/main/java/org/apache/cayenne/reflect/generic/PersistentObjectToOneProperty.java b/cayenne/src/main/java/org/apache/cayenne/reflect/generic/PersistentObjectToOneProperty.java
index 089c2baa1..8a3703819 100644
--- a/cayenne/src/main/java/org/apache/cayenne/reflect/generic/PersistentObjectToOneProperty.java
+++ b/cayenne/src/main/java/org/apache/cayenne/reflect/generic/PersistentObjectToOneProperty.java
@@ -98,8 +98,9 @@ class PersistentObjectToOneProperty extends PersistentObjectBaseProperty impleme
return visitor.visitToOne(this);
}
- public boolean isFault(Object object) {
- return readPropertyDirectly(object) instanceof Fault;
+ @Override
+ public boolean isFaultValue(Object value) {
+ return value instanceof Fault;
}
public void invalidate(Object object) {
diff --git a/cayenne/src/main/java/org/apache/cayenne/util/DeepMergeOperation.java b/cayenne/src/main/java/org/apache/cayenne/util/DeepMergeOperation.java
index 29341ef83..a61f9cd7f 100644
--- a/cayenne/src/main/java/org/apache/cayenne/util/DeepMergeOperation.java
+++ b/cayenne/src/main/java/org/apache/cayenne/util/DeepMergeOperation.java
@@ -90,14 +90,14 @@ public class DeepMergeOperation {
public boolean visitToOne(ToOneProperty property) {
- if (!property.isFault(peerInParentContext)) {
- Persistent destinationSource = (Persistent) property.readProperty(peerInParentContext);
+ Object sourceValue = property.readPropertyDirectly(peerInParentContext);
+ if (!property.isFaultValue(sourceValue)) {
+ Persistent destinationSource = (Persistent) sourceValue;
Object destinationTarget = destinationSource != null
? merge(destinationSource, property.getTargetDescriptor(), seen)
: null;
- Object oldTarget = property.isFault(target)
- ? null
- : property.readProperty(target);
+ Object targetValue = property.readPropertyDirectly(target);
+ Object oldTarget = property.isFaultValue(targetValue) ? null : targetValue;
property.writePropertyDirectly(target, oldTarget, destinationTarget);
}
@@ -105,8 +105,8 @@ public class DeepMergeOperation {
}
public boolean visitToMany(ToManyProperty property) {
- if (!property.isFault(peerInParentContext)) {
- Object value = property.readProperty(peerInParentContext);
+ Object value = property.readPropertyDirectly(peerInParentContext);
+ if (!property.isFaultValue(value)) {
Object targetValue;
if (property instanceof ToManyMapProperty) {
diff --git a/cayenne/src/test/java/org/apache/cayenne/access/DataContextResolveRelationshipIT.java b/cayenne/src/test/java/org/apache/cayenne/access/DataContextResolveRelationshipIT.java
index 0bc3ae8c2..a07c8f4e7 100644
--- a/cayenne/src/test/java/org/apache/cayenne/access/DataContextResolveRelationshipIT.java
+++ b/cayenne/src/test/java/org/apache/cayenne/access/DataContextResolveRelationshipIT.java
@@ -19,7 +19,9 @@
package org.apache.cayenne.access;
import org.apache.cayenne.Cayenne;
+import org.apache.cayenne.Fault;
import org.apache.cayenne.ObjectContext;
+import org.apache.cayenne.ObjectId;
import org.apache.cayenne.Persistent;
import org.apache.cayenne.test.jdbc.TableHelper;
import org.apache.cayenne.testdo.testmap.Artist;
@@ -31,10 +33,13 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
public class DataContextResolveRelationshipIT {
@@ -169,4 +174,83 @@ public class DataContextResolveRelationshipIT {
assertEquals("p1", ((Painting) related.getFirst()).getPaintingTitle());
});
}
+
+ // see https://issues.apache.org/jira/browse/CAY-3045
+ @Test
+ public void toOneFaultResolvedConcurrently() throws Exception {
+ Artist a = context.newObject(Artist.class);
+ a.setArtistName("a1");
+
+ // using custom subclass of Painting to ensure the test is deterministic
+ HandoffPainting p = new HandoffPainting();
+
+ // the subclass is not mapped, so the entity has to be named explicitly
+ p.setObjectId(ObjectId.of("Painting"));
+
+ p.setPaintingTitle("p1");
+ p.setToArtist(a);
+ context.commitChanges();
+
+ // turn the committed object's to-one back into a fault
+ context.invalidateObjects(p);
+ p.getPaintingTitle();
+ assertTrue(p.readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Fault);
+
+ p.readerThread = Thread.currentThread();
+ Thread resolver = new Thread(() -> {
+ p.await(p.faultRead);
+ p.getToArtist();
+ p.resolved.countDown();
+ }, "to-one-resolver");
+ resolver.start();
+
+ try {
+ List<?> related = context.resolveRelationship(p.getObjectId(), Painting.TO_ARTIST.getName(), false);
+ assertEquals(1, related.size());
+ assertSame(a, related.getFirst());
+ } finally {
+ // unblock the other thread on a failure, so that it doesn't hang the test
+ p.faultRead.countDown();
+ resolver.join(TimeUnit.SECONDS.toMillis(10));
+ }
+
+ assertTrue(p.handedOff, "Reader thread never reached the to-one property");
+ assertSame(a, p.getToArtist());
+ }
+
+ /**
+ * Pauses the first read of the "toArtist" property on the designated reader thread, until another thread
+ * signals that it has resolved the fault.
+ */
+ static class HandoffPainting extends Painting {
+
+ final CountDownLatch faultRead = new CountDownLatch(1);
+ final CountDownLatch resolved = new CountDownLatch(1);
+ volatile Thread readerThread;
+ volatile boolean handedOff;
+
+ @Override
+ public Object readPropertyDirectly(String propName) {
+ Object value = super.readPropertyDirectly(propName);
+
+ if (Thread.currentThread() == readerThread && !handedOff && TO_ARTIST.getName().equals(propName)) {
+ handedOff = true;
+ faultRead.countDown();
+ await(resolved);
+ }
+
+ return value;
+ }
+
+ void await(CountDownLatch latch) {
+ try {
+ if (!latch.await(10, TimeUnit.SECONDS)) {
+ fail("Timed out waiting for the other thread");
+ }
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new RuntimeException(e);
+ }
+ }
+ }
}