Skip to content

Commit d40dd1e

Browse files
Serialize destruction of the delegate (#6111)
Serialize destruction of the delegate (#5996) Summary: Pull Request resolved: #5996 When workspace sharing is enabled, if we call `destroy()` from multiple threads it is possible that we enter in `xnn_delete_runtime` from multiple threads. It is not safe to do so, and can cause double free. This serializes the destroy calls for the singleton backend object if/when called multiple times. Not locking in the failure case because we may not have a real memory to free i.e. empty workspace? Reviewed By: digantdesai Differential Revision: D63917052 fbshipit-source-id: 61e6b1c54f887a54501a4fd3433f8470e74dbca3 (cherry picked from commit 566902b) Co-authored-by: Gregory Comer <[email protected]>
1 parent 6655c3b commit d40dd1e

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

backends/xnnpack/runtime/XNNPACKBackend.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ class XnnpackBackend final : public ::executorch::runtime::BackendInterface {
132132

133133
void destroy(DelegateHandle* handle) const override {
134134
if (handle != nullptr) {
135+
#ifdef ENABLE_XNNPACK_SHARED_WORKSPACE
136+
// This is needed to serialize access to xnn_delete_runtime which is not
137+
// thread safe. This can heppen when multiple threads call destroy() on
138+
// the same backend instance.
139+
const std::lock_guard<std::mutex> lock(workspace_mutex_);
140+
#endif
135141
auto executor = static_cast<xnnpack::delegate::XNNExecutor*>(handle);
136142
#ifdef ENABLE_XNNPACK_PROFILING
137143
executor->print_avg_op_timings();

0 commit comments

Comments
 (0)