From 32242a06e88801d9a0a7c903fcca6525d2c0f522 Mon Sep 17 00:00:00 2001 From: "Philip P. Moltmann" Date: Wed, 13 Sep 2017 14:17:21 -0700 Subject: [PATCH] Do not fail session.commit on current thread The adb install command calls session.commit() requires the failure to be delivered on a different thread than the one that calls session.commit(). Test: Tried to install invalid package via adb install Change-Id: I8b616c750afa81f126fa3d3576d21df8274b1432 Fixes: 65555295 --- .../server/pm/PackageInstallerSession.java | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/services/core/java/com/android/server/pm/PackageInstallerSession.java b/services/core/java/com/android/server/pm/PackageInstallerSession.java index 810c399e9f8df..3f1c8a136307c 100644 --- a/services/core/java/com/android/server/pm/PackageInstallerSession.java +++ b/services/core/java/com/android/server/pm/PackageInstallerSession.java @@ -118,6 +118,7 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub { private static final String REMOVE_SPLIT_MARKER_EXTENSION = ".removed"; private static final int MSG_COMMIT = 0; + private static final int MSG_SESSION_FINISHED_WITH_EXCEPTION = 1; /** XML constants used for persisting a session */ static final String TAG_SESSION = "session"; @@ -274,15 +275,27 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub { private final Handler.Callback mHandlerCallback = new Handler.Callback() { @Override public boolean handleMessage(Message msg) { - synchronized (mLock) { - try { - commitLocked(); - } catch (PackageManagerException e) { - final String completeMsg = ExceptionUtils.getCompleteMessage(e); - Slog.e(TAG, "Commit of session " + sessionId + " failed: " + completeMsg); - destroyInternal(); - dispatchSessionFinished(e.error, completeMsg, null); - } + switch (msg.what) { + case MSG_COMMIT: + synchronized (mLock) { + try { + commitLocked(); + } catch (PackageManagerException e) { + final String completeMsg = ExceptionUtils.getCompleteMessage(e); + Slog.e(TAG, + "Commit of session " + sessionId + " failed: " + completeMsg); + destroyInternal(); + dispatchSessionFinished(e.error, completeMsg, null); + } + } + + break; + case MSG_SESSION_FINISHED_WITH_EXCEPTION: + PackageManagerException e = (PackageManagerException) msg.obj; + + dispatchSessionFinished(e.error, ExceptionUtils.getCompleteMessage(e), + null); + break; } return true; @@ -705,9 +718,13 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub { } catch (IOException e) { throw new IllegalArgumentException(e); } catch (PackageManagerException e) { - // Do now throw an exception here to stay compatible with O and older destroyInternal(); - dispatchSessionFinished(e.error, ExceptionUtils.getCompleteMessage(e), null); + + // Cannot call dispatchFinal synchronous as this might be called from inside the + // system server on the main thread. Hence the call back scheduled in + // dispachFinal has to be scheduled on a different thread. + mHandler.obtainMessage(MSG_SESSION_FINISHED_WITH_EXCEPTION, e).sendToTarget(); + return; } }