[llvm-branch-commits] [clang] [llvm] [clang] Remove the temporary PCM file when writing fails (PR #243334)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Nov 16 21:05:02 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Jan Svoboda (jansvoboda11)

<details>
<summary>Changes</summary>

When `writeImpl()` fails after creating the temporary file (writing to it, querying its status, reading it back, or renaming it to the final destination), it returns early and leaves the `*.pcm.tmp` file behind in the module cache. This PR switches to `llvm::sys::fs::TempFile`, which removes the file on all failure paths. It also removes it when the process crashes, since it registers the file with `RemoveFileOnSignal`.

`writeImpl()` reports errors as `std::error_code`, so this PR also adds `TempFile::createWithErrorCode()` and `TempFile::keepWithErrorCode()`. These avoid converting `llvm::Error` back with `errorToErrorCode()`, which aborts on inconvertible errors. The existing `create()` and `keep(Name)` are now thin wrappers around them.

Depends on #<!-- -->243333.


---
Full diff: https://github.com/llvm/llvm-project/pull/243334.diff


5 Files Affected:

- (modified) clang/lib/Serialization/ModuleCache.cpp (+28-25) 
- (modified) clang/unittests/Serialization/ModuleCacheTest.cpp (+13-2) 
- (modified) llvm/include/llvm/Support/FileSystem.h (+6) 
- (modified) llvm/lib/Support/Path.cpp (+17-5) 
- (modified) llvm/unittests/Support/Path.cpp (+29) 


``````````diff
diff --git a/clang/lib/Serialization/ModuleCache.cpp b/clang/lib/Serialization/ModuleCache.cpp
index c7f643c768c90..addda10ad9e85 100644
--- a/clang/lib/Serialization/ModuleCache.cpp
+++ b/clang/lib/Serialization/ModuleCache.cpp
@@ -170,61 +170,64 @@ clang::writeImpl(StringRef Path, llvm::MemoryBufferRef Buffer, off_t &Size,
   ModelPath += Extension;
   ModelPath += ".tmp";
 
-  std::error_code EC;
-  int FD;
-  SmallString<128> TmpPath;
-  if ((EC = llvm::sys::fs::createUniqueFile(ModelPath, FD, TmpPath))) {
-    if (EC != std::errc::no_such_file_or_directory)
-      return EC;
+  // The temporary file is removed on discard() and also when crashing, so that
+  // failing to produce the PCM doesn't leave it behind.
+  llvm::ErrorOr<llvm::sys::fs::TempFile> Temp =
+      llvm::sys::fs::TempFile::createWithErrorCode(ModelPath);
+  if (!Temp) {
+    if (Temp.getError() != std::errc::no_such_file_or_directory)
+      return Temp.getError();
 
     StringRef Dir = llvm::sys::path::parent_path(Path);
     if (std::error_code InnerEC = llvm::sys::fs::create_directories(Dir))
       return InnerEC;
 
-    if ((EC = llvm::sys::fs::createUniqueFile(ModelPath, FD, TmpPath)))
-      return EC;
+    Temp = llvm::sys::fs::TempFile::createWithErrorCode(ModelPath);
+    if (!Temp)
+      return Temp.getError();
   }
 
   llvm::sys::fs::file_status Status;
   std::unique_ptr<llvm::MemoryBuffer> Written;
-  {
-    llvm::raw_fd_ostream OS(FD, /*shouldClose=*/true);
+  auto WriteContentsAndReadBack = [&]() -> std::error_code {
+    // The file descriptor is owned (and closed) by the temporary file.
+    llvm::raw_fd_ostream OS(Temp->FD, /*shouldClose=*/false);
     OS << Buffer.getBuffer();
     // Make sure all contents reach the file before querying its status.
     OS.flush();
-    if ((EC = OS.error())) {
+    if (std::error_code EC = OS.error()) {
       // Prevent the destructor from reporting a fatal error.
       OS.clear_error();
       return EC;
     }
     // Using the status from an open file descriptor ensures this is not racy.
-    if ((EC = llvm::sys::fs::status(FD, Status)))
+    if (std::error_code EC = llvm::sys::fs::status(Temp->FD, Status))
       return EC;
     if (ReadBack) {
       llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf =
           llvm::MemoryBuffer::getOpenFile(
-              llvm::sys::fs::convertFDToNativeFile(FD), Path, Status.getSize(),
-              /*RequiresNullTerminator=*/false);
+              llvm::sys::fs::convertFDToNativeFile(Temp->FD), Path,
+              Status.getSize(), /*RequiresNullTerminator=*/false);
       if (!Buf)
         return Buf.getError();
       Written = std::move(*Buf);
     }
-    // Close explicitly, so that we can handle any errors ourselves.
-    OS.close();
-    if ((EC = OS.error())) {
-      // Prevent the destructor from reporting a fatal error.
-      OS.clear_error();
-      return EC;
-    }
+    return {};
+  };
+
+  if (std::error_code EC = WriteContentsAndReadBack()) {
+    llvm::consumeError(Temp->discard());
+    return EC;
   }
 
+  // Rename to the final path and close. This preserves both size and
+  // modification time. On failure, the temporary file is removed.
+  if (std::error_code EC = Temp->keepWithErrorCode(Path))
+    return EC;
+
   Size = Status.getSize();
   ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
 
-  // This preserves both size and modification time.
-  if ((EC = llvm::sys::fs::rename(TmpPath, Path)))
-    return EC;
-
   return std::move(Written);
 }
 
diff --git a/clang/unittests/Serialization/ModuleCacheTest.cpp b/clang/unittests/Serialization/ModuleCacheTest.cpp
index 1915193f6c092..06d1867ac6b56 100644
--- a/clang/unittests/Serialization/ModuleCacheTest.cpp
+++ b/clang/unittests/Serialization/ModuleCacheTest.cpp
@@ -64,6 +64,15 @@ class ModuleCacheTest : public ::testing::Test {
     OS << Contents;
   }
 
+  // Checks that no temporary files were left behind in the module cache.
+  void expectNoTemporaryFiles() {
+    std::error_code EC;
+    for (sys::fs::recursive_directory_iterator I(ModuleCachePath, EC), E;
+         I != E && !EC; I.increment(EC))
+      EXPECT_NE(sys::path::extension(I->path()), ".tmp") << I->path();
+    EXPECT_FALSE(EC);
+  }
+
   void addDuplicateFrameworks() {
     addFile("test.m", R"cpp(
         @import Top;
@@ -428,7 +437,7 @@ TEST_F(ModuleCacheTest, WriteErrorCreateTempFileAfterCreateDirectories) {
 }
 
 // Test that failing to write the contents is reported instead of crashing in
-// the destructor of the stream.
+// the destructor of the stream, and that the temporary file is removed.
 TEST_F(ModuleCacheTest, WriteErrorWrite) {
   SmallString<256> Path(ModuleCachePath);
   sys::path::append(Path, "M.pcm");
@@ -453,10 +462,11 @@ TEST_F(ModuleCacheTest, WriteErrorWrite) {
   EXPECT_EQ(writeError(Path, Contents, /*ReadBack=*/false),
             std::errc::file_too_large);
   EXPECT_FALSE(sys::fs::exists(Path));
+  expectNoTemporaryFiles();
 }
 
 // Test that failing to rename the temporary file to the final path is
-// reported.
+// reported, and that the temporary file is removed.
 TEST_F(ModuleCacheTest, WriteErrorRename) {
   // The final path is a non-empty directory.
   SmallString<256> RelPath(ModuleCacheDir);
@@ -466,6 +476,7 @@ TEST_F(ModuleCacheTest, WriteErrorRename) {
   sys::path::append(Path, "M.pcm");
 
   EXPECT_EQ(writeError(Path, "contents"), std::errc::is_a_directory);
+  expectNoTemporaryFiles();
 }
 #endif
 
diff --git a/llvm/include/llvm/Support/FileSystem.h b/llvm/include/llvm/Support/FileSystem.h
index 98aa1648d6f61..e2d2ff035051d 100644
--- a/llvm/include/llvm/Support/FileSystem.h
+++ b/llvm/include/llvm/Support/FileSystem.h
@@ -905,6 +905,10 @@ class TempFile {
   LLVM_ABI static Expected<TempFile>
   create(const Twine &Model, unsigned Mode = all_read | all_write,
          OpenFlags ExtraFlags = OF_None);
+  /// Same as create(), but reports errors as std::error_code.
+  LLVM_ABI static ErrorOr<TempFile>
+  createWithErrorCode(const Twine &Model, unsigned Mode = all_read | all_write,
+                      OpenFlags ExtraFlags = OF_None);
   LLVM_ABI TempFile(TempFile &&Other);
   LLVM_ABI TempFile &operator=(TempFile &&Other);
 
@@ -921,6 +925,8 @@ class TempFile {
 
   // Keep this with the given name.
   LLVM_ABI Error keep(const Twine &Name);
+  /// Same as keep(const Twine &), but reports errors as std::error_code.
+  LLVM_ABI std::error_code keepWithErrorCode(const Twine &Name);
 
   // Keep this with the temporary name.
   LLVM_ABI Error keep();
diff --git a/llvm/lib/Support/Path.cpp b/llvm/lib/Support/Path.cpp
index 44011e2858ac2..97a202f851b57 100644
--- a/llvm/lib/Support/Path.cpp
+++ b/llvm/lib/Support/Path.cpp
@@ -1307,6 +1307,10 @@ Error TempFile::discard() {
 }
 
 Error TempFile::keep(const Twine &Name) {
+  return errorCodeToError(keepWithErrorCode(Name));
+}
+
+std::error_code TempFile::keepWithErrorCode(const Twine &Name) {
   assert(!Done);
   Done = true;
   // Always try to close and rename.
@@ -1351,10 +1355,10 @@ Error TempFile::keep(const Twine &Name) {
     TmpName = "";
 
   if (close(FD) == -1)
-    return errorCodeToError(errnoAsErrorCode());
+    return errnoAsErrorCode();
   FD = -1;
 
-  return errorCodeToError(RenameEC);
+  return RenameEC;
 }
 
 Error TempFile::keep() {
@@ -1379,11 +1383,20 @@ Error TempFile::keep() {
 
 Expected<TempFile> TempFile::create(const Twine &Model, unsigned Mode,
                                     OpenFlags ExtraFlags) {
+  ErrorOr<TempFile> Ret = createWithErrorCode(Model, Mode, ExtraFlags);
+  if (!Ret)
+    return errorCodeToError(Ret.getError());
+  return std::move(*Ret);
+}
+
+ErrorOr<TempFile> TempFile::createWithErrorCode(const Twine &Model,
+                                                unsigned Mode,
+                                                OpenFlags ExtraFlags) {
   int FD;
   SmallString<128> ResultPath;
   if (std::error_code EC =
           createUniqueFile(Model, FD, ResultPath, OF_Delete | ExtraFlags, Mode))
-    return errorCodeToError(EC);
+    return EC;
 
   TempFile Ret(ResultPath, FD);
 #ifdef _WIN32
@@ -1399,8 +1412,7 @@ Expected<TempFile> TempFile::create(const Twine &Model, unsigned Mode,
   if (SetSignalHandler && sys::RemoveFileOnSignal(ResultPath)) {
     // Make sure we delete the file when RemoveFileOnSignal fails.
     consumeError(Ret.discard());
-    std::error_code EC(errc::operation_not_permitted);
-    return errorCodeToError(EC);
+    return make_error_code(errc::operation_not_permitted);
   }
   return std::move(Ret);
 }
diff --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp
index 82197aba46598..e58d42c2dee47 100644
--- a/llvm/unittests/Support/Path.cpp
+++ b/llvm/unittests/Support/Path.cpp
@@ -1116,6 +1116,35 @@ TEST_F(FileSystemTest, TempFileDiscardDiscard) {
   ASSERT_FALSE(fs::exists(TestDirectory + "/keep"));
 }
 
+TEST_F(FileSystemTest, TempFileWithErrorCode) {
+  // Creating fails in a missing directory.
+  ErrorOr<fs::TempFile> Missing =
+      fs::TempFile::createWithErrorCode(TestDirectory + "/missing/test-%%%%");
+  ASSERT_EQ(Missing.getError(), errc::no_such_file_or_directory);
+
+  // Keeping succeeds.
+  ErrorOr<fs::TempFile> Kept =
+      fs::TempFile::createWithErrorCode(TestDirectory + "/test-%%%%");
+  ASSERT_NO_ERROR(Kept.getError());
+  ASSERT_NO_ERROR(Kept->keepWithErrorCode(TestDirectory + "/keep"));
+  ASSERT_TRUE(fs::exists(TestDirectory + "/keep"));
+  ASSERT_NO_ERROR(fs::remove(TestDirectory + "/keep"));
+
+  // Keeping fails if the destination is a non-empty directory, and the
+  // temporary file is removed.
+  ASSERT_NO_ERROR(fs::create_directory(TestDirectory + "/dir"));
+  ASSERT_NO_ERROR(fs::create_directory(TestDirectory + "/dir/sub"));
+  ErrorOr<fs::TempFile> NotKept =
+      fs::TempFile::createWithErrorCode(TestDirectory + "/test-%%%%");
+  ASSERT_NO_ERROR(NotKept.getError());
+  std::string TmpName = NotKept->TmpName;
+  ASSERT_TRUE(fs::exists(TmpName));
+  ASSERT_TRUE((bool)NotKept->keepWithErrorCode(TestDirectory + "/dir"));
+  ASSERT_FALSE(fs::exists(TmpName));
+  ASSERT_NO_ERROR(fs::remove(TestDirectory + "/dir/sub"));
+  ASSERT_NO_ERROR(fs::remove(TestDirectory + "/dir"));
+}
+
 TEST_F(FileSystemTest, TempFiles) {
   // Create a temp file.
   int FileDescriptor;

``````````

</details>


https://github.com/llvm/llvm-project/pull/243334


More information about the llvm-branch-commits mailing list