fix(sdk,transport): bounded prekey requests and a real registration retry (4.13.2)
An unreachable prekey server (packets dropped, not refused) held createShade for ~2 min per call, and a failed registration was never retried despite the log saying so. Every request now has a deadline, registration retries with backoff until it lands, and registration: 'background' lets an app open its identity offline. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -143,6 +143,9 @@ export interface ShadeUploadOptions extends TransferOptions {
|
||||
generateThumbnail?: boolean | ThumbnailGenerationOptions;
|
||||
}
|
||||
|
||||
/** Delays between retries of the initial registration; the last repeats. */
|
||||
const REGISTRATION_RETRY_MS: readonly number[] = [5_000, 15_000, 30_000, 60_000];
|
||||
|
||||
/**
|
||||
* The high-level Shade API.
|
||||
*
|
||||
@@ -157,6 +160,9 @@ export class Shade {
|
||||
private manager!: ShadeSessionManager;
|
||||
private transport!: ShadeFetchTransport;
|
||||
private _background: BackgroundTasks | null = null;
|
||||
/** Pending retry of the initial registration; see [retryRegistration]. */
|
||||
private registrationRetry: ReturnType<typeof setTimeout> | null = null;
|
||||
private shutDown = false;
|
||||
private address!: string;
|
||||
private initialized = false;
|
||||
|
||||
@@ -279,28 +285,23 @@ export class Shade {
|
||||
};
|
||||
}
|
||||
|
||||
this.transport = new ShadeFetchTransport({
|
||||
baseUrl: this.config.prekeyServer,
|
||||
crypto: this.crypto,
|
||||
signingPrivateKey: identity.signingPrivateKey,
|
||||
...(ktOpts ? { keyTransparency: ktOpts } : {}),
|
||||
});
|
||||
this.transport = this.makeTransport(identity.signingPrivateKey, ktOpts);
|
||||
|
||||
// Step 5: Initial prekeys + register
|
||||
const otpks = await this.manager.generateOneTimePreKeys(20);
|
||||
const bundle = await this.manager.createPreKeyBundle();
|
||||
const publicIdentity = this.manager.getPublicIdentity();
|
||||
const register = () =>
|
||||
this.transport.register(this.address, publicIdentity, bundle.signedPreKey, otpks);
|
||||
|
||||
try {
|
||||
await this.transport.register(
|
||||
this.address,
|
||||
this.manager.getPublicIdentity(),
|
||||
bundle.signedPreKey,
|
||||
otpks,
|
||||
);
|
||||
} catch (err) {
|
||||
console.warn(
|
||||
`[Shade] Failed to register with prekey server at ${this.config.prekeyServer}: ${(err as Error).message}. Will retry on next replenish.`,
|
||||
);
|
||||
if (this.config.registration === 'background') {
|
||||
this.retryRegistration(register, 0, 0);
|
||||
} else {
|
||||
try {
|
||||
await register();
|
||||
} catch (err) {
|
||||
this.retryRegistration(register, 1, undefined, err as Error);
|
||||
}
|
||||
}
|
||||
|
||||
// Step 6: Background tasks
|
||||
@@ -742,6 +743,10 @@ export class Shade {
|
||||
async rotate(): Promise<void> {
|
||||
if (!this.initialized) throw new Error('Not initialized');
|
||||
|
||||
// The initial registration's retry would publish the identity we are
|
||||
// about to replace; this rotation registers the new one itself.
|
||||
this.cancelRegistrationRetry();
|
||||
|
||||
// Rotate locally first
|
||||
const newBundle = await this.manager.rotateIdentity();
|
||||
|
||||
@@ -749,11 +754,7 @@ export class Shade {
|
||||
// signed operations (replenish, delete, register) work
|
||||
const identity = await this.storage.getIdentityKeyPair();
|
||||
if (!identity) throw new Error('Identity missing after rotate');
|
||||
this.transport = new ShadeFetchTransport({
|
||||
baseUrl: this.config.prekeyServer,
|
||||
crypto: this.crypto,
|
||||
signingPrivateKey: identity.signingPrivateKey,
|
||||
});
|
||||
this.transport = this.makeTransport(identity.signingPrivateKey);
|
||||
|
||||
// Re-upload the new bundle
|
||||
await this.transport.register(
|
||||
@@ -822,17 +823,79 @@ export class Shade {
|
||||
: {}),
|
||||
});
|
||||
await this.manager.initialize();
|
||||
this.transport = new ShadeFetchTransport({
|
||||
baseUrl: this.config.prekeyServer,
|
||||
crypto: this.crypto,
|
||||
signingPrivateKey: restored.signingPrivateKey,
|
||||
});
|
||||
// Same reason as in rotate(): the pending retry is for the old identity.
|
||||
this.cancelRegistrationRetry();
|
||||
this.transport = this.makeTransport(restored.signingPrivateKey);
|
||||
}
|
||||
}
|
||||
|
||||
private makeTransport(
|
||||
signingPrivateKey: Uint8Array,
|
||||
keyTransparency?: KTVerifierOptions,
|
||||
): ShadeFetchTransport {
|
||||
return new ShadeFetchTransport({
|
||||
baseUrl: this.config.prekeyServer,
|
||||
crypto: this.crypto,
|
||||
signingPrivateKey,
|
||||
requestTimeoutMs: this.config.requestTimeoutMs,
|
||||
...(this.config.fetch ? { fetch: this.config.fetch } : {}),
|
||||
...(keyTransparency ? { keyTransparency } : {}),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register with the prekey server, retrying with backoff until it works,
|
||||
* the identity changes, or the instance shuts down.
|
||||
*
|
||||
* This used to be one attempt whose failure promised "will retry on next
|
||||
* replenish" — but replenish never registers, so an identity that came
|
||||
* up while the server was unreachable stayed unregistered until the next
|
||||
* restart.
|
||||
*
|
||||
* `attempt` 0 is the first try (no delay unless `delayMs` says so); each
|
||||
* failure moves one step along [REGISTRATION_RETRY_MS].
|
||||
*/
|
||||
private retryRegistration(
|
||||
register: () => Promise<void>,
|
||||
attempt: number,
|
||||
delayMs?: number,
|
||||
lastError?: Error,
|
||||
): void {
|
||||
if (this.shutDown) return;
|
||||
const steps = REGISTRATION_RETRY_MS;
|
||||
const delay = delayMs ?? steps[Math.min(Math.max(attempt - 1, 0), steps.length - 1)] ?? 60_000;
|
||||
if (lastError) {
|
||||
console.warn(
|
||||
`[Shade] Failed to register with prekey server at ${this.config.prekeyServer}: ${lastError.message}. Retrying in ${Math.round(delay / 1000)} s.`,
|
||||
);
|
||||
}
|
||||
this.registrationRetry = setTimeout(() => {
|
||||
this.registrationRetry = null;
|
||||
if (this.shutDown) return;
|
||||
register().then(
|
||||
() => {
|
||||
if (attempt > 0) {
|
||||
console.warn(`[Shade] Registered with prekey server at ${this.config.prekeyServer}.`);
|
||||
}
|
||||
},
|
||||
(err: Error) => this.retryRegistration(register, attempt + 1, undefined, err),
|
||||
);
|
||||
}, delay);
|
||||
(this.registrationRetry as unknown as { unref?: () => void }).unref?.();
|
||||
}
|
||||
|
||||
private cancelRegistrationRetry(): void {
|
||||
if (this.registrationRetry !== null) {
|
||||
clearTimeout(this.registrationRetry);
|
||||
this.registrationRetry = null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Clean shutdown: stop timers, close storage if it supports it */
|
||||
async shutdown(): Promise<void> {
|
||||
this._background?.stop();
|
||||
this.cancelRegistrationRetry();
|
||||
this.shutDown = true;
|
||||
if (this.transferEngine !== null) this.transferEngine.destroy();
|
||||
if (this.controlChannel !== null) this.controlChannel.destroy();
|
||||
if (this.webrtcRuntime !== null) {
|
||||
|
||||
Reference in New Issue
Block a user