fix(sync): detect provider changes before storage events arrive

This commit is contained in:
bincxz committed 2026-09-27 16:29:42 +08:00
1 parent dee468dcfa
commit fd177e8453
2 files changed
+26 -7

No files matched your search

@@ -600,6 +600,7 @@ export async function syncAllProvidersImpl(this: any,
const uploadTasks = validUploads.map(async ({ provider, adapter }) => {
try {
const originalProviderWriteSeq = providerWriteSeqAtStart.get(provider);
let storedProviderFingerprint: string | null = null;
const assertProviderConnectionUnchanged = () => {
if (
originalProviderWriteSeq != null
@@ -609,6 +610,17 @@ export async function syncAllProvidersImpl(this: any,
'Provider connection changed during sync; retry with its latest credentials',
);
}
// Storage events arrive asynchronously in this window. Re-read the
// persisted connection so a peer-window write is visible even before
// its event has advanced providerWriteSeq here.
if (
storedProviderFingerprint !== null
&& JSON.stringify(this.loadProviderConnection(provider)) !== storedProviderFingerprint
) {
throw new ProviderConnectionChangedDuringSyncError(
'Provider connection changed during sync; retry with its latest credentials',
);
}
};
const entry = checkResults.find((result) => result.provider === provider);
const checkedRemoteFile = entry?.check?.remoteFile;
@@ -654,9 +666,9 @@ export async function syncAllProvidersImpl(this: any,
// this sync starts, so the write sequence alone can look stable
// while in-memory credentials still lag the stored connection.
if (typeof this.loadProviderConnection === 'function') {
const storedConnection = await decryptProviderSecrets(
this.loadProviderConnection(provider),
);
const rawStoredConnection = this.loadProviderConnection(provider);
storedProviderFingerprint = JSON.stringify(rawStoredConnection);
const storedConnection = await decryptProviderSecrets(rawStoredConnection);
assertSyncSecurityGeneration(this, syncSecurityGeneration);
assertProviderConnectionUnchanged();
const identityFields = (connection: ProviderConnection) => JSON.stringify({