artifact store: keep track of in-progress artifacts in memory #7860
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The artifact store in Sled Agent writes in-progress files to a known filename (
tmp/{sha256}
), then after the file is complete and the hash is correct, persists the file to the parent directory.Evidently using OpenOptions::create_new is not enough to avoid a TOCTOU problem, as described in #7796. We are seeing a situation where:
tmp/{sha256}
and starts writing to ittmp/{sha256}
and starts writing to ittmp/{sha256}
to{sha256}
, which is actually request B's incomplete filetmp/{sha256}
to{sha256}
Instead of using the filesystem to keep track of which artifacts are in progress, let's keep that information in memory. The implementation creates a oneshot channel, moving the sender into the write task and keeping the receiver in a map inside a mutex. When the write task finishes, the sender is dropped along with the rest of the ArtifactWriter; we can see this on the receiver side to track whether the write is still in progress.
The implementation now avoids writing to a specific temporary file, instead using a randomized filename within the temporary directory. The filename is still prepended with the artifact sha256 to help with debugging.