Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a86fe5b7 authored by Matt Pietal's avatar Matt Pietal
Browse files

Protect callbacks from concurrent modification

Just synchronize around them

Fixes: 380528207
Test: Manual - race condition
Flag: EXEMPT bugfix
Change-Id: I106a6aeab627cac08c8ba59a713705e94f631393
parent 312a7a08
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -31,15 +31,17 @@ class DozeTransitionListener @Inject constructor() :
    override fun transitionTo(oldState: DozeMachine.State, newState: DozeMachine.State) {
        this.oldState = oldState
        this.newState = newState
        callbacks.forEach { it.onDozeTransition(oldState, newState) }

        val cbs = synchronized(this) { callbacks.toSet() }
        cbs.forEach { it.onDozeTransition(oldState, newState) }
    }

    override fun addCallback(callback: DozeTransitionCallback) {
        callbacks.add(callback)
        synchronized(this) { callbacks.add(callback) }
    }

    override fun removeCallback(callback: DozeTransitionCallback) {
        callbacks.remove(callback)
        synchronized(this) { callbacks.remove(callback) }
    }
}