From fc90538d82d4681d94cd5acf817d70a00368fa6a Mon Sep 17 00:00:00 2001
From: Esben Haabendal <esben@geanix.com>
Date: Wed, 29 Mar 2023 11:49:52 +0200
Subject: [PATCH] openharmony-standard: Workaround for race condition in ace
 engine

When starting up abilities, there is a race condition between OnSurfaceChanged
and RunPage, which both are executed as PostTask on the same task executor.
This executor works in FIFO mode, and if (when) the OnSurfaceChanged comes
first, it will fail with a nullptr check on runningPage_.  This is because the
runningPage_ variable is set by the RunPage PostTask.

This patch is obviously just a workaround, and not a proper fix.  Hopefully, 500
ms will be enough in all relevant situations, but at least in theory, the
problem can still appear.

Signed-off-by: Esben Haabendal <esben@geanix.com>
---
 ...ace_engine-race-condition-workaround.patch | 38 +++++++++++++++++++
 .../openharmony/openharmony-standard_3.0.bb   |  1 +
 2 files changed, 39 insertions(+)
 create mode 100644 recipes-openharmony/openharmony/openharmony-standard-3.0/foundation_ace_engine-race-condition-workaround.patch

diff --git a/recipes-openharmony/openharmony/openharmony-standard-3.0/foundation_ace_engine-race-condition-workaround.patch b/recipes-openharmony/openharmony/openharmony-standard-3.0/foundation_ace_engine-race-condition-workaround.patch
new file mode 100644
index 00000000..d6a398ab
--- /dev/null
+++ b/recipes-openharmony/openharmony/openharmony-standard-3.0/foundation_ace_engine-race-condition-workaround.patch
@@ -0,0 +1,38 @@
+# SPDX-FileCopyrightText: Huawei Inc.
+#
+# SPDX-License-Identifier: Apache-2.0
+
+Apply to foundation/ace/ace_engine repository of OpenHarmony 3.0.1 codebase.
+
+When starting up abilities, there is a race condition between OnSurfaceChanged
+and RunPage, which both are executed as PostTask on the same task executor. This
+executor works in FIFO mode, and if (when) the OnSurfaceChanged comes first, it
+will fail with a nullptr check on runningPage_. This is because the runningPage_
+variable is set by the RunPage PostTask.
+
+This patch is obviously just a workaround, and not a proper fix.  Hopefully, 500
+ms will be enough in all relevant situations, but at least in theory, the
+problem can still appear.
+
+Signed-off-by: Esben Haabendal <esben@geanix.com>
+Upstream-Status: Invalid [workaround]
+
+diff --git a/adapter/ohos/cpp/ace_container.cpp b/adapter/ohos/cpp/ace_container.cpp
+index 9dea6db1e527..ac3e280eacb5 100644
+--- a/adapter/ohos/cpp/ace_container.cpp
++++ b/adapter/ohos/cpp/ace_container.cpp
+@@ -348,8 +366,12 @@ void AceContainer::InitializeCallback()
+ 
+     auto&& viewChangeCallback = [context = pipelineContext_](int32_t width, int32_t height) {
+         ACE_SCOPED_TRACE("ViewChangeCallback(%d, %d)", width, height);
+-        context->GetTaskExecutor()->PostTask(
+-            [context, width, height]() { context->OnSurfaceChanged(width, height); }, TaskExecutor::TaskType::UI);
++        LOGI("context->OnSurfaceChanged [PostTask]");
++        // This has been observed racing with QjsEngineInstance::FireJsEvent(),
++        // so we delay this slightly as a kind of workaround...
++        LOGI("Scheduling OnSurfaceChanged to be run in 500 ms to workaround race condition with RunPage");
++        context->GetTaskExecutor()->PostDelayedTask(
++            [context, width, height]() { context->OnSurfaceChanged(width, height); }, TaskExecutor::TaskType::UI, 500);
+     };
+     aceView_->RegisterViewChangeCallback(viewChangeCallback);
+ 
diff --git a/recipes-openharmony/openharmony/openharmony-standard_3.0.bb b/recipes-openharmony/openharmony/openharmony-standard_3.0.bb
index dbf7c0f9..77d2f1d2 100644
--- a/recipes-openharmony/openharmony/openharmony-standard_3.0.bb
+++ b/recipes-openharmony/openharmony/openharmony-standard_3.0.bb
@@ -102,6 +102,7 @@ SRC_URI += "file://test-xts-acts-fix-hicolliecpptest.patch;patchdir=${S}/test/xt
 SRC_URI += "file://test-xts-acts-increase-testsuite-timeouts.patch;patchdir=${S}/test/xts/acts"
 SRC_URI += "file://actsgetwantalltest-wait-between-testcases.patch;patchdir=${S}/test/xts/acts"
 SRC_URI += "file://actsfeatureabilitytest-wait-between-testcases.patch;patchdir=${S}/test/xts/acts"
+SRC_URI += "file://foundation_ace_engine-race-condition-workaround.patch;patchdir=${S}/foundation/ace/ace_engine"
 
 inherit python3native gn_base ptest
 
-- 
GitLab