Skip to content

[ISSUE #S1] Scope the index list write lock to the list update in getAndCreateLastIndexFile - #11098

Open
zjncs wants to merge 1 commit into
apache:developfrom
zjncs:fix/index-service-lock-imbalance
Open

[ISSUE #S1] Scope the index list write lock to the list update in getAndCreateLastIndexFile#11098
zjncs wants to merge 1 commit into
apache:developfrom
zjncs:fix/index-service-lock-imbalance

Conversation

@zjncs

@zjncs zjncs commented Sep 9, 2026

Copy link
Copy Markdown

Motivation

In IndexService.getAndCreateLastIndexFile the write lock is acquired inside the try block, after the IndexFile constructor, while finally unconditionally calls writeLock().unlock():

try {
    ...
    indexFile = new IndexFile(fileName, ...);   // may throw IOException
    this.readWriteLock.writeLock().lock();      // only reached if the constructor returned
    this.indexFileList.add(indexFile);
} catch (Exception e) {
    LOGGER.error("getLastIndexFile exception ", e);
} finally {
    this.readWriteLock.writeLock().unlock();    // unlocks a lock that was never acquired
}

When IndexFile creation fails (store directory missing/not writable/disk full), the constructor throws, lock() is never reached, and the finally block throws IllegalMonitorStateException, which propagates out of getAndCreateLastIndexFile into putKeyEnd — i.e. into the message store write path (buildKey/putKey while appending index entries). The original creation failure is already logged; the extra IMSE only masks it.

Modifications

  • Create the IndexFile outside the lock; take the write lock only to append it to indexFileList, with a balanced try/finally.

Verification

Fail-before (new test on unpatched code):

IndexServiceTest.testGetAndCreateLastIndexFileWhenCreateFileFails » IllegalMonitorState

The test points the index store path below a regular file so IndexFile creation is guaranteed to fail.

Pass-after — full IndexServiceTest (5 existing + 1 new):

mvn -pl store test -Dtest='IndexServiceTest'
Tests run: 6, Failures: 0, Errors: 0, Skipped: 0

…AndCreateLastIndexFile

If IndexFile creation fails (e.g. the store directory is not writable),
the catch block logs the error but the finally block then calls
writeLock().unlock() although lock() was never reached, throwing
IllegalMonitorStateException out of getAndCreateLastIndexFile into the
message write path. Create the file outside the lock and only take the
write lock to append it to indexFileList.

Signed-off-by: zjncs <18910855655@163.com>
Copilot AI lite review requested due to automatic review settings September 9, 2026 10:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@RockteMQ-AI RockteMQ-AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Scopes the write lock to only guard the indexFileList.add() call, moving IndexFile construction outside the lock. This prevents IllegalMonitorStateException when file creation fails (the finally block was unlocking a lock that was never acquired). Well-structured fix with a targeted regression test.

LGTM.


Automated review by github-manager-bot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants