Skip to content

Commit

Permalink
Handling FaissException in few destructors of ResultHandler.h (#3311)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #3311

**Context**
[Issue 2948](#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
  • Loading branch information
kuarora authored and facebook-github-bot committed Mar 22, 2024
1 parent fa1f39e commit 798427c
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions faiss/impl/ResultHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
#pragma once

#include <faiss/impl/AuxIndexStructures.h>
#include <faiss/impl/FaissException.h>
#include <faiss/utils/Heap.h>
#include <faiss/utils/partitioning.h>
#include <iostream>

namespace faiss {

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

~SingleResultHandler() {
pres.finalize();
try {
// finalize the partial result
pres.finalize();
} catch (const faiss::FaissException& e) {
// Do nothing if allocation fails in finalizing partial results.
#ifndef NDEBUG
std::cerr << e.what() << std::endl;
#endif
}
}
};

Expand Down Expand Up @@ -559,8 +569,15 @@ struct RangeSearchBlockResultHandler : BlockResultHandler<C> {
}

~RangeSearchBlockResultHandler() {
if (partial_results.size() > 0) {
RangeSearchPartialResult::merge(partial_results);
try {
if (partial_results.size() > 0) {
RangeSearchPartialResult::merge(partial_results);
}
} catch (const faiss::FaissException& e) {
// Do nothing if allocation fails in merge.
#ifndef NDEBUG
std::cerr << e.what() << std::endl;
#endif
}
}
};
Expand Down

0 comments on commit 798427c

Please sign in to comment.