Skip to content

Commit 0dd171d

Browse files
kuarorafacebook-github-bot
authored andcommitted
Handling FaissException in few destructors of ResultHandler.h (facebookresearch#3311)
Summary: Pull Request resolved: facebookresearch#3311 **Context** [Issue 2948](facebookresearch#2948) highlights potential issue of calling allocation on result handler which may throw exception but it is not handled. **In this diff**, I observed two calls where we may potentially call allocation in ResultHandler.h and handled FaissException. 1/ partial result when finalized in ~SingleResultHandler 2/ partial result when merged in ~RangeSearchBlockResultHandler Reviewed By: junjieqi Differential Revision: D55258213 fbshipit-source-id: 259be472e73619b2fcb0ea480d6d3486affeafdf
1 parent 3f13bab commit 0dd171d

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

faiss/impl/ResultHandler.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
#pragma once
1313

1414
#include <faiss/impl/AuxIndexStructures.h>
15+
#include <faiss/impl/FaissException.h>
1516
#include <faiss/utils/Heap.h>
1617
#include <faiss/utils/partitioning.h>
18+
#include <iostream>
1719

1820
namespace faiss {
1921

@@ -504,7 +506,15 @@ struct RangeSearchBlockResultHandler : BlockResultHandler<C> {
504506
void end() {}
505507

506508
~SingleResultHandler() {
507-
pres.finalize();
509+
try {
510+
// finalize the partial result
511+
pres.finalize();
512+
} catch (const faiss::FaissException& e) {
513+
// Do nothing if allocation fails in finalizing partial results.
514+
#ifndef NDEBUG
515+
std::cerr << e.what() << std::endl;
516+
#endif
517+
}
508518
}
509519
};
510520

@@ -559,8 +569,15 @@ struct RangeSearchBlockResultHandler : BlockResultHandler<C> {
559569
}
560570

561571
~RangeSearchBlockResultHandler() {
562-
if (partial_results.size() > 0) {
563-
RangeSearchPartialResult::merge(partial_results);
572+
try {
573+
if (partial_results.size() > 0) {
574+
RangeSearchPartialResult::merge(partial_results);
575+
}
576+
} catch (const faiss::FaissException& e) {
577+
// Do nothing if allocation fails in merge.
578+
#ifndef NDEBUG
579+
std::cerr << e.what() << std::endl;
580+
#endif
564581
}
565582
}
566583
};

0 commit comments

Comments
 (0)