mershad-manesh opened a new issue, #2808:
URL: https://github.com/apache/karaf/issues/2808

   Seen this issue in Karaf 4.4.9, but suspect it will impact newer releases.
   
   `SimpleDownloadTask.download()` stages `wrap:`/`blueprint:`/`spring:` bundle 
URLs into a file named by hashing the URL, then does:
   
   ```java
   if (file.exists() && !file.delete()) { throw ...; }
   tmpFile.renameTo(file);
   ```
   
   If two overlapping resolutions of the same URL race (e.g. two feature 
installs, each with their own `DownloadManager` — dedup only happens within one 
instance), the second one can `delete()` the file the first one just wrote, 
right as something else opens it:
   
   ```
   java.io.FileNotFoundException: 
/..../data/tmp/9c73e02c-failureaccess-1.0.1.jar (No such file or directory)
       at java.base/java.io.FileInputStream.open0(Native Method)
       at java.base/java.io.FileInputStream.open(FileInputStream.java:216)
   ```
   
   Seen for multiple different artifacts, so it's generic to the staging code, 
not any one jar. Shows up as needing multiple restarts before a container boots 
cleanly.
   
   ## Fix
   
   Replace delete-then-rename with one atomic move:
   
   ```java
   try {
       Files.move(tmpFile.toPath(), file.toPath(),
               StandardCopyOption.REPLACE_EXISTING, 
StandardCopyOption.ATOMIC_MOVE);
   } catch (AtomicMoveNotSupportedException e) {
       Files.move(tmpFile.toPath(), file.toPath(), 
StandardCopyOption.REPLACE_EXISTING);
   }
   ```
   
   A concurrent reader then always sees either the old or new file — never a 
missing one.
   
   ## Proof
   
   Stress test (attached `RaceDemo.java`, 6 writers + 6 readers, 3s):
   
   ```
   original delete+renameTo -> FileNotFoundException count: 18733
   patched Files.move       -> FileNotFoundException count: 0
   ```
   
   ## Attached
   
   - 
[SimpleDownloadTask.patch](https://github.com/user-attachments/files/31228683/SimpleDownloadTask.patch)
 - the fix as a diff 
   - 
[RaceDemo.java](https://github.com/user-attachments/files/31228704/RaceDemo.java)
 - standalone reproducer (`javac RaceDemo.java && java RaceDemo`)
   
   Related: #2805 (different bug, same "startup race" theme — fileinstall 
config corruption).
   


-- 
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