-
-
Notifications
You must be signed in to change notification settings - Fork 265
fix(android): harden download polling against DB crashes and zombie rows #456
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
Open
Ayush7614
wants to merge
1
commit into
off-grid-ai:main
Choose a base branch
from
Ayush7614:fix/android-download-zombie-polling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
__tests__/integration/models/downloadZombieRecovery.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| const mockBackgroundDownloadService = { | ||
| isAvailable: jest.fn(), | ||
| getActiveDownloads: jest.fn(), | ||
| }; | ||
|
|
||
| jest.mock('../../../src/services/backgroundDownloadService', () => ({ | ||
| backgroundDownloadService: mockBackgroundDownloadService, | ||
| })); | ||
|
|
||
| const { hydrateDownloadStore } = require('../../../src/services/downloadHydration'); | ||
| const { useDownloadStore } = require('../../../src/stores/downloadStore'); | ||
| const { isRetryable } = require('../../../src/utils/downloadErrors'); | ||
|
|
||
| describe('download zombie recovery integration', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| useDownloadStore.setState({ | ||
| downloads: {}, | ||
| downloadIdIndex: {}, | ||
| repairingVisionIds: {}, | ||
| }); | ||
| mockBackgroundDownloadService.isAvailable.mockReturnValue(true); | ||
| }); | ||
|
|
||
| it('hydrates interrupted downloads as failed with a retryable reason code', async () => { | ||
| mockBackgroundDownloadService.getActiveDownloads.mockResolvedValue([ | ||
| { | ||
| downloadId: 'dl-zombie-1', | ||
| modelId: 'qwen3-4b', | ||
| modelKey: 'qwen3-4b:Q4_K_M.gguf', | ||
| fileName: 'Q4_K_M.gguf', | ||
| modelType: 'text', | ||
| status: 'failed', | ||
| bytesDownloaded: 0, | ||
| totalBytes: 2_500_000_000, | ||
| reason: 'The download was interrupted.', | ||
| reasonCode: 'download_interrupted', | ||
| createdAt: 1_700_000_000_000, | ||
| }, | ||
| ]); | ||
|
|
||
| await hydrateDownloadStore(); | ||
|
|
||
| const entry = useDownloadStore.getState().downloads['qwen3-4b:Q4_K_M.gguf']; | ||
| expect(entry).toBeDefined(); | ||
| expect(entry.status).toBe('failed'); | ||
| expect(entry.errorCode).toBe('download_interrupted'); | ||
| expect(entry.errorMessage).toBe('The download was interrupted.'); | ||
| expect(isRetryable(entry.errorCode)).toBe(true); | ||
| }); | ||
|
|
||
| it('does not drop failed rows so the Download Manager can show retry', async () => { | ||
| mockBackgroundDownloadService.getActiveDownloads.mockResolvedValue([ | ||
| { | ||
| downloadId: 'dl-zombie-2', | ||
| modelId: 'whisper-base', | ||
| modelKey: 'whisper-base:ggml-base.en.bin', | ||
| fileName: 'ggml-base.en.bin', | ||
| modelType: 'stt', | ||
| status: 'failed', | ||
| bytesDownloaded: 32, | ||
| totalBytes: 148_000_000, | ||
| reason: 'The download was interrupted.', | ||
| reasonCode: 'download_interrupted', | ||
| createdAt: 1_700_000_000_100, | ||
| }, | ||
| ]); | ||
|
|
||
| await hydrateDownloadStore(); | ||
|
|
||
| expect(Object.keys(useDownloadStore.getState().downloads)).toHaveLength(1); | ||
| expect(useDownloadStore.getState().downloadIdIndex['dl-zombie-2']).toBe( | ||
| 'whisper-base:ggml-base.en.bin', | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -312,12 +312,40 @@ class DownloadManagerModule(reactContext: ReactApplicationContext) : | |
| @ReactMethod | ||
| fun startProgressPolling() { | ||
| scope.launch { | ||
| val active = withContext(Dispatchers.IO) { | ||
| downloadDao.getAllDownloads().first().filter { | ||
| it.status == DownloadStatus.QUEUED || it.status == DownloadStatus.RUNNING | ||
| try { | ||
| val active = withContext(Dispatchers.IO) { | ||
| downloadDao.getAllDownloads().first().filter { | ||
| it.status == DownloadStatus.QUEUED || it.status == DownloadStatus.RUNNING | ||
| } | ||
| } | ||
| for (download in active) { | ||
| if (workObservers.containsKey(download.id)) continue | ||
| val workInfos = withContext(Dispatchers.IO) { | ||
| workManager.getWorkInfosForUniqueWork(WorkerDownload.workName(download.id)).get() | ||
| } | ||
| if (DownloadPolling.isZombieWorkStates(workInfos.map { it.state })) { | ||
| withContext(Dispatchers.IO) { | ||
| downloadDao.updateStatus( | ||
| download.id, | ||
| DownloadStatus.FAILED, | ||
| DownloadReason.DOWNLOAD_INTERRUPTED, | ||
| ) | ||
| } | ||
| DownloadEventBridge.error( | ||
| download.id, | ||
| download.fileName, | ||
| download.modelId, | ||
| DownloadReason.messageFor(DownloadReason.DOWNLOAD_INTERRUPTED) | ||
| ?: "Download was interrupted.", | ||
| DownloadReason.DOWNLOAD_INTERRUPTED, | ||
| ) | ||
| } else { | ||
| registerObserver(download.id, download.fileName, download.modelId) | ||
| } | ||
| } | ||
| } catch (_: Exception) { | ||
| // DB unavailable — polling skipped, no crash | ||
| } | ||
|
Comment on lines
+315
to
348
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2. Polling aborts on error A single exception while processing one active row (e.g., during WorkManager work info lookup) exits the entire polling loop, so later active downloads won’t be zombie-checked or have observers registered in that cycle. The empty catch also makes these failures silent, reducing debuggability when polling stops working. Agent Prompt
|
||
| active.forEach { if (!workObservers.containsKey(it.id)) registerObserver(it.id, it.fileName, it.modelId) } | ||
| } | ||
| } | ||
|
|
||
|
|
||
14 changes: 14 additions & 0 deletions
14
android/app/src/main/java/ai/offgridmobile/download/DownloadPolling.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package ai.offgridmobile.download | ||
|
|
||
| import androidx.work.WorkInfo | ||
|
|
||
| /** | ||
| * Pure helpers for [DownloadManagerModule.startProgressPolling]. | ||
| * Extracted so zombie-detection rules are unit-testable without Room/WorkManager. | ||
| */ | ||
| internal object DownloadPolling { | ||
| fun isZombieWorkStates(states: List<WorkInfo.State>): Boolean = | ||
| states.isEmpty() || states.all { | ||
| it == WorkInfo.State.CANCELLED || it == WorkInfo.State.FAILED | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Cancellation swallowed
🐞 Bug☼ ReliabilityAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools