fix(sync): prevent master state from clearing when a peer pairs
CI / Build & Test (push) Successful in 4m10s
CI / Build & Test (push) Successful in 4m10s
When a device pairs with the master, the master's autopass can emit an update and applyRemoteUpdate would sometimes apply an empty or weaker snapshot from the merged view, overwriting the master's state. Skip applying remote state when we're the master and the incoming snapshot has less content (servers + vhosts + serviceTunnels) than current state.
This commit is contained in:
@@ -204,6 +204,13 @@ function onRemoteUpdate() {
|
||||
}, REMOTE_UPDATE_DEBOUNCE_MS);
|
||||
}
|
||||
|
||||
function snapshotContentSize(snapshot) {
|
||||
if (!snapshot || typeof snapshot !== 'object') return 0;
|
||||
return (Array.isArray(snapshot.servers) ? snapshot.servers.length : 0) +
|
||||
(Array.isArray(snapshot.virtualHosts) ? snapshot.virtualHosts.length : 0) +
|
||||
(Array.isArray(snapshot.serviceTunnels) ? snapshot.serviceTunnels.length : 0);
|
||||
}
|
||||
|
||||
async function applyRemoteUpdate() {
|
||||
if (weJustPushed || !pass || !holesailManager) return;
|
||||
try {
|
||||
@@ -219,13 +226,20 @@ async function applyRemoteUpdate() {
|
||||
return;
|
||||
}
|
||||
const statePath = getStateFilePath();
|
||||
let current = null;
|
||||
if (statePath && fs.existsSync(statePath)) {
|
||||
try {
|
||||
const currentRaw = fs.readFileSync(statePath, 'utf8');
|
||||
const current = JSON.parse(currentRaw);
|
||||
current = JSON.parse(currentRaw);
|
||||
if (stateFingerprint(current) === stateFingerprint(snapshot)) return;
|
||||
} catch (_) {}
|
||||
}
|
||||
const identity = loadIdentity();
|
||||
if (identity && identity.isMaster) {
|
||||
const currentSize = current ? snapshotContentSize(current) : snapshotContentSize(holesailManager.getStateSnapshot());
|
||||
const snapshotSize = snapshotContentSize(snapshot);
|
||||
if (snapshotSize < currentSize) return;
|
||||
}
|
||||
await applySyncedState(snapshot);
|
||||
lastSyncedAt = Date.now();
|
||||
if (emitEvent) emitEvent('syncApplied', {});
|
||||
|
||||
Reference in New Issue
Block a user