From 509ac11749ed91b8ca36beb95b973b1c05f87f78 Mon Sep 17 00:00:00 2001 From: Tom Hicks Date: Tue, 22 Sep 2026 21:43:53 -0700 Subject: [PATCH] Throw and safely catch InterruptedException on task cancellation --- .../majinnaibu/monstercards/MainActivity.java | 9 ++++++- .../importers/GitRepoImporterService.java | 24 +++++++++++++------ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/Android/app/src/main/java/com/majinnaibu/monstercards/MainActivity.java b/Android/app/src/main/java/com/majinnaibu/monstercards/MainActivity.java index a2a585c..bf22806 100644 --- a/Android/app/src/main/java/com/majinnaibu/monstercards/MainActivity.java +++ b/Android/app/src/main/java/com/majinnaibu/monstercards/MainActivity.java @@ -268,7 +268,14 @@ public class MainActivity extends AppCompatActivity { return totalImported; }; } else { - importTask = () -> GitRepoImporterService.importFromGitRepository(getApplicationContext(), source, () -> mImportDisposable != null && mImportDisposable.isDisposed()); + importTask = () -> { + try { + return GitRepoImporterService.importFromGitRepository(getApplicationContext(), source, () -> mImportDisposable != null && mImportDisposable.isDisposed()); + } catch (InterruptedException e) { + Logger.logError("Import task was interrupted", e); + return 0; // Return gracefully on interruption + } + }; } mImportDisposable = Single.fromCallable(importTask) diff --git a/Android/app/src/main/java/com/majinnaibu/monstercards/importers/GitRepoImporterService.java b/Android/app/src/main/java/com/majinnaibu/monstercards/importers/GitRepoImporterService.java index 77663c6..8fb9f20 100644 --- a/Android/app/src/main/java/com/majinnaibu/monstercards/importers/GitRepoImporterService.java +++ b/Android/app/src/main/java/com/majinnaibu/monstercards/importers/GitRepoImporterService.java @@ -80,7 +80,7 @@ public class GitRepoImporterService { return totalImported; } - private static void downloadZip(String urlStr, File dest, BooleanSupplier isCancelled) throws IOException { + private static void downloadZip(String urlStr, File dest, BooleanSupplier isCancelled) throws IOException, InterruptedException { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); @@ -106,13 +106,15 @@ public class GitRepoImporterService { byte[] buffer = new byte[8192]; int count; while ((count = in.read(buffer)) != -1) { - if (isCancelled.getAsBoolean()) break; + if (isCancelled.getAsBoolean()) { + throw new InterruptedException("Git download was cancelled."); + } out.write(buffer, 0, count); } } } - private static int unzipAndFilter(File zipFile, File extractDir, String filterExtension, String subfolder, BooleanSupplier isCancelled) { + private static int unzipAndFilter(File zipFile, File extractDir, String filterExtension, String subfolder, BooleanSupplier isCancelled) throws InterruptedException { int extractedCount = 0; try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile))) { ZipEntry entry; @@ -123,7 +125,7 @@ public class GitRepoImporterService { } catch (Exception e) { Logger.logError("Failed to close zip entry upon cancellation", e); } - break; + throw new InterruptedException("Git extraction was cancelled."); } if (!entry.isDirectory()) { String name = entry.getName(); @@ -137,9 +139,13 @@ public class GitRepoImporterService { byte[] buffer = new byte[8192]; int count; while ((count = zis.read(buffer)) != -1) { - if (isCancelled.getAsBoolean()) break; + if (isCancelled.getAsBoolean()) { + throw new InterruptedException("Git extraction was cancelled during file read."); + } fos.write(buffer, 0, count); } + } catch (InterruptedException e) { + throw e; } catch (Exception e) { Logger.logError("Failed to write extracted file to disk: " + name, e); } @@ -151,13 +157,15 @@ public class GitRepoImporterService { Logger.logError("Failed to close zip entry", e); } } + } catch (InterruptedException e) { + throw e; } catch (Exception e) { Logger.logError("Failed during unzip stream processing", e); } return extractedCount; } - private static int processDirectory(File dir, EntityImporter importer, MonsterRepository repository, BooleanSupplier isCancelled) { + private static int processDirectory(File dir, EntityImporter importer, MonsterRepository repository, BooleanSupplier isCancelled) throws InterruptedException { int importedCount = 0; File[] files = dir.listFiles(); if (files == null) { @@ -168,7 +176,9 @@ public class GitRepoImporterService { Logger.logWTF("Attempting to process " + files.length + " JSON files."); for (File file : files) { - if (isCancelled.getAsBoolean()) break; + if (isCancelled.getAsBoolean()) { + throw new InterruptedException("Git import processing was cancelled."); + } if (file.isFile()) { String content = readFileContent(file); if (content != null) {