Fix NetworkOnMainThreadException when starting a download - #1893
Merged
tapframe merged 1 commit intoSep 7, 2026
Merged
Conversation
DownloadsTransferJobService runs its coroutine scope on Dispatchers.Main.immediate. AndroidDownloadScheduler.execute() closed the per network OkHttpClient's connection pool synchronously in its finally block. That call closes pooled SSL sockets and crashes with NetworkOnMainThreadException whenever onNetworkChanged restarts the transfer. The eviction now runs on Dispatchers.IO.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes a crash on starting a download.
DownloadsTransferJobServicecloses the per networkOkHttpClientconnection pool synchronously onDispatchers.Main.immediate, which closes pooled SSL sockets on the main thread and throwsNetworkOnMainThreadException.PR type
Why
Starting a download crashes the app with
NetworkOnMainThreadExceptionwhenever the transfer restarts on a network change (a common case: Wi-Fi state changing while a download is running). This is a hard crash, the process is killed after repeated occurrences.Issue or approval
Fixes #1892
UI / behavior impact
Policy check
CONTRIBUTING.md.Scope boundaries
Only the socket eviction call is moved off the main thread. Nothing else in
execute()or the download flow is touched.Testing
./gradlew :composeApp:compileAndroidMainsucceeds../gradlew :composeApp:testAndroidHostTest --tests "com.nuvio.app.features.downloads.*"passes for the tests that exerciseAndroidDownloadScheduler.execute()(scheduledDownloadsArePersistedUserInitiatedTransfers,pausedTransferIsNotRestartedBySystemRedelivery,completedRenameIsRecoveredAfterProcessDeathBeforeStateCommit,backgroundExecutionDoesNotNeedRepositoryOrActivityCallbacks). One unrelated pre existing test in that class fails locally due to a Robolectric native runtime path issue in my environment, unrelated to this change.adb logcat, confirmed the fix removes the main thread socket close from theonNetworkChangedrestart path by code inspection.Screenshots / Video (UI changes only)
Not a UI change.
Breaking changes
None.
Linked issues
Fixes #1892