Metal: early out WaitForCommandsToBeScheduled if already destroyed.
Bug: b/41488814
Change-Id: I8355dc6eade5c7d0345bd049b4559d8d45adde64
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/178800
Reviewed-by: Austin Eng <enga@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Quyen Le <lehoangquyen@chromium.org>
diff --git a/src/dawn/native/metal/QueueMTL.mm b/src/dawn/native/metal/QueueMTL.mm
index 1f7901e..21e9c71 100644
--- a/src/dawn/native/metal/QueueMTL.mm
+++ b/src/dawn/native/metal/QueueMTL.mm
@@ -104,6 +104,9 @@
}
void Queue::WaitForCommandsToBeScheduled() {
+ if (!IsAlive()) {
+ return;
+ }
if (GetDevice()->ConsumedError(SubmitPendingCommandBuffer())) {
return;
}
diff --git a/src/dawn/tests/BUILD.gn b/src/dawn/tests/BUILD.gn
index b2b0f93..aaa8aa2 100644
--- a/src/dawn/tests/BUILD.gn
+++ b/src/dawn/tests/BUILD.gn
@@ -825,6 +825,7 @@
"white_box/GPUTimestampCalibrationTests.h",
"white_box/GPUTimestampCalibrationTests_Metal.mm",
"white_box/MetalAutoreleasePoolTests.mm",
+ "white_box/MetalBackendTests.cpp",
]
# If a "build with ARC" config is present, remove it.
diff --git a/src/dawn/tests/white_box/MetalBackendTests.cpp b/src/dawn/tests/white_box/MetalBackendTests.cpp
new file mode 100644
index 0000000..76221ba
--- /dev/null
+++ b/src/dawn/tests/white_box/MetalBackendTests.cpp
@@ -0,0 +1,58 @@
+// Copyright 2024 The Dawn & Tint Authors
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this
+// list of conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+// this list of conditions and the following disclaimer in the documentation
+// and/or other materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "dawn/tests/DawnTest.h"
+
+#include "dawn/native/MetalBackend.h"
+
+namespace dawn::native {
+namespace {
+
+class MetalBackendTests : public DawnTest {
+ private:
+ void SetUp() override {
+ DawnTest::SetUp();
+ DAWN_TEST_UNSUPPORTED_IF(UsesWire());
+ }
+};
+
+// Test WaitForCommandsToBeScheduled() call without any crash.
+TEST_P(MetalBackendTests, WaitForCommandsToBeScheduledOK) {
+ metal::WaitForCommandsToBeScheduled(device.Get());
+}
+
+// Test WaitForCommandsToBeScheduled() won't crash even if the device is destroyed.
+TEST_P(MetalBackendTests, WaitForCommandsToBeScheduledOnDestroyedDevice) {
+ auto device2 = CreateDevice();
+ device2.Destroy();
+ metal::WaitForCommandsToBeScheduled(device2.Get());
+}
+
+DAWN_INSTANTIATE_TEST(MetalBackendTests, MetalBackend());
+
+} // anonymous namespace
+} // namespace dawn::native