Skip to content

Commit

Permalink
Fix tor access (#1591)
Browse files Browse the repository at this point in the history
* Support closure on TorClientHolder

+ Add TorClient accessMutex

* Reset after dispose

* Check nativeHandle

* Changelog update
  • Loading branch information
HonzaR authored Sep 16, 2024
1 parent 7398cbc commit e63df56
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ and this library adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [2.2.4] - 2024-09-12
## [2.2.4] - 2024-09-16

### Added
- `TransactionOverview.isShielding` has been added to indicate the shielding transaction type
Expand All @@ -18,6 +18,7 @@ and this library adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- Android 15 (SDK level 35) support added for 16 KB memory page size
- The disposing logic of `TorClient` (the USD/ZEC rate fetching component) has been improved

## [2.2.3] - 2024-09-09

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,31 @@ import cash.z.ecc.android.sdk.internal.ext.mkdirsSuspend
import cash.z.ecc.android.sdk.internal.jni.RustBackend
import dalvik.annotation.optimization.CriticalNative
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import java.io.File
import java.math.BigDecimal

class TorClient private constructor(
private val nativeHandle: Long?,
private var nativeHandle: Long?,
) {
private val accessMutex = Mutex()

suspend fun dispose() =
withContext(Dispatchers.IO) {
nativeHandle?.let { freeTorRuntime(it) }
accessMutex.withLock {
withContext(Dispatchers.IO) {
nativeHandle?.let { freeTorRuntime(it) }
nativeHandle = null
}
}

suspend fun getExchangeRateUsd(): BigDecimal =
withContext(Dispatchers.IO) {
getExchangeRateUsd(nativeHandle!!)
accessMutex.withLock {
withContext(Dispatchers.IO) {
checkNotNull(nativeHandle) { "TorClient is disposed" }
getExchangeRateUsd(nativeHandle!!)
}
}

companion object {
Expand All @@ -33,7 +43,7 @@ class TorClient private constructor(
error("${torDir.path} directory does not exist and could not be created.")
}

TorClient(createTorRuntime(torDir.path))
TorClient(nativeHandle = createTorRuntime(torDir.path))
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class UsdExchangeRateFetcher(torDir: File) {
val rate =
try {
Twig.info { "[USD] Fetch start" }
torHolder().getExchangeRateUsd()
torHolder { it.getExchangeRateUsd() }
} catch (e: Exception) {
Twig.error(e) { "[USD] Fetch failed" }
return FetchFiatCurrencyResult.Error(e)
Expand Down Expand Up @@ -69,16 +69,17 @@ private class TorClientHolder(private val torDir: File) {
private val mutex = Mutex()
private var torClient: TorClient? = null

suspend operator fun invoke(): TorClient =
suspend operator fun <T> invoke(block: suspend (TorClient) -> T): T =
mutex.withLock {
if (torClient == null) {
torClient = TorClient.new(torDir)
}
return torClient!!
return block(torClient!!)
}

suspend fun dispose() =
mutex.withLock {
torClient?.dispose()
torClient = null
}
}

0 comments on commit e63df56

Please sign in to comment.