Skip to content

Commit d975338

Browse files
addrdb: Remove temporary files created in SerializeFileDB. Fixes non-determinism in unit tests.
1 parent 7524376 commit d975338

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/addrdb.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,30 @@ bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data
4444
fs::path pathTmp = GetDataDir() / tmpfn;
4545
FILE *file = fsbridge::fopen(pathTmp, "wb");
4646
CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
47-
if (fileout.IsNull())
47+
if (fileout.IsNull()) {
48+
fileout.fclose();
49+
remove(pathTmp);
4850
return error("%s: Failed to open file %s", __func__, pathTmp.string());
51+
}
4952

5053
// Serialize
51-
if (!SerializeDB(fileout, data)) return false;
52-
if (!FileCommit(fileout.Get()))
54+
if (!SerializeDB(fileout, data)) {
55+
fileout.fclose();
56+
remove(pathTmp);
57+
return false;
58+
}
59+
if (!FileCommit(fileout.Get())) {
60+
fileout.fclose();
61+
remove(pathTmp);
5362
return error("%s: Failed to flush file %s", __func__, pathTmp.string());
63+
}
5464
fileout.fclose();
5565

5666
// replace existing file, if any, with new file
57-
if (!RenameOver(pathTmp, path))
67+
if (!RenameOver(pathTmp, path)) {
68+
remove(pathTmp);
5869
return error("%s: Rename-into-place failed", __func__);
70+
}
5971

6072
return true;
6173
}

0 commit comments

Comments
 (0)