zjncs opened a new pull request, #11098:
URL: https://github.com/apache/rocketmq/pull/11098

   ### Motivation
   
   In `IndexService.getAndCreateLastIndexFile` the write lock is acquired 
*inside* the `try` block, after the `IndexFile` constructor, while `finally` 
unconditionally calls `writeLock().unlock()`:
   
   ```java
   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
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to