Skip to content

Fix IndexOutOfBoundsException when reattaching reordered list with cache.collections=false#534

Merged
andyjefferson merged 1 commit into
datanucleus:masterfrom
jasper-vandemalle:fix/issue-526-list-reorder-ioob
May 7, 2026
Merged

Fix IndexOutOfBoundsException when reattaching reordered list with cache.collections=false#534
andyjefferson merged 1 commit into
datanucleus:masterfrom
jasper-vandemalle:fix/issue-526-list-reorder-ioob

Conversation

@jasper-vandemalle

@jasper-vandemalle jasper-vandemalle commented Apr 3, 2026

Copy link
Copy Markdown
Contributor

Fixes #526.

If you detach an entity with a List, insert elements anywhere but the end, then reattach with datanucleus.cache.collections=false, you get an IndexOutOfBoundsException during the reorder phase in SCOUtils.updateListWithListElements().

Root cause is two things fighting each other:

  1. The backed List/ArrayList/Vector set(int, E, boolean) methods call delegate.set() without loading the delegate first when useCache=false. Empty delegate, instant crash.

  2. The reorder phase reads positions via list.get(position) (hits the backing store when uncached) but writes via set() (hits the delegate). After queued updates these two views disagree, so you get garbage.

The fix: set() now calls loadFromStore() before touching the delegate. This is already guarded by !isCacheLoaded so it's a no-op once loaded. For the reorder phase, I snapshot the current list order via iteration before comparing, so we're not reading from two different sources mid-loop.

Integration test in companion PR: datanucleus/tests#92

@jasper-vandemalle jasper-vandemalle force-pushed the fix/issue-526-list-reorder-ioob branch from 178bd9f to 4430d47 Compare April 3, 2026 10:28
@jasper-vandemalle jasper-vandemalle marked this pull request as ready for review April 3, 2026 10:55
@andyjefferson andyjefferson merged commit 81ae547 into datanucleus:master May 7, 2026
@andyjefferson andyjefferson added this to the 6.0.12 milestone May 7, 2026
@jasper-vandemalle jasper-vandemalle deleted the fix/issue-526-list-reorder-ioob branch May 7, 2026 07:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants