diff --git a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt index cf9850f52b..28463fe89b 100644 --- a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt +++ b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt @@ -173,8 +173,10 @@ class DefaultBehaviourContextWithFSM( fun Job.enableRemoveOnCompletion(state: T) { invokeOnCompletion { launchSafelyWithoutExceptions { - if (this@enableRemoveOnCompletion === statesJobs[state]) { - statesJobs.remove(state) + statesJobsMutex.withLock { + if (this@enableRemoveOnCompletion === statesJobs[state]) { + statesJobs.remove(state) + } } } }