From bac99b5f3bfbf6cdb475d072125ac88327f66806 Mon Sep 17 00:00:00 2001 From: Song Chun Fan Date: Tue, 4 Feb 2025 14:26:47 -0800 Subject: [pm] move PackageInstallerSession.extractNativeLibraries to IO thread PackageInstallerSession.write() holds mLock and is on the IO thread. Prior to this CL, PackageInstallerSession.extractNativeLibraries() also holds mLock but was on the mInstallThread. This causes a lock contention where the write() can ANR waiting for mLock held by extractNativeLibraries(). Move the latter to the IO thread to avoid such lock contentions. FLAG: EXEMPT refactor BUG: 393698598 Test: manual Change-Id: I99418b8983a63751e6d485efae947a49a6a88db5 --- .../android/server/pm/PackageInstallerSession.java | 41 +++++++++++++++++----- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/services/core/java/com/android/server/pm/PackageInstallerSession.java b/services/core/java/com/android/server/pm/PackageInstallerSession.java index 1b41c3617a05..352985d5a023 100644 --- a/services/core/java/com/android/server/pm/PackageInstallerSession.java +++ b/services/core/java/com/android/server/pm/PackageInstallerSession.java @@ -189,6 +189,7 @@ import com.android.internal.util.IndentingPrintWriter; import com.android.internal.util.Preconditions; import com.android.modules.utils.TypedXmlPullParser; import com.android.modules.utils.TypedXmlSerializer; +import com.android.server.IoThread; import com.android.server.LocalServices; import com.android.server.art.ArtManagedInstallFileHelper; import com.android.server.pm.Installer.InstallerException; @@ -236,6 +237,8 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub { private static final int MSG_SESSION_VALIDATION_FAILURE = 5; private static final int MSG_PRE_APPROVAL_REQUEST = 6; + private static final int MSG_ON_NATIVE_LIBS_EXTRACTED = 7; + /** XML constants used for persisting a session */ static final String TAG_SESSION = "session"; static final String TAG_CHILD_SESSION = "childSession"; @@ -943,6 +946,8 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub { case MSG_PRE_APPROVAL_REQUEST: handlePreapprovalRequest(); break; + case MSG_ON_NATIVE_LIBS_EXTRACTED: + handleOnNativeLibsExtracted(); } return true; @@ -2908,15 +2913,13 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub { } private void verify() { + // Extract native libraries on the IO thread before proceeding to the verification + runExtractNativeLibraries(); + } + + @WorkerThread + private void handleOnNativeLibsExtracted() { try { - List children = getChildSessions(); - if (isMultiPackage()) { - for (PackageInstallerSession child : children) { - child.extractNativeLibraries(); - } - } else { - extractNativeLibraries(); - } verifyNonStaged(); } catch (PackageManagerException e) { final String completeMsg = ExceptionUtils.getCompleteMessage(e); @@ -2926,6 +2929,27 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub { } } + private void runExtractNativeLibraries() { + IoThread.getHandler().post(() -> { + try { + List children = getChildSessions(); + if (isMultiPackage()) { + for (PackageInstallerSession child : children) { + child.extractNativeLibraries(); + } + } else { + extractNativeLibraries(); + } + mHandler.obtainMessage(MSG_ON_NATIVE_LIBS_EXTRACTED).sendToTarget(); + } catch (PackageManagerException e) { + final String completeMsg = ExceptionUtils.getCompleteMessage(e); + final String errorMsg = PackageManager.installStatusToString(e.error, completeMsg); + setSessionFailed(e.error, errorMsg); + onSessionVerificationFailure(e.error, errorMsg); + } + }); + } + private IntentSender getRemoteStatusReceiver() { synchronized (mLock) { return mRemoteStatusReceiver; @@ -3072,6 +3096,7 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub { } } + @WorkerThread private void extractNativeLibraries() throws PackageManagerException { synchronized (mLock) { if (mPackageLite != null) { -- cgit v1.2.3-59-g8ed1b