Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write cache files atomically #1544

Merged
merged 1 commit into from
Nov 22, 2019
Merged

Write cache files atomically #1544

merged 1 commit into from
Nov 22, 2019

Conversation

sjakobi
Copy link
Collaborator

@sjakobi sjakobi commented Nov 15, 2019

Fixes #1540.

@@ -554,7 +555,7 @@ writeToSemanticCache :: Dhall.Crypto.SHA256Digest -> Data.ByteString.ByteString
writeToSemanticCache hash bytes = do
_ <- Maybe.runMaybeT $ do
cacheFile <- getCacheFile "dhall" hash
liftIO (Data.ByteString.writeFile cacheFile bytes)
liftIO (System.AtomicWrite.Writer.ByteString.atomicWriteFile cacheFile bytes)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not yet sure whether we ought to use

http://hackage.haskell.org/package/atomic-write-0.2.0.6/docs/System-AtomicWrite-Writer-ByteString.html#v:atomicWriteFileWithMode

with some "binary" FileMode instead. I can't actually find any documentation on constructing these FileModes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah no, FileMode seems to be about file permissions instead of text mode vs binary mode.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've asked for clarification regarding newline adjustments in stackbuilders/atomic-write#16.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sjakobi: Yeah, I don't believe binary mode is possible using the current atomic-write API

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll wait a bit for feedback from the atomic-write maintainers. Not sure whether I should then make a patch to add binary mode writes to atomic-write, or just switch to unliftio (which however doesn't seem to implement atomicity on Windows).

It would be nice if we could use the same package to fix #498 too…

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have opened fpco/unliftio#50 regarding Windows support and fpco/unliftio#51 regarding support for text mode in unliftio.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to switch to unliftio if that ends up being easier

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

atomic-write now offers binary mode writes too, which I'm using now.

@sjakobi
Copy link
Collaborator Author

sjakobi commented Nov 15, 2019

According to stackbuilders/atomic-write#9, we might write over pre-existing read-only files here. I'm not overly worried about this since we only write files in the dhall and dhall-haskell subdirectories of the cache directory.

@sjakobi sjakobi changed the title WIP: Write cache files atomically Write cache files atomically Nov 21, 2019
@sjakobi
Copy link
Collaborator Author

sjakobi commented Nov 21, 2019

Heads up @jneira and other Windows users – I'm not sure how well atomic-write supports Windows. If funny things start happening with your caches, it might be due to this patch! :)

@mergify mergify bot merged commit 3b222cd into master Nov 22, 2019
@mergify mergify bot deleted the sjakobi/1540-atomic-cache branch November 22, 2019 00:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cache can get into a weird state
2 participants