Repository navigation
Fix ClassCastException in mvnd when extension ClassRealm is reloaded - #308
Conversation
mvnd (Maven Daemon) caches extension ClassRealms across builds. When the njord extension JAR is updated mid-session (e.g. parent POM upgrade bumps the version), mvnd creates a fresh ClassRealm for the new JAR but leaves Plexus singletons from the stale ClassRealm registered in the container. The stale NjordRepositoryConnectorFactory then tries to cast a Session object that was created by the new ClassRealm's lifecycle participant into its own ClassRealm's Session type, throwing: ClassCastException: DefaultSession (loader ClassRealm @A) cannot be cast to Session (loader ClassRealm @b) Root cause: both ClassRealms key their session data entry with the same String "eu.maveniverse.maven.njord.shared.Session", so the stale realm finds — and attempts to cast — the other realm's object. Fix: use Session.class (the Class *object*) as the session data key instead of Session.class.getName() (a String). Class objects are ClassRealm-local: each realm has its own Session.class instance, so realms have independent, non-interfering session data slots. The instanceof guard makes both methods safe even if the entry was somehow populated by a foreign object.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughUpdate Njord session caching to use the class object ChangesSession Cache Key Migration
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Nice finding! |
|
Hm, or make connector not singleton but a prototype? |
|
thank you @cstamas for your feedback!
done
i understand this is meant to be alternative solution to the same problem? |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In
`@extension3/src/main/java/eu/maveniverse/maven/njord/extension3/NjordSessionLifecycleParticipant.java`:
- Line 117: The Njord session cache removal call
(NjordUtils.removeNjordSession(session.getRepositorySession())) must be executed
unconditionally in a finally block inside NjordSessionLifecycleParticipant so
cleanup runs even if publish/close throws; modify the method surrounding the
Optional<...> ns handling and the publish/close logic to move the
removeNjordSession(...) call into a finally block that always runs (regardless
of ns.isPresent()), ensuring stale Session instances cannot be reused by
lazyInit.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 0b8adab4-0b61-4035-9d2f-8f44994db048
📒 Files selected for processing (2)
core/src/main/java/eu/maveniverse/maven/njord/shared/NjordUtils.javaextension3/src/main/java/eu/maveniverse/maven/njord/extension3/NjordSessionLifecycleParticipant.java
🚧 Files skipped from review as they are similar to previous changes (1)
- core/src/main/java/eu/maveniverse/maven/njord/shared/NjordUtils.java
334b214 to
0ba5e71
Compare
|
Thank you for your review and merge, @cstamas ! |
|
thanks for a quick release @cstamas ! |
mvnd (Maven Daemon) caches extension ClassRealms across builds. When the njord extension JAR is updated mid-session (e.g. parent POM upgrade bumps the version), mvnd creates a fresh ClassRealm for the new JAR but leaves Plexus singletons from the stale ClassRealm registered in the container. The stale NjordRepositoryConnectorFactory then tries to cast a Session object that was created by the new ClassRealm's lifecycle participant into its own ClassRealm's Session type, throwing:
Root cause: both ClassRealms key their session data entry with the same String "eu.maveniverse.maven.njord.shared.Session", so the stale realm finds — and attempts to cast — the other realm's object.
Fix: use Session.class (the Class object) as the session data key instead of Session.class.getName() (a String). Class objects are ClassRealm-local: each realm has its own Session.class instance, so realms have independent, non-interfering session data slots. The instanceof guard makes both methods safe even if the entry was somehow populated by a foreign object.
Summary by CodeRabbit
Refactor
Bug Fix