Skip to content

[FSSDK-13442] Settle the close promise after client init failure - #1189

Merged
Raju Ahmed (raju-opti) merged 3 commits into
masterfrom
raju/close-after-init-failure
Nov 17, 2026
Merged

Raju Ahmed (raju-opti) merged 3 commits into
masterfrom
raju/close-after-init-failure

Conversation

@raju-opti

@raju-opti Raju Ahmed (raju-opti) commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

After a failed init, close() and onTerminated() never settled. The failure handler only rejected startPromise, and stop() does nothing once the client is Failed, so awaiting close() hung (e.g. with an invalid datafile).

  • On start failure, reject stopPromise with the init error and stop the child services (config manager, event processor, ODP manager) through a shared stopChildServices() helper.
  • onReady() clears its timeout in onRunning().finally(). Before, a failed init left the timer pending until the timeout (30 s by default), keeping a Node process alive.
  • Remove the onReady() cleanup tasks and INSTANCE_CLOSED. close() during start already rejects onRunning() via stop(), so they were redundant. onReady() rejects with the same errors as before.

close() while the client is still starting still resolves.

Test plan

  • New start failure tests in lib/optimizely/index.spec.ts:
    • close() and onTerminated() reject with the start error.
    • Child services are stopped exactly once, including when 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, or close() is called while waiting.
  • npm run test-mocha: 815 passing. npx vitest run: 1137 passed. tsc --noEmit clean.
  • Manual: built SDK with a static config manager holding an invalid datafile. close() now rejects with got invalid datafile; on 6.7.0 it stays pending.

Issues

  • FSSDK-13442

🤖 Generated with Claude Code

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>
@coveralls

Coveralls (coveralls) commented Nov 17, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 77.435% (-0.06%) from 77.492% — raju/close-after-init-failure into master

…e starting

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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() and onTerminated() 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

Comment thread lib/optimizely/index.ts Resolved

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread lib/optimizely/index.ts
this.projectConfigManager.onRunning(),
this.eventProcessor ? this.eventProcessor.onRunning() : Promise.resolve(),
this.odpManager ? this.odpManager.onRunning() : Promise.resolve(),
this.vuidManager ? this.vuidManager.initialize() : Promise.resolve(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread lib/optimizely/index.ts Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@raju-opti
Raju Ahmed (raju-opti) merged commit bd3ea3a into master Nov 17, 2026
22 of 29 checks passed
@raju-opti
Raju Ahmed (raju-opti) deleted the raju/close-after-init-failure branch November 17, 2026 16:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants