Skip to content

Commit

Permalink
Fix race when stopping Tor
Browse files Browse the repository at this point in the history
  • Loading branch information
grote committed Jun 28, 2023
1 parent 3dd9606 commit 845de5c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
3 changes: 1 addition & 2 deletions app/src/main/java/org/onionshare/android/ShareManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ class ShareManager @Inject constructor(
}
startSharingJob = null

val torState = torManager.state.value
if (torState !is TorState.Stopped && torState !is TorState.Stopping) torManager.stop()
torManager.stop()
if (webserverManager.state.value !is WebServerState.Stopped) webserverManager.stop()
fileManager.stop()
notificationManager.onStopped()
Expand Down
22 changes: 14 additions & 8 deletions app/src/main/java/org/onionshare/android/tor/TorManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ class TorManager @Inject constructor(
/**
* Updates the [_state] with the given new [state] preventing concurrent modifications.
* The state only gets updated when [_state] was in [expectedState].
* @return true if the state was updated and false if not.
*
* Note that the underlying [MutableStateFlow] may reject updates that are equal to the previous state.
*
* @return true if the expected state was either null or matched the previous state.
*/
@Synchronized
private fun updateTorState(expectedState: KClass<*>?, newState: TorState, warn: Boolean = true): Boolean {
Expand Down Expand Up @@ -104,13 +107,16 @@ class TorManager @Inject constructor(
}

fun stop() {
LOG.info("Stopping...")
updateTorState(null, TorState.Stopping)
startCheckJob?.cancel()
startCheckJob = null
tor.stop()
Intent(app, ShareService::class.java).also { intent ->
app.stopService(intent)
if (updateTorState(TorState.Stopping::class, TorState.Stopping, warn = false)) {
LOG.info("Was already stopping. Not stopping again.")
} else {
LOG.info("Stopping...")
startCheckJob?.cancel()
startCheckJob = null
tor.stop()
Intent(app, ShareService::class.java).also { intent ->
app.stopService(intent)
}
}
}

Expand Down

0 comments on commit 845de5c

Please sign in to comment.