Skip to content

SimpleDownloadTask race causes intermittent FileNotFoundException #2808

Description

@mershad-manesh

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:

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:

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

Related: #2805 (different bug, same "startup race" theme — fileinstall config corruption).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions