Repository navigation
[FSSDK-13442] Settle the close promise after client init failure - #1189
Conversation
When the client failed to start, its state was set to Failed but only the startPromise was rejected. stop() is a no-op for a Failed service, so close() / onTerminated() never settled. Now, on start failure, the client rejects the stopPromise with the init error and stops the project config manager, event processor and ODP manager. If stop() was called while starting (which makes the child services reject onRunning()), stop() still settles the stopPromise, so close() resolves as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e starting Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
The error handler can call close() before the initialization failure is recorded, causing onRunning() and onReady() to receive cancellation errors instead of the original failure.
1 open issue
What changed in this PR
This PR addresses client shutdown hanging after initialization fails by settling the shutdown promise and stopping child services.
Changes:
- Rejects
close()andonTerminated()with the initialization error. - Shares child-service cleanup between normal shutdown and startup failure.
- Adds regression tests for failure handling and closing during startup.
| File | Description |
|---|---|
lib/optimizely/index.ts |
Adds startup-failure handling and shared child-service cleanup. |
lib/optimizely/index.spec.ts |
Tests error propagation, child-service shutdown, and closing during startup. |
Review effort: Balanced
Md Junaed Hossain (junaed-optimizely)
left a comment
There was a problem hiding this comment.
Looks good. I have 2 concerns. one is a side effect of stopping the child services on failure, the other is a pre-existing leak in the scenario this PR targets.
| this.projectConfigManager.onRunning(), | ||
| this.eventProcessor ? this.eventProcessor.onRunning() : Promise.resolve(), | ||
| this.odpManager ? this.odpManager.onRunning() : Promise.resolve(), | ||
| this.vuidManager ? this.vuidManager.initialize() : Promise.resolve(), |
There was a problem hiding this comment.
a VUID init failure now stops the event processor and config manager. initialize() rejects whenever localStorage/AsyncStorage throws. LocalStorageCache and VuidCacheManager.load() don't catch anything.
In that case the config is already loaded, and decisions don't check client state. So activate()/decide() keep returning variations, but the event processor is terminated and no impressions go out. On master, events were still dispatched btw.
VUID being fatal seems to have come along from the old readyPromise. Should it be non-fatal? If you'd rather keep it fatal, that's fine.
There was a problem hiding this comment.
Yeah, VUID init failure being fatal is expected. The fact that decide() works is not in the contract. The expectation is if onReady() rejects, the instance should not be used.
as VUID cannot work without local storage, the user should check whether it's available before initializing the vuid manager. We might handle that internally with a in memory vuid cache automatically in future. But that defeats the purpose of VUID, it would only be there to handle this exceptional case
There was a problem hiding this comment.
The PR description says the onReady cleanup tasks still run only from stop(). But stop() does nothing once the client has Failed, so they never run here. The timer is only cleared on success. After a failed start plus an awaited close(), one timer is still pending: up to 30s with the default timeout, which keeps a Node process alive.
Running the cleanup tasks in the failure handler would make onReady() reject with INSTANCE_CLOSED instead of the real error, so clearing it here seems better.
const clearReadyTimeout = () => {
clearTimeout(readyTimeout);
this.cleanupTasks.delete(cleanupTaskId);
};
return Promise.race([this.onRunning().then(clearReadyTimeout, (err) => {
clearReadyTimeout();
throw err;
}), timeoutPromise]);There was a problem hiding this comment.
updated and simplified
onReady() only cleared its timeout when onRunning() resolved, so on start failure the timer stayed pending until the timeout expired, keeping a Node process alive. Clear it in onRunning().finally() instead. close() before the client is running already rejects onRunning() via stop(), so the cleanup task mechanism (cleanupTasks, INSTANCE_CLOSED) was redundant and is removed. onReady() still rejects with "Client stopped before running" on close, as before. Add tests that onReady() rejects immediately, without waiting for the timeout, when start has already failed, when start fails while waiting, and when close() is called while waiting. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Md Junaed Hossain (junaed-optimizely)
left a comment
There was a problem hiding this comment.
lgtm

Summary
After a failed init,
close()andonTerminated()never settled. The failure handler only rejectedstartPromise, andstop()does nothing once the client isFailed, so awaitingclose()hung (e.g. with an invalid datafile).stopPromisewith the init error and stop the child services (config manager, event processor, ODP manager) through a sharedstopChildServices()helper.onReady()clears its timeout inonRunning().finally(). Before, a failed init left the timer pending until the timeout (30 s by default), keeping a Node process alive.onReady()cleanup tasks andINSTANCE_CLOSED.close()during start already rejectsonRunning()viastop(), so they were redundant.onReady()rejects with the same errors as before.close()while the client is still starting still resolves.Test plan
start failuretests inlib/optimizely/index.spec.ts:close()andonTerminated()reject with the start error.close()is called while starting (which still resolves).onReady()rejects without waiting for the timeout and leaves no pending timer: when start has already failed, fails while waiting, orclose()is called while waiting.npm run test-mocha: 815 passing.npx vitest run: 1137 passed.tsc --noEmitclean.close()now rejects withgot invalid datafile; on 6.7.0 it stays pending.Issues
🤖 Generated with Claude Code