[JIRA] (HHH-16903) java.lang.IllegalStateException: Illegal pop() with non-matching JdbcValuesSourceProcessingState
by Prashant Guleria (JIRA)
Prashant Guleria ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiOGFjNWJkOWVi... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16903?atlOrigin=eyJpIjoiOGFjNW... ) HHH-16903 ( https://hibernate.atlassian.net/browse/HHH-16903?atlOrigin=eyJpIjoiOGFjNW... ) java.lang.IllegalStateException: Illegal pop() with non-matching JdbcValuesSourceProcessingState ( https://hibernate.atlassian.net/browse/HHH-16903?atlOrigin=eyJpIjoiOGFjNW... )
Issue Type: Bug Affects Versions: 6.2.2, 6.2.3, 6.2.4, 6.2.5, 6.2.6 Assignee: Unassigned Components: hibernate-core Created: 06/Jul/2023 05:24 AM Environment: HIbenrate 6.2.2
Spring 3.0.5
JVM 17.02
MacOS, Linux
PostgreSQL 14 Priority: Major Reporter: Prashant Guleria ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
I am getting
java.lang.IllegalStateException: Illegal pop() with non-matching JdbcValuesSourceProcessingState
I have observed this issue occurs intermittently when I load a lazy loaded entity in the CompletableFuture API.
public Collection<? extends GrantedAuthority> getAuthorities() {
return AnonymousHelper.runAnonymouslyAndReturnValue(() -> roles.stream()
.flatMap(roleEntity -> roleEntity.getPrivileges().stream())
.map(privilege -> new CustomGrantedAuthority(
privilege.getName(),
privilege.getTargetEntity(),
privilege.getAllowCreate(),
privilege.getAllowRead(),
privilege.getAllowUpdate(),
privilege.getAllowDelete()
))
.collect(Collectors.toList()));
}
I have a function runAnonymouslyAndReturnValue which takes a supplier, run the required supplier with elevated permissions and then return back the privilege entities.
Following is the definition of the function :
public static <T> T runAnonymouslyAndReturnValue(@NonNull Supplier<T> taskToPerform) {
Authentication oldAuthentication = SecurityContextHolder.getContext().getAuthentication();
try {
SecurityContextHolder.getContext().setAuthentication(anonymousUser);
return taskToPerform.get();
} finally {
SecurityContextHolder.clearContext();
SecurityContextHolder.getContext().setAuthentication(oldAuthentication);
}
}
anonymousUser is created like this return new UsernamePasswordAuthenticationToken(null, null, authorities);
In function getAuthorities when roles is defined as LAZY
@Schema(description = "The Role entities that are related to this entity.")
@ManyToMany(cascade = {CascadeType.MERGE}, fetch = FetchType.LAZY)
@CsvIgnore
// % protected region % [Update the annotation for roles here] end
private Set<RoleEntity> roles = new HashSet<>();
The following exception is generated:
java.lang.IllegalStateException: Illegal pop() with non-matching JdbcValuesSourceProcessingState
at org.hibernate.sql.results.spi.LoadContexts.deregister(LoadContexts.java:46)
at org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:209)
at org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:33)
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.doExecuteQuery(JdbcSelectExecutorStandardImpl.java:362)
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.executeQuery(JdbcSelectExecutorStandardImpl.java:168)
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.list(JdbcSelectExecutorStandardImpl.java:93)
at org.hibernate.sql.exec.spi.JdbcSelectExecutor.list(JdbcSelectExecutor.java:31)
at org.hibernate.loader.ast.internal.CollectionBatchLoaderArrayParam.initializeKeys(CollectionBatchLoaderArrayParam.java:145)
at org.hibernate.loader.ast.internal.CollectionBatchLoaderArrayParam.load(CollectionBatchLoaderArrayParam.java:107)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:669)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:75)
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:127)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1697)
at org.hibernate.collection.spi.AbstractPersistentCollection.lambda$initialize$3(AbstractPersistentCollection.java:617)
at org.hibernate.collection.spi.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:265)
at org.hibernate.collection.spi.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:615)
at org.hibernate.collection.spi.AbstractPersistentCollection.read(AbstractPersistentCollection.java:136)
at org.hibernate.collection.spi.PersistentSet.iterator(PersistentSet.java:169)
at java.base/java.util.Spliterators$IteratorSpliterator.estimateSize(Unknown Source)
at java.base/java.util.Spliterator.getExactSizeIfKnown(Unknown Source)
at java.base/java.util.stream.AbstractPipeline.copyInto(Unknown Source)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(Unknown Source)
at java.base/java.util.stream.AbstractPipeline.evaluate(Unknown Source)
at java.base/java.util.stream.ReferencePipeline.collect(Unknown Source)
at kmsweb.entities.UserEntity.lambda$getAuthorities$2(UserEntity.java:218)
at kmsweb.configs.security.helpers.AnonymousHelper.runAnonymouslyAndReturnValue(AnonymousHelper.java:2762)
at kmsweb.entities.UserEntity.getAuthorities(UserEntity.java:208)
The exception in this part of code stopped coming when I changed the *fetch* type of roles to *EAGER :*
@Schema(description = "The Role entities that are related to this entity.")
@ManyToMany(cascade = {CascadeType.MERGE}, fetch = FetchType.EAGER)
@CsvIgnore
// % protected region % [Update the annotation for roles here] end
private Set<RoleEntity> roles = new HashSet<>();
After solving above isue, I am seeing this exception again when making use of graphql-dataloaders and making use of following function in Dataloader definition.
public static <T> CompletableFuture<T> supplyAsync(Supplier<T> supplier) {
SecurityContext securityContext = SecurityContextHolder.getContext();
String tenantId = TenantContextHolder.getTenantId();
return CompletableFuture.supplyAsync(() -> {
SecurityContextHolder.setContext(securityContext);
TenantContextHolder.setTenantId(tenantId);
try {
return supplier.get();
}catch (Exception ex){
if (isDevProfile){
ex.printStackTrace();
}
throw ex;
}
finally {
CompletableFuture.runAsync(SecurityContextHolder::clearContext);
}
});
}
public DataLoader<K, T> createDataLoader(String keyFunctionName) {
// Returns a CompletableFuture that supplies the result of the finder function applied to the list of keys
return new DataLoader<>(keys -> SecurityContextCompletableFuture.supplyAsync(
() -> finder.apply(keys)
).thenApply(
entities -> {
Map<K, T> map = new HashMap<>();
for (T entity : entities) {
try {
K key = null;
// Checks if the key function name is "getId" or not
if(keyFunctionName.equalsIgnoreCase("getId")){
Method method = entity.getClass().getMethod(keyFunctionName);
key = (K) method.invoke(entity);
}else{
Method method = entity.getClass().getMethod(keyFunctionName);
Object relatedEntity = method.invoke(entity);
Method idMethod = relatedEntity.getClass().getMethod("getId");
key = (K) idMethod.invoke(relatedEntity);
}
map.put(key, entity);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
//It's not sure that the data is returned in the same order of keys thht are sent
// Creates a list of entities in the order of the keys
List<T> output = new ArrayList<>();
for (K key : keys) {
output.add(map.getOrDefault(key, null));
}
return output;
}
)
);
}
The follwing stack trace is generated intermittently :
java.lang.IllegalStateException: Illegal pop() with non-matching JdbcValuesSourceProcessingState
at org.hibernate.sql.results.spi.LoadContexts.deregister(LoadContexts.java:46)
at org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:209)
at org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:33)
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.doExecuteQuery(JdbcSelectExecutorStandardImpl.java:362)
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.executeQuery(JdbcSelectExecutorStandardImpl.java:168)
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.list(JdbcSelectExecutorStandardImpl.java:93)
at org.hibernate.sql.exec.spi.JdbcSelectExecutor.list(JdbcSelectExecutor.java:31)
at org.hibernate.loader.ast.internal.CollectionBatchLoaderArrayParam.initializeKeys(CollectionBatchLoaderArrayParam.java:145)
at org.hibernate.loader.ast.internal.CollectionBatchLoaderArrayParam.load(CollectionBatchLoaderArrayParam.java:107)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:669)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:75)
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:127)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1697)
at org.hibernate.collection.spi.AbstractPersistentCollection.lambda$initialize$3(AbstractPersistentCollection.java:617)
at org.hibernate.collection.spi.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:265)
at org.hibernate.collection.spi.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:615)
at org.hibernate.collection.spi.AbstractPersistentCollection.read(AbstractPersistentCollection.java:136)
at org.hibernate.collection.spi.AbstractPersistentCollection.lambda$readSize$0(AbstractPersistentCollection.java:163)
at org.hibernate.collection.spi.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:265)
at org.hibernate.collection.spi.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:148)
at org.hibernate.collection.spi.PersistentSet.size(PersistentSet.java:151)
at graphql.util.FpKit.toSize(FpKit.java:179)
at graphql.execution.ExecutionStrategy.completeValueForList(ExecutionStrategy.java:518)
at graphql.execution.ExecutionStrategy.completeValueForList(ExecutionStrategy.java:503)
at graphql.execution.ExecutionStrategy.completeValue(ExecutionStrategy.java:442)
at graphql.execution.ExecutionStrategy.completeField(ExecutionStrategy.java:407)
at graphql.execution.ExecutionStrategy.lambda$resolveFieldWithInfo$1(ExecutionStrategy.java:213)
at java.base/java.util.concurrent.CompletableFuture.uniApplyNow(CompletableFuture.java:684)
at java.base/java.util.concurrent.CompletableFuture.uniApplyStage(CompletableFuture.java:662)
at java.base/java.util.concurrent.CompletableFuture.thenApply(CompletableFuture.java:2168)
at graphql.execution.ExecutionStrategy.resolveFieldWithInfo(ExecutionStrategy.java:212)
at graphql.execution.AsyncExecutionStrategy.execute(AsyncExecutionStrategy.java:59)
at graphql.execution.ExecutionStrategy.completeValueForObject(ExecutionStrategy.java:670)
at graphql.execution.ExecutionStrategy.completeValue(ExecutionStrategy.java:457)
at graphql.execution.ExecutionStrategy.completeField(ExecutionStrategy.java:407)
at graphql.execution.ExecutionStrategy.lambda$resolveFieldWithInfo$1(ExecutionStrategy.java:213)
at java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:646)
at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510)
at java.base/java.util.concurrent.CompletableFuture.complete(CompletableFuture.java:2147)
at org.dataloader.DataLoaderHelper.lambda$dispatchQueueBatch$2(DataLoaderHelper.java:259)
at java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:646)
at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510)
at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run$$$capture(CompletableFuture.java:1773)
at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java)
at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1760)
at java.base/java.util.concurrent.ForkJoinTask.doExec$$$capture(ForkJoinTask.java:373)
at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java)
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)
at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)
at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)
I am unable to understand, what is causing this issue due to which I am unable to provide a reproducer.
( https://hibernate.atlassian.net/browse/HHH-16903#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16903#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100230- sha1:e339e49 )
2 years, 9 months