fix: address pr-bug-scan validated finding from #4243

Defer pairScanRootRef dispose to a microtask; if a sibling branch remounts (mountedRef back to true), skip dispose — blocks branch-swap teardown.
This commit is contained in:
orca-bug-scan-bot
2026-05-31 12:10:15 -07:00
parent 7a92f9b4b8
commit d401bf9d97
+15 -3
View File
@@ -51,10 +51,22 @@ export default function PairScanScreen() {
return
}
// Why: pairing attempts can outlive the visible route; dispose them when
// the scan screen detaches without a passive cleanup-only Effect.
// the scan screen detaches without a passive cleanup-only Effect. The
// dispose is deferred to a microtask because a permission-state flip
// unmounts one conditional branch and mounts another in the same
// commit — the unmounting branch's ref(null) fires before the new
// branch's ref(node), and we must not tear down an in-flight pairing
// attempt for what is really a sibling-branch swap inside the same
// mounted screen.
mountedRef.current = false
activePairingAttemptRef.current?.dispose()
activePairingAttemptRef.current = null
const attempt = activePairingAttemptRef.current
queueMicrotask(() => {
if (mountedRef.current) return
attempt?.dispose()
if (activePairingAttemptRef.current === attempt) {
activePairingAttemptRef.current = null
}
})
}, [])
const handleBarCodeScanned = useCallback(