Fix crash in OpenedFileCounter::ensureMaxOpenFileLimit()

The crash happens if PieceStorage and/or DiskAdaptor are not
initialized in one of active RequestGroups.
This commit is contained in:
Tatsuhiro Tsujikawa 2014-07-30 22:31:48 +09:00
parent bd0a3960df
commit e74b39deaf

View File

@ -71,12 +71,32 @@ void OpenedFileCounter::ensureMaxOpenFileLimit(size_t numNewFiles)
std::advance(mark,
SimpleRandomizer::getInstance()->
getRandomNumber(requestGroups.size()));
auto closeFun = [&left](const std::shared_ptr<RequestGroup>& group)
{
auto& ps = group->getPieceStorage();
if(!ps) {
return;
}
auto diskAdaptor = ps->getDiskAdaptor();
if(!diskAdaptor) {
return;
}
left -= diskAdaptor->tryCloseFile(left);
};
for(auto i = mark; i != std::end(requestGroups) && left > 0; ++i) {
left -= (*i)->getPieceStorage()->getDiskAdaptor()->tryCloseFile(left);
closeFun(*i);
}
for(auto i = std::begin(requestGroups); i != mark && left > 0; ++i) {
left -= (*i)->getPieceStorage()->getDiskAdaptor()->tryCloseFile(left);
closeFun(*i);
}
assert(left == 0);
numOpenFiles_ += numNewFiles - numClose;
}