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

Create .cmi files atomically (MPR#7472) #1307

Merged
merged 2 commits into from
Sep 6, 2017
Merged

Conversation

xavierleroy
Copy link
Contributor

@xavierleroy xavierleroy commented Aug 29, 2017

As suggested in MPR#7472, this is done by writing the data to a temporary file, compute the checksum, finish writing the data, and only then rename the temporary file to the destination .cmi file.

Writing .cmi files this way should avoid the corruption of .cmi files reported in MPR#4991. This corruption can occur when a .cmi file is produced simultaneously by a run of ocamlc and a run of ocamlopt.

"Atomic" here means "as atomic as the underlying file system guarantees". The atomicity guarantees of Windows file systems aren't entirely clear. Don't get me started on NFS either.

Speaking of Windows, this commit requires the new Win32 implementation of Sys.rename from GPR #1306 to work correctly in the MSVC and Mingw ports. I'll update this PR and remove the WIP tag when #1306 is merged. Until then Appveyor testing is expected to fail.

@dbuenzli
Copy link
Contributor

Wouldn't it make sense to generalize this to any file produced by the compiler ? This would also avoid corrupted objects if the compiler is killed during execution.

This is done by writing the data to a temporary file, compute the checksum, finish writing the data, and only then rename the temporary file to the destination .cmi file.

Writing .cmi files this way should avoid the corruption of .cmi files reported in MPR#4991.  This corruption can occur when a .cmi file is produced simultaneously by a run of ocamlc and a run of ocamlopt.

"Atomic" here means "as atomic as the underlying file system guarantees".  The atomicity guarantees of Windows file systems aren't entirely clear.
@xavierleroy
Copy link
Contributor Author

Now that #1306 is merged in trunk, I rebased and force-pushed like a pig, and this PR also works under Windows. Removing the WIP tag and asking for review.

@alainfrisch
Copy link
Contributor

.cmt and .annot files would have the same issue (except that it might more easily go unnoticed since the compiler itself does not open those files). So I think it is worth abstracting the pattern (e.g. a new function in Misc) and reuse it at least for those files.

@v-gb
Copy link
Contributor

v-gb commented Aug 31, 2017

If this going to be merged, it would be great if there was a simple pattern one can add to the gitignore to ignore these temporary files. My preference is a ".tmp" suffix.

Follow-up to MPR#7472.  The pattern "write to temporary file then rename"
is abstracted in the new function Misc.output_to_file_via_temporary
and applied to .cmi, .cmt and .annot files.
@xavierleroy
Copy link
Contributor Author

Good suggestions (".tmp" suffix + same handling for ".cmt" and ".annot" files). See latest commit.

@damiendoligez damiendoligez requested review from damiendoligez and removed request for damiendoligez September 6, 2017 17:59
Copy link
Member

@damiendoligez damiendoligez left a comment

Choose a reason for hiding this comment

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

LGTM

@damiendoligez damiendoligez merged commit 071866c into trunk Sep 6, 2017
@xavierleroy xavierleroy deleted the atomic_write_cmi branch October 3, 2017 13:44
@bobzhang
Copy link
Member

bobzhang commented Oct 7, 2017 via email

@dbuenzli
Copy link
Contributor

dbuenzli commented Oct 8, 2017

Would this hurt performance on build systems who never allows ocamlc and
ocamlopt run in parallel?

On unix I highly doubt that your build system performance is going to be bound by these rename(2), but if you want to make some measurements I'd love to be proven wrong.

EmileTrotignon pushed a commit to EmileTrotignon/ocaml that referenced this pull request Jan 12, 2024
…l#1307)

* use .not-prose for the .btn on install page
* change .prose a CSS style to take into account .not-prose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants