From c41e63248a80994cef85d5c8e7ea3ef9cf991060 Mon Sep 17 00:00:00 2001 From: noctuum <25441068+noctuum@users.noreply.github.com> Date: Sun, 13 Sep 2026 08:44:32 +0500 Subject: [PATCH] Check the flush and sync on session file writes good() was checked before close(), where the data is actually flushed. --- src/session/download_storer.cc | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/session/download_storer.cc b/src/session/download_storer.cc index 1af2f1cd..1464a906 100644 --- a/src/session/download_storer.cc +++ b/src/session/download_storer.cc @@ -127,8 +127,12 @@ save_stream(const std::string& path, bool use_fsyncdisk, const std::stringstream if (!output.good()) throw torrent::storage_error("failed to write stream to file : " + path); + // The data only reaches the kernel here, so this is where a full disk is seen. output.close(); + if (!output.good()) + throw torrent::storage_error("failed to flush stream to file : " + path); + // Ensure that the new file is actually written to the disk int fd = ::open(path.c_str(), O_WRONLY); @@ -137,13 +141,19 @@ save_stream(const std::string& path, bool use_fsyncdisk, const std::stringstream if (use_fsyncdisk) { #ifdef __APPLE__ - ::fsync(fd); + int sync_result = ::fsync(fd); #else - ::fdatasync(fd); + int sync_result = ::fdatasync(fd); #endif + + if (sync_result == -1) { + ::close(fd); + throw torrent::storage_error("failed to sync file to disk : " + path); + } } - ::close(fd); + if (::close(fd) == -1) + throw torrent::storage_error("failed to close file descriptor : " + path); } } // namespace anonymous