Skip to content

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

Open
jasper-vandemalle wants to merge 1 commit intodatanucleus:masterfrom
jasper-vandemalle:fix/issue-526-list-reorder-ioob
Open

Fix IndexOutOfBoundsException when reattaching reordered list with cache.collections=false#534
jasper-vandemalle wants to merge 1 commit intodatanucleus:masterfrom
jasper-vandemalle:fix/issue-526-list-reorder-ioob

Conversation

@jasper-vandemalle
Copy link
Copy Markdown
Contributor

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

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant