apache / apache/grails-core

Race condition

Open
#14,473 0 comments 0 reactions 0 assignees View on GitHub
relates-to: grails-data-hibernate5
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

I'm having a weird error with a plugin, but I think it is a race condition in HibernateMappingContextConfiguration, long story short, it gives me a NullPointerException on the line [266](https://github.com/grails/gorm-hibernate5/blob/master/grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/cfg/HibernateMappingContextConfiguration.java#L260) `((ServiceRegistryImplementor)serviceRegistry).destroy();`

From what I see, **the SessionFactoryObserver is set before the serviceRegistry is created** therefore leading to a NPE in some cases, and so my app runs some times and other times it flops with NPE. I don't really understand why sessionFactoryImple.Close() is being called, and this may be another problem.

```java

setSessionFactoryObserver(new SessionFactoryObserver() {
private static final long serialVersionUID = 1;
public void sessionFactoryCreated(SessionFactory factory) {}
public void sessionFactoryClosed(SessionFactory factory) {
((ServiceRegistryImplementor)serviceRegistry).destroy();
}
});

StandardServiceRegistryBuilder standardServiceRegistryBuilder = createStandardServiceRegistryBuilder(bootstrapServiceRegistry)
.applySettings(getProperties());

StandardServiceRegistry serviceRegistry = standardServiceRegistryBuilder.build();
sessionFactory = super.buildSessionFactory(serviceRegistry);
this.serviceRegistry = serviceRegistry;

```
Am I wrong thinking this may produce a race condition?

Here is more detail of issue https://github.com/mathpere/grails-hibernate-search-plugin/issues/33

Contributor guide

Open the contributing guide

Research direction

Start in grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/cfg/HibernateMappingContextConfiguration.java around line 266 and trace the SessionFactoryObserver, serviceRegistry creation, and sessionFactory close path. Review the linked grails-hibernate-search-plugin issue for reproduction details. Done should include a confirmed lifecycle explanation and a regression test covering the intermittent NullPointerException.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.