refactor: use injected ioDispatcher and ApplicationCoroutineScope

Replaces two coroutine anti-patterns flagged by the architecture audit:

* FirmwareUpdateViewModel.onCleared() was using GlobalScope.launch to
  run cleanup work that must outlive the ViewModel scope. Introduce a
  new ApplicationCoroutineScope marker interface + Koin @Single binding
  in core/common, backed by SupervisorJob() + ioDispatcher, and inject
  it into the VM. NonCancellable is preserved so shutdown work still
  cannot be interrupted. Verified end-to-end by the KoinVerificationTest
  full-graph check.
* Six sites in feature/settings (android+jvm LogExporter/PrefExporter)
  and core/ui (android+jvm PlatformUtils) were hardcoding Dispatchers.IO.
  Switch to the project's injectable ioDispatcher property so tests
  can substitute a test dispatcher without reflection.

Dispatchers.Main sites in LogExporter are intentionally left alone —
they are fully test-swappable via Dispatchers.setMain() and there is
no existing mainDispatcher property to inject.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
James Rich 2026-04-17 10:46:48 -05:00
parent df3b5365f9
commit 092238cb55
12 changed files with 86 additions and 17 deletions

View file

@ -35,6 +35,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeoutOrNull
import org.jetbrains.compose.resources.StringResource
import org.koin.core.annotation.KoinViewModel
import org.meshtastic.core.common.di.ApplicationCoroutineScope
import org.meshtastic.core.common.util.CommonUri
import org.meshtastic.core.common.util.safeCatching
import org.meshtastic.core.database.entity.FirmwareRelease
@ -91,6 +92,7 @@ class FirmwareUpdateViewModel(
private val firmwareUpdateManager: FirmwareUpdateManager,
private val usbManager: FirmwareUsbManager,
private val fileHandler: FirmwareFileHandler,
private val applicationScope: ApplicationCoroutineScope,
) : ViewModel() {
private val _state = MutableStateFlow<FirmwareUpdateState>(FirmwareUpdateState.Idle)
@ -124,12 +126,10 @@ class FirmwareUpdateViewModel(
override fun onCleared() {
super.onCleared()
// viewModelScope is already cancelled when onCleared() runs, so launch cleanup in a
// standalone scope. SupervisorJob prevents the coroutine from propagating failures to a
// shared parent, and NonCancellable on the launch keeps cleanup running even if the scope
// is cancelled concurrently.
@OptIn(kotlinx.coroutines.DelicateCoroutinesApi::class)
kotlinx.coroutines.GlobalScope.launch(NonCancellable) {
// viewModelScope is already cancelled when onCleared() runs, so launch cleanup on the
// application-wide scope (SupervisorJob + ioDispatcher). NonCancellable keeps cleanup
// running even if something tries to cancel it mid-flight.
applicationScope.launch(NonCancellable) {
tempFirmwareFile = cleanupTemporaryFiles(fileHandler, tempFirmwareFile)
}
}

View file

@ -108,6 +108,7 @@ class FirmwareUpdateIntegrationTest {
firmwareUpdateManager,
usbManager,
fileHandler,
TestApplicationCoroutineScope(testDispatcher),
)
@Test

View file

@ -124,6 +124,7 @@ class FirmwareUpdateViewModelTest {
firmwareUpdateManager,
usbManager,
fileHandler,
TestApplicationCoroutineScope(testDispatcher),
)
@Test

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.meshtastic.feature.firmware
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import org.meshtastic.core.common.di.ApplicationCoroutineScope
internal class TestApplicationCoroutineScope(dispatcher: CoroutineDispatcher) :
ApplicationCoroutineScope,
CoroutineScope by CoroutineScope(SupervisorJob() + dispatcher)

View file

@ -116,6 +116,7 @@ class FirmwareUpdateViewModelFileTest {
firmwareUpdateManager,
usbManager,
fileHandler,
TestApplicationCoroutineScope(testDispatcher),
)
// -----------------------------------------------------------------------