fix: uninitialized exception for lateinit guardSnode in certain cases, replace with a nullable version for error handling (#926)

This commit is contained in:
Harris 2022-07-19 14:32:05 +10:00 committed by GitHub
parent d3e2ef0b40
commit 2ea7f638d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -308,10 +308,11 @@ object OnionRequestAPI {
*/
private fun sendOnionRequest(destination: Destination, payload: Map<*, *>): Promise<Map<*, *>, Exception> {
val deferred = deferred<Map<*, *>, Exception>()
lateinit var guardSnode: Snode
var guardSnode: Snode? = null
buildOnionForDestination(payload, destination).success { result ->
guardSnode = result.guardSnode
val url = "${guardSnode.address}:${guardSnode.port}/onion_req/v2"
val nonNullGuardSnode = result.guardSnode
val url = "${nonNullGuardSnode.address}:${nonNullGuardSnode.port}/onion_req/v2"
val finalEncryptionResult = result.finalEncryptionResult
val onion = finalEncryptionResult.ciphertext
if (destination is Destination.Server && onion.count().toDouble() > 0.75 * FileServerAPIV2.maxFileSize.toDouble()) {
@ -398,13 +399,17 @@ object OnionRequestAPI {
val promise = deferred.promise
promise.fail { exception ->
if (exception is HTTP.HTTPRequestFailedException && SnodeModule.isInitialized) {
val path = paths.firstOrNull { it.contains(guardSnode) }
val checkedGuardSnode = guardSnode
val path =
if (checkedGuardSnode == null) null
else paths.firstOrNull { it.contains(checkedGuardSnode) }
fun handleUnspecificError() {
if (path == null) { return }
var pathFailureCount = OnionRequestAPI.pathFailureCount[path] ?: 0
pathFailureCount += 1
if (pathFailureCount >= pathFailureThreshold) {
dropGuardSnode(guardSnode)
guardSnode?.let { dropGuardSnode(it) }
path.forEach { snode ->
@Suppress("ThrowableNotThrown")
SnodeAPI.handleSnodeError(exception.statusCode, exception.json, snode, null) // Intentionally don't throw