Skip to content

Prevent race when performing cache maintenance - #125

Open
mezz wants to merge 2 commits into
neoforged:mainfrom
mezz:main
Open

Prevent race when performing cache maintenance#125
mezz wants to merge 2 commits into
neoforged:mainfrom
mezz:main

Conversation

@mezz

@mezz mezz commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Problem

There is a race condition, where maintenance can run after a cache hit is confirmed but before the files can be written.

Fix

To simplify the things I need to protect from races, I moved the "writing" logic out of RunNeoFormCommand#execute and into NeoFormEngine#writeResults, then I sealed off everything else in NeoFormEngine as private so that it only has that one public entry-point.

From there, I could lock the whole method with a read lock, and made it so that the cache maintenance needs to get a write lock so it cannot run when the NeoFormEngine is active.

ReadWriteFileLock

That just left one problem: we need a way for multiple threads and processes across multiple JVMs to have a read-write lock, and there's no re-entrant ReadWrite File Lock in Java.
Unfortunately (sorry) I created a new library to support this: https://github.com/mezz/ReadWriteFileLock
Please see the readme for justification for why I created this instead of using another library.
I wrote a test suite based on studying the other implementations, so it should cover all the necessary requirements for this project.

@neoforged-pr-publishing

Copy link
Copy Markdown
  • Publish PR to GitHub Packages

@sciwhiz12 sciwhiz12 added the bug Something isn't working label Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants