Clean up staged session data on validation failure

Bug: 173132101
Test: manual test using `adb install --staged foo.apk` multiple times
Test: manual test using `adb install-multi-package --staged foo.apk bar.apk`
Change-Id: Idd6597cd0d2dda34a8a7626b585401eeee39c31f
Merged-In: Idd6597cd0d2dda34a8a7626b585401eeee39c31f
(cherry picked from commit f54412183a19916f6ce704f113967b8b19ef6ed8)
diff --git a/services/core/java/com/android/server/pm/PackageInstallerSession.java b/services/core/java/com/android/server/pm/PackageInstallerSession.java
index 1976ffd..5d4c7d5 100644
--- a/services/core/java/com/android/server/pm/PackageInstallerSession.java
+++ b/services/core/java/com/android/server/pm/PackageInstallerSession.java
@@ -881,6 +881,12 @@
                             + mParentSessionId +  " and may not be committed directly.");
         }
         if (!markAsCommitted(statusReceiver, forTransfer)) {
+            if (isStaged()) {
+                // cleanStageDir calls mSessionProvider to get hold of child sessions, which in turn
+                // needs PackageInstallerService#mSessions lock. So we should not call cleanStageDir
+                // while holding mLock to avoid lock inversion.
+                cleanStageDir();
+            }
             return;
         }
         if (isMultiPackage()) {