Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Impeller] Fix a race that can abort the process if the Vulkan context is destroyed while pipeline creation tasks are pending #50883

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions impeller/base/base_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

#include "flutter/testing/testing.h"
#include "impeller/base/promise.h"
#include "impeller/base/strings.h"
#include "impeller/base/thread.h"

Expand Down Expand Up @@ -233,5 +234,21 @@ TEST(ConditionVariableTest, TestsCriticalSectionAfterWait) {
ASSERT_EQ(sum, kThreadCount);
}

TEST(BaseTest, NoExceptionPromiseValue) {
NoExceptionPromise<int> wrapper;
std::future future = wrapper.get_future();
wrapper.set_value(123);
ASSERT_EQ(future.get(), 123);
}

TEST(BaseTest, NoExceptionPromiseEmpty) {
auto wrapper = std::make_shared<NoExceptionPromise<int>>();
std::future future = wrapper->get_future();

// Destroy the empty promise with the future still pending. Verify that the
// process does not abort while destructing the promise.
wrapper.reset();
}

} // namespace testing
} // namespace impeller
28 changes: 28 additions & 0 deletions impeller/base/promise.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,34 @@ std::future<T> RealizedFuture(T t) {
return future;
}

// Wraps a std::promise and completes the promise with a value during
// destruction if the promise does not already have a value.
//
// By default the std::promise destructor will complete an empty promise with an
// exception. This will fail because Flutter is built without exception support.
template <typename T>
class NoExceptionPromise {
public:
NoExceptionPromise() = default;

~NoExceptionPromise() {
if (!value_set_) {
promise_.set_value({});
}
}

std::future<T> get_future() { return promise_.get_future(); }

void set_value(const T& value) {
promise_.set_value(value);
value_set_ = true;
}

private:
std::promise<T> promise_;
bool value_set_ = false;
};

} // namespace impeller

#endif // FLUTTER_IMPELLER_BASE_PROMISE_H_
2 changes: 1 addition & 1 deletion impeller/renderer/backend/vulkan/pipeline_library_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ PipelineFuture<PipelineDescriptor> PipelineLibraryVK::GetPipeline(
}

auto promise = std::make_shared<
std::promise<std::shared_ptr<Pipeline<PipelineDescriptor>>>>();
NoExceptionPromise<std::shared_ptr<Pipeline<PipelineDescriptor>>>>();
auto pipeline_future =
PipelineFuture<PipelineDescriptor>{descriptor, promise->get_future()};
pipelines_[descriptor] = pipeline_future;
Expand Down