Skip to content

Commit

Permalink
Fixed tracker-resolver. Fixed some closeable leaks in storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Revertron committed Jan 17, 2023
1 parent 96433af commit 9db0a7c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
2 changes: 1 addition & 1 deletion apps/Android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {
minSdk 21
targetSdk 31
versionCode 15
versionName "1.8.0"
versionName "1.8.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
13 changes: 13 additions & 0 deletions apps/Android/app/src/main/java/com/revertron/mimir/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package com.revertron.mimir

import android.app.Application
import android.os.Handler
import android.os.StrictMode
import android.os.StrictMode.VmPolicy
import com.revertron.mimir.storage.SqlStorage


class App: Application() {

companion object {
Expand All @@ -14,6 +17,16 @@ class App: Application() {

override fun onCreate() {
super.onCreate()

if (BuildConfig.DEBUG) {
StrictMode.setVmPolicy(
VmPolicy.Builder()
.detectLeakedClosableObjects()
.penaltyLog()
.build()
)
}

storage = SqlStorage(this)
storage.cleanUp()
app = this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Resolver(private val storage: SqlStorage, private val tracker: InetSocketA

private val random = Random(System.currentTimeMillis())
private val nonces = HashMap<Int, Pair<ByteArray, ResolverReceiver>>()
private val timeouts = HashMap<Int, Thread>()
private val socket = DatagramSocket()

init {
Expand All @@ -43,6 +44,9 @@ class Resolver(private val storage: SqlStorage, private val tracker: InetSocketA
val nonce = dis.readInt()
val command = dis.readByte()
val pair = nonces.remove(nonce) ?: continue
synchronized(timeouts) {
timeouts.remove(nonce)?.interrupt()
}
when (command.toInt()) {
CMD_ANNOUNCE -> {
val ttl = dis.readLong()
Expand All @@ -51,7 +55,10 @@ class Resolver(private val storage: SqlStorage, private val tracker: InetSocketA
CMD_GET_IPS -> {
val count = dis.readByte()
Log.i(TAG, "Got $count ips")
if (count <= 0) continue
if (count <= 0) {
pair.second.onError(pair.first)
continue
}
val results = mutableListOf<Peer>()
val ipBuf = ByteArray(16)
val sigBuf = ByteArray(64)
Expand Down Expand Up @@ -106,7 +113,9 @@ class Resolver(private val storage: SqlStorage, private val tracker: InetSocketA
val packet = DatagramPacket(request, request.size, tracker)
try {
socket.send(packet)
startTimeoutThread(nonce, receiver)
} catch (e: IOException) {
Log.e(TAG, "Error sending packet: $e")
val pair = nonces.remove(nonce)!!
pair.second.onError(pubkey)
}
Expand Down Expand Up @@ -134,12 +143,40 @@ class Resolver(private val storage: SqlStorage, private val tracker: InetSocketA
val packet = DatagramPacket(request, request.size, tracker)
try {
socket.send(packet)
startTimeoutThread(nonce, receiver)
Log.i(TAG, "Announce packet sent")
} catch (e: IOException) {
Log.e(TAG, "Error sending packet: $e")
val pair = nonces.remove(nonce)!!
pair.second.onError(pubkey)
}
}

private fun startTimeoutThread(nonce: Int, receiver: ResolverReceiver): Thread {
val t = Thread {
//Log.d(TAG, "Timeout thread for $nonce started")
try {
Thread.sleep(10000)
synchronized(nonces) {
if (nonces.containsKey(nonce)) {
//Log.d(TAG, "Timeout thread for $nonce got timeout")
val pair = nonces.remove(nonce)!!
receiver.onError(pair.first)
}
}
} catch (e: InterruptedException) {
//Log.d(TAG, "Timeout thread for $nonce interrupted")
}
synchronized(timeouts) {
timeouts.remove(nonce)
}
}
synchronized(timeouts) {
timeouts.put(nonce, t)
}
t.start()
return t
}
}

interface ResolverReceiver {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ class SqlStorage(context: Context): SQLiteOpenHelper(context, DATABASE_NAME, nul
val cursor =
db.query("messages", arrayOf("delivered"), "contact = ? AND incoming = 0", arrayOf("$userId"), null, null, "id DESC", "1")
val result = if (cursor.moveToNext()) {
return cursor.getInt(0) > 0
cursor.getInt(0) > 0
} else {
null
}
Expand Down Expand Up @@ -447,6 +447,7 @@ class SqlStorage(context: Context): SQLiteOpenHelper(context, DATABASE_NAME, nul
} else {
Pair("guid", "id = ?")
}
var result: Message? = null
val columns = arrayOf("contact", pair.first, "replyTo", "incoming", "delivered", "read", "time", "edit", "type", "message")
val cursor = readableDatabase.query("messages", columns, pair.second, arrayOf("$messageId"), null, null, null, "1")
if (cursor.moveToNext()) {
Expand All @@ -461,26 +462,27 @@ class SqlStorage(context: Context): SQLiteOpenHelper(context, DATABASE_NAME, nul
val type = cursor.getInt(8)
val message = cursor.getBlobOrNull(9)
//Log.i(TAG, "$messageId: $guid")
cursor.close()
return if (byGuid) {
result = if (byGuid) {
Message(idOrGuid, contactId, messageId, replyTo, incoming, delivered, read, time, edit, type, message)
} else {
Message(messageId, contactId, idOrGuid, replyTo, incoming, delivered, read, time, edit, type, message)
}
}
cursor.close()
return null
return result
}

private fun getMessageIdByGuid(guid: Long): Long {
val db = this.readableDatabase
val statement = db.compileStatement("SELECT id FROM messages WHERE guid=? LIMIT 1")
statement.bindLong(1, guid)
return try {
val result = try {
statement.simpleQueryForLong()
} catch (e: SQLiteDoneException) {
-1
}
statement.close()
return result
}

fun getContactId(pubkey: ByteArray): Long {
Expand Down Expand Up @@ -587,6 +589,7 @@ class SqlStorage(context: Context): SQLiteOpenHelper(context, DATABASE_NAME, nul
Log.i(TAG, "Found account $name with pubkey ${Hex.toHexString(pubkey)}")
return AccountInfo(name, info, avatar, updated, clientId, AsymmetricCipherKeyPair(pub, priv))
}
cursor.close()
Log.w(TAG, "Didn't find account info $id, or it didn't change since ${Date(ifUpdatedSince * 1000)}")
return null
}
Expand Down

0 comments on commit 9db0a7c

Please sign in to comment.