Skip to content

Commit

Permalink
[PM-15444] Increase WASM timeout to 10s (#12158)
Browse files Browse the repository at this point in the history
* Increase WASM timeout to 10s

* Change time to 3s, add logService with debug log
  • Loading branch information
Hinton authored Nov 28, 2024
1 parent d4395d8 commit 5968634
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion apps/browser/src/background/main.background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ export default class MainBackground {
);

const sdkClientFactory = flagEnabled("sdk")
? new BrowserSdkClientFactory()
? new BrowserSdkClientFactory(this.logService)
: new NoopSdkClientFactory();
this.sdkService = new DefaultSdkService(
sdkClientFactory,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { SdkClientFactory } from "@bitwarden/common/platform/abstractions/sdk/sdk-client-factory";
import { RecoverableSDKError } from "@bitwarden/common/platform/services/sdk/default-sdk.service";
import type { BitwardenClient } from "@bitwarden/sdk-internal";

import { BrowserApi } from "../../browser/browser-api";
Expand Down Expand Up @@ -62,24 +64,39 @@ async function load() {
* Works both in popup and service worker.
*/
export class BrowserSdkClientFactory implements SdkClientFactory {
constructor(private logService: LogService) {}

async createSdkClient(
...args: ConstructorParameters<typeof BitwardenClient>
): Promise<BitwardenClient> {
const startTime = performance.now();
try {
await loadWithTimeout();
} catch (error) {
throw new Error(`Failed to load: ${error.message}`);
}

return Promise.resolve((globalThis as any).init_sdk(...args));
const endTime = performance.now();
const elapsed = Math.round((endTime - startTime) / 1000);

const instance = (globalThis as any).init_sdk(...args);

this.logService.info("WASM SDK loaded in", Math.round(endTime - startTime), "ms");

// If it takes 3 seconds or more to load, we want to capture it.
if (elapsed >= 3) {
throw new RecoverableSDKError(instance, elapsed);
}

return instance;
}
}

const loadWithTimeout = async () => {
return new Promise<void>((resolve, reject) => {
const timer = setTimeout(() => {
reject(new Error("Operation timed out after 1 second"));
}, 1000);
reject(new Error("Operation timed out after 10 second"));
}, 10000);

load()
.then(() => {
Expand Down
5 changes: 3 additions & 2 deletions apps/browser/src/popup/services/services.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,9 @@ const safeProviders: SafeProvider[] = [
}),
safeProvider({
provide: SdkClientFactory,
useClass: flagEnabled("sdk") ? BrowserSdkClientFactory : NoopSdkClientFactory,
deps: [],
useFactory: (logService) =>
flagEnabled("sdk") ? new BrowserSdkClientFactory(logService) : new NoopSdkClientFactory(),
deps: [LogService],
}),
safeProvider({
provide: LoginEmailService,
Expand Down
22 changes: 21 additions & 1 deletion libs/common/src/platform/services/sdk/default-sdk.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,33 @@ import { SdkService } from "../../abstractions/sdk/sdk.service";
import { compareValues } from "../../misc/compare-values";
import { EncryptedString } from "../../models/domain/enc-string";

export class RecoverableSDKError extends Error {
sdk: BitwardenClient;
timeout: number;

constructor(sdk: BitwardenClient, timeout: number) {
super(`SDK took ${timeout}s to initialize`);

this.sdk = sdk;
this.timeout = timeout;
}
}

export class DefaultSdkService implements SdkService {
private sdkClientCache = new Map<UserId, Observable<BitwardenClient>>();

client$ = this.environmentService.environment$.pipe(
concatMap(async (env) => {
const settings = this.toSettings(env);
return await this.sdkClientFactory.createSdkClient(settings, LogLevel.Info);
try {
return await this.sdkClientFactory.createSdkClient(settings, LogLevel.Info);
} catch (e) {
if (e instanceof RecoverableSDKError) {
await this.failedToInitialize("sdk", e);
return e.sdk;
}
throw e;
}
}),
shareReplay({ refCount: true, bufferSize: 1 }),
);
Expand Down

0 comments on commit 5968634

Please sign in to comment.