App unsubscribe not working

I'm trying to build my first app for controlling multiple linked switches with one master switch .

The problem that i'm having is that when i unsubscribe event handlers they still appear to be firing and cause a feedback.

Here are the blocks of code that handle master switch changes and linked switch changes:

def masterSwitchOnHandler(evt) {
	log "Master on"
	if (state.switchStates.size() == 0) {
		log "No remembered switch states"
	} else {
		// restore previously on switches
		bypassLinkedSwitchesSubscription {
			switches.each { s -> 
				def switchState = state.switchStates[s.deviceId.toString()]
				if (switchState == "on") {
					log "Restore ${s.displayName} to on"
					s.on()
				}
			}
		}
	}
}

def linkedSwitchOnHandler(evt) {
	log "Linked switch ${evt.displayName} is on"

	state.switchStates[evt.deviceId.toString()] = "on"

	// if any of the linked switches are on the master switch should be on

	bypassMasterSwitchSubscription {
		masterSwitch.on()
	}

	log "Remembering linked switches state: ${state.switchStates}"
}

def bypassMasterSwitchSubscription(Closure callback) {
	// unsubscribe to avoid feedback
	log "Unsubscribe master"
	unsubscribe(masterSwitch)

	callback();

	log "Restore subscription master"
	// restore subscription
	subscribe(masterSwitch, "switch.on", masterSwitchOnHandler)
	subscribe(masterSwitch, "switch.off", masterSwitchOffHandler)
}

def bypassLinkedSwitchesSubscription(Closure callback) {
	// unsubscribe to avoid feedback
	log "Unsubscribe linked"
	unsubscribe(switches)
	
	callback();

	log "Restore subscription linked"
	// restore subscription
	subscribe(switches, "switch.on", linkedSwitchOnHandler)
	subscribe(switches, "switch.off", linkedSwitchOffHandler)
}

Here are the logs:

app:2632019-10-08 11:17:02.096 pm debugRemembering linked switches state: [265:on, 266:on]
app:2632019-10-08 11:17:02.065 pm debugRestore subscription master
app:2632019-10-08 11:17:02.018 pm debugRemembering linked switches state: [265:on, 266:on]
app:2632019-10-08 11:17:01.995 pm debugRestore subscription master
app:2632019-10-08 11:17:01.970 pm debugUnsubscribe master
app:2632019-10-08 11:17:01.957 pm debugLinked switch Entrance Light is on
app:2632019-10-08 11:17:01.852 pm debugUnsubscribe master
app:2632019-10-08 11:17:01.824 pm debugLinked switch Hallway Light is on
app:2632019-10-08 11:17:01.539 pm debugRestore subscription linked
app:2632019-10-08 11:17:01.503 pm debugRestore Entrance Light to on
app:2632019-10-08 11:17:01.458 pm debugRestore Hallway Light to on
app:2632019-10-08 11:17:01.435 pm debugUnsubscribe linked
app:2632019-10-08 11:17:01.427 pm debugMaster on

Am I not calling unsubscribe correctly?

Note: i have tried unsubscribe(switches, "switch.on", linkedSwitchOnHandler) and unsubscribe(switches, "switch.on", "linkedSwitchOnHandler") - same result.

Is this documentation not correct? App Object - Hubitat Documentation

I think actually works:
unsubscribe(switches, "switch.on", "linkedSwitchOnHandler")

The problem was that the events were firing after I have already resubscribed. So I used runInMillis to resubscribe and my app works as expected now.