Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 99 additions & 21 deletions app/src/main/java/to/bitkit/repositories/LightningRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class LightningRepo @Inject constructor(
* `start()`, so without this a detached recovery can restart the previous config between a
* request's stop and start, leaving the request to report and persist a config that never
* started. Recovery skips the release gate, but while the stopped node's VSS writes keep the
* store busy it can take up to [RECOVERY_START_ATTEMPTS] builds of up to 60 s each.
* store busy it can take up to [BUSY_STORE_START_ATTEMPTS] builds of up to 60 s each.
*/
private val configChangeMutex = Mutex()
private val isChangingAddressType = AtomicBoolean(false)
Expand Down Expand Up @@ -411,6 +411,7 @@ class LightningRepo @Inject constructor(

if (shouldCancelPendingStop) cancelPendingStop()
val stopRequestsAtStart = stopRequestsSeen ?: stopRequestCount.get()
val isConfigChange = customServerUrl != null || customRgsServerUrl != null

// Spark needs no node, so it starts alongside it and stays up while the node stops in background.
if (!isWiping) scope.launch { sparkRepo.start(walletIndex) }
Expand Down Expand Up @@ -438,6 +439,12 @@ class LightningRepo @Inject constructor(
)
}

// A changed config starts only a node it built itself, never a handle another start left behind.
if (isConfigChange && lightningService.node != null) {
releaseUnsavedConfigNode()
if (lightningService.node != null) return@withLock Result.failure(NodeConfigNotAppliedError())
}

runCatching {
_lightningState.update { it.copy(nodeLifecycleState = NodeLifecycleState.Starting) }

Expand All @@ -457,6 +464,17 @@ class LightningRepo @Inject constructor(
}
}

// A stop requested during a server change's or recovery's build wins. A foreground start can cancel
// that stop before it runs, so release the built node here, or that start would reuse it. An ordinary
// start, which cancels pending stops, is not held back by one.
val yieldsToStop = isConfigChange || !shouldCancelPendingStop
if (yieldsToStop && stopRequestsSeen != null && stopRequestCount.get() != stopRequestsSeen) {
Logger.info("Skipped start because a stop was requested during setup", context = TAG)
releaseUnsavedConfigNode()
_lightningState.update { it.copy(nodeLifecycleState = NodeLifecycleState.Stopped) }
return@withLock Result.failure(NodeStartYieldedToStopError(null))
Comment thread
ovitrif marked this conversation as resolved.
Resolved
}

if (lightningService.status?.isRunning == true) {
Logger.info("LDK node already running", context = TAG)
runSuspendCatching { lightningService.reconcileWatchOnlyAccounts() }
Expand Down Expand Up @@ -499,7 +517,15 @@ class LightningRepo @Inject constructor(
Result.success(Unit)
}.getOrElse { e ->
if (e is CancellationException) {
withContext(NonCancellable) { reconcileCancelledStart(initialLifecycleState) }
withContext(NonCancellable) {
if (isConfigChange) {
// The change was never saved, so neither keep nor adopt a node built with its server.
stopLocked()
Logger.info("Released the node of a cancelled config change", context = TAG)
} else {
reconcileCancelledStart(initialLifecycleState)
}
}
throw e
}

Expand All @@ -520,6 +546,9 @@ class LightningRepo @Inject constructor(
shouldRetryStart = true
Result.failure(e)
} else {
// A node built with a changed server must not outlive its failed start: the next start would
// reuse it although the change was never saved.
if (isConfigChange) releaseUnsavedConfigNode()
_lightningState.update { it.copy(nodeLifecycleState = NodeLifecycleState.ErrorStarting(e)) }
Result.failure(e)
}
Expand Down Expand Up @@ -573,6 +602,11 @@ class LightningRepo @Inject constructor(
}
}

private suspend fun releaseUnsavedConfigNode() {
runSuspendCatching { lightningService.stop() }
.onFailure { Logger.warn("Failed to release node built with an unsaved config", it, context = TAG) }
}

private suspend fun reconcileCancelledStart(initialLifecycleState: NodeLifecycleState) {
if (lightningService.status?.isRunning == true) {
if (_lightningState.value.nodeLifecycleState.isRunning()) {
Expand Down Expand Up @@ -1021,23 +1055,23 @@ class LightningRepo @Inject constructor(
configChangeMutex.withLock {
waitForNodeToStop().onFailure { return@withContext Result.failure(it) }

recoverOnCancellation {
var stopRequests: Long? = null
recoverOnCancellation(stopRequestsSeen = { stopRequests }) {
stop().onFailure {
Logger.error("Failed to stop node during electrum server change", it, context = TAG)
return@withContext Result.failure(it)
}
// A stop requested from here on, during the change's first build included, wins over every start.
stopRequests = stopRequestCount.get()

Logger.debug("Starting node with new electrum server: '$newServerUrl'", context = TAG)

start(
shouldRetry = false,
customServerUrl = newServerUrl,
)
startConfigChange(stopRequests = stopRequests, customServerUrl = newServerUrl)
}.onFailure {
Comment thread
ovitrif marked this conversation as resolved.
Resolved
// Recover in the background: a wedged node's release can gate the rebuild for tens of
// seconds, and the caller must surface this failure now rather than block on recovery.
Logger.warn("Failed ldk-node config change, recovering in background…", context = TAG)
scope.launch { restartWithPreviousConfig() }
scope.launch { restartWithPreviousConfig(stopRequests) }
}.onSuccess {
runSuspendCatching { settingsStore.update { it.copy(electrumServer = newServerUrl) } }
.onFailure {
Expand All @@ -1061,23 +1095,23 @@ class LightningRepo @Inject constructor(
configChangeMutex.withLock {
waitForNodeToStop().onFailure { return@withContext Result.failure(it) }

recoverOnCancellation {
var stopRequests: Long? = null
recoverOnCancellation(stopRequestsSeen = { stopRequests }) {
stop().onFailure {
Logger.error("Failed to stop node during RGS server change", it, context = TAG)
return@withContext Result.failure(it)
}
// A stop requested from here on, during the change's first build included, wins over every start.
stopRequests = stopRequestCount.get()

Logger.debug("Starting node with new RGS server: '$newRgsUrl'", context = TAG)

start(
shouldRetry = false,
customRgsServerUrl = newRgsUrl,
)
startConfigChange(stopRequests = stopRequests, customRgsServerUrl = newRgsUrl)
}.onFailure {
// Recover in the background: a wedged node's release can gate the rebuild for tens of
// seconds, and the caller must surface this failure now rather than block on recovery.
Logger.warn("Failed ldk-node config change, recovering in background…", context = TAG)
scope.launch { restartWithPreviousConfig() }
scope.launch { restartWithPreviousConfig(stopRequests) }
}.onSuccess {
settingsStore.update { it.copy(rgsServerUrl = newRgsUrl) }

Expand All @@ -1086,16 +1120,53 @@ class LightningRepo @Inject constructor(
}
}

private inline fun <T> recoverOnCancellation(block: () -> T): T {
/**
* Starts the node with a changed server, retrying while the build fails because the stopped node's VSS
* writes keep the store busy, which has nothing to do with the new server. A stop requested meanwhile wins.
*/
private suspend fun startConfigChange(
stopRequests: Long?,
customServerUrl: String? = null,
customRgsServerUrl: String? = null,
): Result<Unit> {
var result = startNode(
shouldRetry = false,
customServerUrl = customServerUrl,
customRgsServerUrl = customRgsServerUrl,
stopRequestsSeen = stopRequests,
)
var attempt = 1
// The stopped node's VSS writes can keep retrying for minutes, past one build's wait for them.
while (result.exceptionOrNull().isKvStoreSetupFailure() && attempt < BUSY_STORE_START_ATTEMPTS) {
if (pendingStopJob.get() != null) {
Logger.info("Skipped server change retry because a stop was requested", context = TAG)
return result
}
attempt++
Logger.warn("Retrying server change start '$attempt/$BUSY_STORE_START_ATTEMPTS'", context = TAG)
result = startNode(
Comment thread
ovitrif marked this conversation as resolved.
Resolved
Comment thread
ovitrif marked this conversation as resolved.
Resolved
shouldRetry = false,
customServerUrl = customServerUrl,
customRgsServerUrl = customRgsServerUrl,
shouldCancelPendingStop = false,
stopRequestsSeen = stopRequests,
)
}
return result
}

private inline fun <T> recoverOnCancellation(stopRequestsSeen: () -> Long?, block: () -> T): T {
try {
return block()
} catch (e: CancellationException) {
// The caller is route-scoped: leaving the screen cancels the change mid-rebuild. stop()
// and start() rethrow the cancellation instead of reporting a failure, so the onFailure
// recovery never runs and the node would stay down until the next ON_START. Hand the
// recovery to the repo scope, which outlives the caller, then preserve the cancellation.
// A stop requested since the change's own stop still wins over that recovery.
Logger.warn("Cancelled ldk-node config change, recovering in background…", context = TAG)
scope.launch { restartWithPreviousConfig() }
val stopRequests = stopRequestsSeen()
scope.launch { restartWithPreviousConfig(stopRequests) }
throw e
}
}
Expand Down Expand Up @@ -1303,10 +1374,17 @@ class LightningRepo @Inject constructor(
return remaining.isEmpty()
}

private suspend fun restartWithPreviousConfig(): Result<Unit> = withContext(bgDispatcher) {
/** [stopRequestsSeen] is the failed change's reading: a stop requested since then wins over recovery. */
private suspend fun restartWithPreviousConfig(
stopRequestsSeen: Long? = null,
): Result<Unit> = withContext(bgDispatcher) {
// Runs detached from the failed change, so it takes the same transaction lock: without it
// this recovery can restart the previous config between a later change's stop and start.
configChangeMutex.withLock {
if (stopRequestsSeen != null && stopRequestCount.get() != stopRequestsSeen) {
Logger.info("Skipped recovery because a stop was requested", context = TAG)
return@withContext Result.failure(NodeStartYieldedToStopError(null))
}
Logger.debug("Stopping node for recovery attempt", context = TAG)

stop().onFailure { e ->
Expand All @@ -1328,15 +1406,15 @@ class LightningRepo @Inject constructor(
}
attempt++
if (attempt > 1) {
Logger.warn("Retrying recovery start '$attempt/$RECOVERY_START_ATTEMPTS'", context = TAG)
Logger.warn("Retrying recovery start '$attempt/$BUSY_STORE_START_ATTEMPTS'", context = TAG)
}
// A direct stop since recovery's own stop wins over every attempt.
result = startNode(
shouldRetry = false,
shouldCancelPendingStop = false,
stopRequestsSeen = stopRequests,
)
} while (result.exceptionOrNull().isKvStoreSetupFailure() && attempt < RECOVERY_START_ATTEMPTS)
} while (result.exceptionOrNull().isKvStoreSetupFailure() && attempt < BUSY_STORE_START_ATTEMPTS)
result.onSuccess {
Logger.debug("Successfully started node with previous config", context = TAG)
}.onFailure {
Expand Down Expand Up @@ -3445,8 +3523,8 @@ class LightningRepo @Inject constructor(
private const val SYNC_RETRY_DELAY_MS = 15_000L
private const val MSATS_PER_SAT = 1_000uL

/** Recovery starts while a stopped node's VSS writes keep the store busy; each build waits up to 60 s. */
private const val RECOVERY_START_ATTEMPTS = 5
/** Starts while a stopped node's VSS writes keep the store busy; each build waits up to 60 s. */
private const val BUSY_STORE_START_ATTEMPTS = 5

/** LDK's default routing fee limit is 1% of the amount, this divisor, plus [DEFAULT_ROUTING_FEE_BASE_MSAT]. */
private const val DEFAULT_ROUTING_FEE_DIVISOR = 100uL
Expand Down
Loading
Loading