Skip to content

Commit

Permalink
Avoid ConcurrentModificationException in periodic dropped peers handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ovitrif committed Sep 6, 2024
1 parent 885ed8a commit 361a9ec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ dependencies {
### Android
- Open `lib/android` in Android Studio
- To enable documentation for the LDK code, follow this guide: [How to attach JavaDoc to the library in Android Studio](https://medium.com/@mydogtom/tip-how-to-attach-javadoc-to-the-library-in-android-studio-5ff43c4303b3), ie.:
1. Switch to `Project` view in the Project browser tool windo
1. Switch to `Project` view in the Project browser tool window
2. Expand `External Libraries`
3. Right click on `Gradle: ./libs/LDK-release.aar` then `Library Properties…`
4. Tap the ➕ button and select the `./lib/android/libs/ldk-java-javadoc.jar` file
Expand Down
27 changes: 13 additions & 14 deletions lib/android/src/main/java/com/reactnativeldk/LdkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import org.ldk.enums.Currency
import org.ldk.enums.Network
import org.ldk.enums.Recipient
import org.ldk.enums.RetryableSendFailure
import org.ldk.impl.bindings.LDKPaymentSendFailure.DuplicatePayment
import org.ldk.impl.bindings.get_ldk_c_bindings_version
import org.ldk.impl.bindings.get_ldk_version
import org.ldk.structs.*
Expand All @@ -29,6 +28,10 @@ import java.nio.file.Files
import java.nio.file.Paths
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.ScheduledFuture
import java.util.concurrent.ScheduledThreadPoolExecutor
import java.util.concurrent.TimeUnit


//MARK: ************Replicate in typescript and swift************
Expand Down Expand Up @@ -178,9 +181,9 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
private var currentBlockchainHeight: Double? = null

//List of peers that "should" remain connected. Stores address: String, port: Double, pubKey: String
private var addedPeers: MutableList<HashMap<String, Any>> = mutableListOf()
private var currentlyConnectingPeers: MutableList<String> = mutableListOf()
private var timerTaskScheduled: Boolean = false
private var addedPeers = ConcurrentLinkedQueue<Map<String, Any>>()
private var currentlyConnectingPeers = ConcurrentLinkedQueue<String>()
private var periodicDroppedPeersHandler: ScheduledFuture<*>? = null

//Static to be accessed from other classes
companion object {
Expand Down Expand Up @@ -508,14 +511,10 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod

peerHandler = channelManagerConstructor!!.nio_peer_handler

//Start watching for dropped peers every 1 second
if (!timerTaskScheduled) {
Timer().schedule(object : TimerTask() {
override fun run() {
handleDroppedPeers()
}
}, 1000, 3000)
timerTaskScheduled = true
//after 1s, Start watching for dropped peers every 3 seconds
if (periodicDroppedPeersHandler == null) {
periodicDroppedPeersHandler = ScheduledThreadPoolExecutor(1)
.scheduleWithFixedDelay(::handleDroppedPeers,1, 3, TimeUnit.SECONDS)
}

//Cached for restarts
Expand Down Expand Up @@ -617,7 +616,7 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
handleResolve(promise, LdkCallbackResponses.chain_sync_success)
}

fun handleDroppedPeers() {
private fun handleDroppedPeers() {
peerHandler ?: return LdkEventEmitter.send(EventTypes.native_log, "Handling dropped peers error. Peer handler not initialized.")

LdkEventEmitter.send(EventTypes.native_log, "Checking for dropped peers")
Expand Down Expand Up @@ -675,7 +674,7 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
currentlyConnectingPeers.remove(pubKey)

//Should retry if success or fail
if (!addedPeers.map { it["pubKey"] as String }.contains(pubKey)) {
if (addedPeers.none { it["pubKey"] as String == pubKey }) {
addedPeers.add(hashMapOf(
"address" to address,
"port" to port,
Expand Down

0 comments on commit 361a9ec

Please sign in to comment.