SERVER-109621 When handling std::fstream in SorterBase::File check fail/bad bits (#43921)

GitOrigin-RevId: ba0b9320f9f55773a44a8011ec0f923371c1670c
This commit is contained in:
Wei Hu 2025-11-14 08:05:43 -08:00 committed by MongoDB Bot
parent 0c76c7790f
commit a973e3f682
2 changed files with 21 additions and 9 deletions

View File

@ -355,6 +355,11 @@ private:
*/
void _ensureOpenForWriting();
/**
* Returns lastPosixError() or a generic iostream error code if no posix error set.
*/
std::error_code _getErrorCode();
// The current offset of the end of the storage if there may be unflushed data, or -1 if the
// file either has not yet been opened or has been flushed.
std::streamoff _offset = -1;

View File

@ -1453,7 +1453,7 @@ inline void SorterFile::read(std::streamoff offset, std::streamsize size, void*
uassert(5479100,
str::stream() << "Error flushing file " << _path.string() << ": "
<< errorMessage(lastPosixError()),
<< errorMessage(_getErrorCode()),
_file);
}
@ -1462,7 +1462,7 @@ inline void SorterFile::read(std::streamoff offset, std::streamsize size, void*
uassert(16817,
str::stream() << "Error reading file " << _path.string() << ": "
<< errorMessage(lastPosixError()),
<< errorMessage(_getErrorCode()),
_file);
invariant(_file.gcount() == size,
@ -1471,7 +1471,7 @@ inline void SorterFile::read(std::streamoff offset, std::streamsize size, void*
uassert(51049,
str::stream() << "Error reading file " << _path.string() << ": "
<< errorMessage(lastPosixError()),
<< errorMessage(_getErrorCode()),
_file.tellg() >= 0);
}
@ -1490,12 +1490,10 @@ inline void SorterFile::write(const char* data, std::streamsize size) {
str::stream() << ex.what() << ": " << _path.string());
}
uasserted(5642403,
str::stream() << "Error writing to file " << _path.string() << ": "
<< errorMessage(lastPosixError()));
} catch (const std::exception&) {
str::stream() << "Error writing to file " << _path.string() << ": " << ex.what());
} catch (const std::exception& ex) {
uasserted(16821,
str::stream() << "Error writing to file " << _path.string() << ": "
<< errorMessage(lastPosixError()));
str::stream() << "Error writing to file " << _path.string() << ": " << ex.what());
}
}
@ -1517,7 +1515,7 @@ inline void SorterFile::_open() {
uassert(16818,
str::stream() << "Error opening file " << _path.string() << ": "
<< errorMessage(lastPosixError()),
<< errorMessage(_getErrorCode()),
_file.good());
if (_stats) {
@ -1539,6 +1537,15 @@ inline void SorterFile::_ensureOpenForWriting() {
}
}
inline std::error_code SorterFile::_getErrorCode() {
auto err = lastPosixError();
// If no posix error, check for iostream error.
if (!err && (_file.fail() || _file.bad())) {
return std::make_error_code(std::io_errc::stream);
}
return err;
}
//
// SortedStorageWriter
//