Skip to content

fix: Fix cancelling installation by not prematurely deleting patched APK #2490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 5, 2025

Conversation

kitadai31
Copy link
Contributor

@kitadai31 kitadai31 commented Apr 20, 2025

The issue had been caused by a race condition in asynchronous processing.
In setLastPatchedApp() method, await keyword was needed before deleteLastPatchedApp();

A code to reproduce:

Future<void> deleteLastPatchedApp() async {
  final PatchedApplication? app = getLastPatchedApp();
  if (app != null) {
    print('kitadai31: (2) Starting a 2 seconds wait');
    await Future.delayed(const Duration(seconds: 2)); // <- ADDED
    print('kitadai31: (5) 2 seconds elapsed. Delete files/lastPatchedApp.apk');
    final File file = File(app.patchedFilePath);
    await file.delete();
    await _prefs.remove('lastPatchedApp');
  }
}

Future<void> setLastPatchedApp(PatchedApplication app, File outFile) async {
  print('kitadai31: (1) calling deleteLastPatchedApp()');
  deleteLastPatchedApp();
  final Directory appCache = await getApplicationSupportDirectory();
  print('kitadai31: (3) Starting copying the patched APK to files/lastPatchedApp.apk');
  app.patchedFilePath =
      outFile.copySync('${appCache.path}/lastPatchedApp.apk').path;
  print('kitadai31: (4) Copy succeeded');
  app.fileSize = outFile.lengthSync();
  await _prefs.setString('lastPatchedApp', json.encode(app.toJson()));
}

The code is executed in the order (1) → (2) → (3) → (4) → (5).
As a result, lastPatchedApp.apk is sometimes randomly deleted after copying is succeeded.

Conclusion

The deleteLastPatchedApp() calling in setLastPatchedApp() is unnecessary from the beginning, so this PR removed this.
Because lastPatchedApp.apk and a pref will be overwritten without calling deleteLastPatchedApp().

The calling in reAssessPatchedApps() is also redundant because the file does not exist, so removed.

@kitadai31
Copy link
Contributor Author

kitadai31 commented Apr 20, 2025

#2332 (fix: Directly save APK to the cache when saving last patched app) can be reflected after this PR just to improve efficiency
but randomization should be reverted

@oSumAtrIX oSumAtrIX changed the title fix(Save patched app): Patched APK file immediately deleted and installation canceled fix: Installation cancelled due to premature deletion of patched APK Apr 25, 2025
@oSumAtrIX oSumAtrIX changed the title fix: Installation cancelled due to premature deletion of patched APK fix: Do not prematurrely delete patched APK to fix cancelling installation Apr 25, 2025
@oSumAtrIX oSumAtrIX changed the title fix: Do not prematurrely delete patched APK to fix cancelling installation fix: Fix installation cancellation by not prematurely deleting patched APK Apr 25, 2025
@oSumAtrIX oSumAtrIX changed the title fix: Fix installation cancellation by not prematurely deleting patched APK fix: Fix cancelling installation by not prematurely deleting patched APK Apr 25, 2025
Copy link

@Kimmon322 Kimmon322 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Run

@kitadai31 kitadai31 requested a review from oSumAtrIX April 28, 2025 04:23
@oSumAtrIX oSumAtrIX merged commit dedcb3c into ReVanced:dev May 5, 2025
github-actions bot pushed a commit that referenced this pull request May 5, 2025
# [1.25.0-dev.1](v1.24.1-dev.5...v1.25.0-dev.1) (2025-05-05)

### Bug Fixes

* Fix installation being cancelled at installation by not prematurely deleting patched APK  ([#2490](#2490)) ([dedcb3c](dedcb3c))
* Use device locale for app language (Default to English) ([#2488](#2488)) ([3074766](3074766))

### Features

* Add toggle to use pre-releases ([#2485](#2485)) ([89b48ce](89b48ce))
@kitadai31 kitadai31 deleted the fix/installation-canceled branch May 5, 2025 14:31
@E0697
Copy link

E0697 commented Jun 24, 2025

It doesn't seem to be fixed unfortunately. I was just able to reproduce it on v1.25.0-dev.1.

@kitadai31
Copy link
Contributor Author

It doesn't seem to be fixed unfortunately. I was just able to reproduce it on v1.25.0-dev.1.

Yes. This fix was accidentally rolled back while resolving conflict in another pull request.

This accident is already fixed, but the new dev release is not released due to a build failure.

@E0697
Copy link

E0697 commented Jun 24, 2025

Oh I see. Apologies and thanks a lot for the fix!

github-actions bot pushed a commit that referenced this pull request Jun 27, 2025
# [1.25.0](v1.24.0...v1.25.0) (2025-06-27)

### Bug Fixes

* "Save patched app" attempts to copy APK when patching fails ([#2565](#2565)) ([bdb0317](bdb0317))
* Correct supported required patch option types  ([#2475](#2475)) ([cde3f8d](cde3f8d))
* Crash using when Integer type in Patch Options ([#2453](#2453)) ([05575cc](05575cc))
* Fix installation being cancelled at installation by not prematurely deleting patched APK  ([#2490](#2490)) ([dedcb3c](dedcb3c))
* Log errors and warnings when compiling resources ([5c7d52c](5c7d52c))
* Nearly all rare cases of GPU renderer issues, and allow building on manager again ([#2602](#2602)) ([21ceada](21ceada))
* Obscure Flutter Impeller renderer bugs ([a5e909c](a5e909c))
* Unable to Share Logs due to missing ProGuard rules ([#2474](#2474)) ([915ec0e](915ec0e))
* Use device locale for app language (Default to English) ([#2488](#2488)) ([3074766](3074766))
* Use device locale when no preference is set ([#2483](#2483)) ([f79aa9e](f79aa9e))

### Features

* Add toggle to use pre-releases ([#2485](#2485)) ([89b48ce](89b48ce))
@KobeW50
Copy link
Contributor

KobeW50 commented Jun 27, 2025

Thank you @kitadai31

Can #2224 be closed now? I don't have the ability to close it

Edit: Ty oSum

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.

bug: Manager sometimes immediately cancels installation and Export APK button stops working
5 participants