Repository navigation
Fix parallel package installation - #5979
Conversation
tingerrr
left a comment
There was a problem hiding this comment.
Looks good so far, I'm thinking we may still need a lock file to ensure that non-atomic moves are actually finished before another process tries to access it.
I think
Is it not already with it being in
Maybe, I don't see much of a point here, perhaps with an environment variable only.
No biggie.
We could give users the ability to gc this will a custom command like I suggested, something like
Laurenz said he doesn't mind too much if there's duplicate, competing work on the same package download. I think it's not much of a big deal, too. |
Also removes the possibility of a broken package cache state.
fd9e7fe to
2aaf8a8
Compare
It's technically only for extraction.
Perhaps.
It is already. It can be in the temp dir outside the "Typst cache dir".
Yeah, with how much is already customizable it becomes more bloated to my eye, but if we started this trend, then we might as well continue delivering on the customizablity. But this also would depend on the final location of the download directory.
I'm impartial to this. |
|
I don't want to add a new CLI command for clean up for now. Maybe we'll add one in the future or maybe we'll auto-cleanup the stuff somehow, but I don't want to commit to having it in the CLI interface. |
PgBiel
left a comment
There was a problem hiding this comment.
Overall, I have to admit I'm not too fond of this approach, but I'll leave some comments on its current state.
|
I've read up on the wall of text, and I've got a few comments. I think we're definitely overthinking at this point. We can't really ensure that the In my opinion, we should simply provide the users with the necessary documentation to fix up the rare error case in some form of FAQ. Approach 1 of PgBiel is also what I thought of while reading the discussion, it would at the very least reduce the possibility of accidentally putting the Let's talk about the edge cases, or more helpfully, their consequences when compiling:
All of these would occur exclusively inside a cached package (not a local package) if we assume corruption due to concurrent access or mid-operation crash. This could also be the case if the maintainer pushes a broken update, but I think it's safe to ignore this. If/Once we have unique error codes for certain types of errors, we could very easily identify any of these issues. Meaning, if we run into these issues in a cached package, we can simply suggest clearing the package directory and re-download on the next compilation. Honestly, we can probably emit this diagnostic already. Compare the full paths with the package cache path when such an error occurs and emit the hint that it could be corrupted. Regardless, that should be a different PR and should not block this one. |
|
That honestly sounds like checksum without checksum. But also, since a general assumption that program's cache can be removed at any moment (by the program, or by the user) and nothing should break (removing that during compilation would be of course not ideal), I don't think that we should ask user to clean anything, if compiler can identify that its cache is broken, then who if not the compiler knows the best course of action to fix the problem. So I'll say that it can remove the corrupted package and re-download it (once). If anything, it can simply emit a warning that such thing happened and that it already have been taken care of. |
|
I definitely agree that there's overthinking going on here. I still don't understand how we expect the package cache to get broken in the first place. The only concurrent "write" access is the rename operation and does any mainstream file steam actually fail mid-operation here? I don't see how a file should go missing or corrupted. And if the compiler sees a bad source file, how should it know that it got broken somehow rather than just being a bad package? Especially a missing file might just be an error by the package author. The only case I can see happening realistically is a user hitting Ctrl+C mid-unzipping. And the only consequence of that is a bit of wasted space in the randomly named directory in the cache. |
I don't know what to say except reread all the previous statements that describe how exactly it can happen. As to corrupted vs. bad package, I guess we can't know for sure which one it is. Compiler can re-download the package once and if it still fails, then it's highly likely that it's a bad package. But a proper solution is checksum. I actually didn't read the existing code carefully enough and now know that you do remove the failed extraction directory. So that's one mystery out of the way. |
|
I disagree with the overthinking claims, given that, if we didn't want to handle edge cases, this PR wouldn't be open in the first place. We can always "not care" about the problem - but we chose to care, so let's think about it properly. Doing things in a rush and without proper consideration will just lead to the same issue being reopened in a different case. And it's essential Typst works well in most environments. With that said, I'd say we can try the solution of downloading in the target directory into a |
|
Maybe overthinking is the wrong term. It's more that we're going in circles a bit, with everybody kinda just repeating their arguments. |
Now the "downloads" constant isn't required.
|
I'm guessing we won't have any tests for this one? |
I don't think this is feasible to test. |
This way, we auto-clean-up in all code paths, and possibly even on panic. Makes it a bit simpler conceptually.
Because the package directory was created immediately empty (before extraction), other Typst instances would already start using it and complain about missing `typst.toml`. The package directory must not exist until the rename takes place. To simplify that, this commit moves the location of the temporary dir into the package subfolder so that it's a move to a direct sibling location.
|
I made a few changes:
From my side, this would be ready to merge now. I'll wait a bit to give you all a chance to take a look if you want. |
|
|
|
Also, it looks like |
|
And lastly, I think the patchdiff --git a/crates/typst-kit/src/package.rs b/crates/typst-kit/src/package.rs
index abfc66f8..4e2b3a93 100644
--- a/crates/typst-kit/src/package.rs
+++ b/crates/typst-kit/src/package.rs
@@ -219,7 +219,7 @@ impl PackageStorage {
// Decompress the archive into the temporary directory.
let decompressed = flate2::read::GzDecoder::new(data.as_slice());
tar::Archive::new(decompressed)
- .unpack(&tempdir)
+ .unpack(&tempdir.0)
.map_err(|err| PackageError::MalformedArchive(Some(eco_format!("{err}"))))?;
// When trying to move (i.e., `rename`) the directory from one place to
@@ -234,7 +234,7 @@ impl PackageStorage {
// directory already existed in the first place. If situations with
// broken packages still occur even with the rename safeguard, we might
// consider more complex solutions like file locking or checksums.
- match fs::rename(&tempdir, &package_dir) {
+ match fs::rename(&tempdir.0, &package_dir) {
Ok(()) => Ok(()),
Err(err) if err.kind() == io::ErrorKind::DirectoryNotEmpty => Ok(()),
Err(err) => {
@@ -269,20 +269,6 @@ impl Drop for Tempdir {
}
}
-impl AsRef<Path> for Tempdir {
- fn as_ref(&self) -> &Path {
- self
- }
-}
-
-impl Deref for Tempdir {
- type Target = Path;
-
- fn deref(&self) -> &Self::Target {
- &self.0
- }
-}
-
/// Enriches an I/O error with a message and turns it into a
/// `PackageError::Other`.
#[cold]I remember that working with |
Changed.
I don't have benchmarks, but I like it, it does not hurt, is used across the codebase, and I don't really wanna argue about it in this PR.
The Deref is unnecessary, but I think the AsRef is nice.
It's how tempdir/tempfile does it. I think it's fine. |
|
Thanks for your work on this! |
|
Great work everyone. |
Co-authored-by: Laurenz <[email protected]>
Co-authored-by: Laurenz <[email protected]>
Attempt number 2, here we go...
Also removes the possibility of a broken package cache state.
Notes:
Cc: @laurmaedje, @tingerrr, @PgBiel
Fixes #2739.
Fixes #5935.