Skip to content

Hold a process back while its module's APK is being replaced - #1117

Open
Dev4Mod wants to merge 1 commit into
JingMatrix:masterfrom
Dev4Mod:fix/stale-module-apk
Open

Dev4Mod wants to merge 1 commit into
JingMatrix:masterfrom
Dev4Mod:fix/stale-module-apk

Conversation

@Dev4Mod

@Dev4Mod Dev4Mod commented Nov 30, 2026 •

Copy link
Copy Markdown

Reinstalling a module and starting one of its targets right away handed the target the module as it was before the install. The package manager deletes the old /data/app directory as soon as the install returns, but the cache only moves to the new APK once ACTION_PACKAGE_ADDED reaches the daemon and performCacheUpdate has read the module again. Until that state is swapped in, getModulesForProcess serves the old LoadedModule: an apkPath that no longer exists, and the code that was read from it.

The window is the delivery of the broadcast plus the rebuild, and the rebuild is the long part on release builds, where every dex of the module is obfuscated as it is read. With a module of about 100 MB it stayed open for roughly 2.5 seconds after pm install returned; on a debug build, which does not obfuscate, it is under a second and hard to hit. A target started inside it logs VectorModuleClassLoader: Cannot open for the deleted path, and whatever the module reads from its own APK fails: resources injected from it failed about 80 times per start.

getModulesForProcess now checks that the APK of every module it is about to hand out still exists. When one is gone it takes the rebuild lock, which waits out a rebuild already running, and runs one itself if the state is still stale after that. performCacheUpdate already falls back to the package manager's path when the stored one is missing, so this holds even before the broadcast has arrived. A normal start only pays for a stat per module.

Measured on Android 13 with a release build. Starting the target 0.5 to 1.8 seconds after pm install, 8 starts out of 8 got the deleted path before this change. With it, 6 starts out of 6 at 0.5 to 0.9 seconds got the new APK, the process waiting 0.36 to 1.95 seconds for the rebuild instead of the usual 0.05.

Reinstalling a module and starting one of its targets right away handed
the target the module as it was before the install. The package manager
deletes the old /data/app directory as soon as the install returns, but
the cache only moves to the new APK once ACTION_PACKAGE_ADDED reaches
the daemon and performCacheUpdate has read the module again. Until that
state is swapped in, getModulesForProcess serves the old LoadedModule:
an apkPath that no longer exists, and the code that was read from it.

The window is the delivery of the broadcast plus the rebuild, and the
rebuild is the long part on release builds, where every dex of the
module is obfuscated as it is read. With a module of about 100 MB it
stayed open for roughly 2.5 seconds after `pm install` returned; on a
debug build, which does not obfuscate, it is under a second and hard to
hit. A target started inside it logs "VectorModuleClassLoader: Cannot
open" for the deleted path, and whatever the module reads from its own
APK fails: resources injected from it failed about 80 times per start.

getModulesForProcess now checks that the APK of every module it is
about to hand out still exists. When one is gone it takes the rebuild
lock, which waits out a rebuild already running, and runs one itself if
the state is still stale after that. performCacheUpdate already falls
back to the package manager's path when the stored one is missing, so
this holds even before the broadcast has arrived. A normal start only
pays for a stat per module.

Measured on Android 13 with a release build. Starting the target 0.5 to
1.8 seconds after `pm install`, 8 starts out of 8 got the deleted path
before this change. With it, 6 starts out of 6 at 0.5 to 0.9 seconds got
the new APK, the process waiting 0.36 to 1.95 seconds for the rebuild
instead of the usual 0.05.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant