Skip to content

fix: initialization request cancelled by poller on slow network#118

Draft
duyhungtnn wants to merge 1 commit intomainfrom
fix/start-get-evaluation-tasks-after-init
Draft

fix: initialization request cancelled by poller on slow network#118
duyhungtnn wants to merge 1 commit intomainfrom
fix/start-get-evaluation-tasks-after-init

Conversation

@duyhungtnn
Copy link
Copy Markdown
Collaborator

This pull request introduces a mechanism to control when evaluation polling tasks (foreground and background) are enabled in the Bucketeer SDK, ensuring that no polling occurs during client initialization and preventing race conditions or request cancellations. The change also adds comprehensive tests to validate the new behavior and updates documentation for developer guidance.

Task enable/disable mechanism for evaluation polling:

  • Added an enabled flag to EvaluationForegroundTask and EvaluationBackgroundTask, with methods to enable tasks after initialization completes. Tasks are now constructed with enabled: false and only enabled after the initial fetch. [1] [2] [3] [4]
  • Modified polling logic in both tasks to check the enabled flag before executing, preventing premature polling during initialization. [1] [2]

Integration and scheduling improvements:

  • Updated TaskScheduler to provide an enableEvaluationTask() method that enables both foreground and background evaluation tasks after client initialization.
  • Changed BKTClient initialization flow so that evaluation tasks are enabled only after the initial fetch completes (success or failure), preventing background poller from interfering with initialization.

Testing and documentation:

  • Added a new test suite EvaluationTaskEnabledTests.swift with five tests to verify task enable/disable behavior, integration, and correct polling after initialization (including failure scenarios).
  • Added a detailed project overview and architecture guide to .github/copilot-instructions.md for developer and AI tooling guidance.

Make evaluation polling opt-in by default: add an 'enabled' flag and enable() API to EvaluationForegroundTask and EvaluationBackgroundTask, and update TaskScheduler to create evaluation tasks disabled by default. Add TaskScheduler.enableEvaluationTask() to enable both foreground and background evaluation tasks. Update BKTClient to call enableEvaluationTask() after the initial fetch completes (success or failure) to avoid the background poller interfering with initialization. Add comprehensive unit tests (EvaluationTaskEnabledTests) to verify disabled-by-default behavior, enabling, and that tasks remain enabled after init success/failure. Also add .github/copilot-instructions.md documentation for repository guidance.
@duyhungtnn duyhungtnn changed the title chore: ensuring that no polling occurs during client initialization and preventing race conditions or request cancellations chore: ensuring that no polling occurs during client initialization Feb 9, 2026
@duyhungtnn
Copy link
Copy Markdown
Collaborator Author

duyhungtnn commented Feb 9, 2026

During testing the iOS, I saw this error only one when I have a super slow network connection.
It seems quite race but we may consider to make a fix

Request Cancellation Issue in BKTClient Initialization

The Problem

The foreground poller can cancel the initial fetchEvaluations request during BKTClient.initialize(), causing the error: BKTError.illegalState(message: "Request cancelled by newer execution").

What happens:

  1. initialize() starts the poller (60s interval) and begins initial fetch with requestId = A
  2. Initial request gets HTTP 499 → retries with exponential backoff (1s, 2s, 4s, 8s...)
  3. If retries exceed 60 seconds, because network issue, the poller fires → creates new request with requestId = B
  4. Original retry fails the currentRequestId == requestId check → gets cancelled

When: During server deployments with 499 errors when retries exceed 60 seconds.

@duyhungtnn duyhungtnn changed the title chore: ensuring that no polling occurs during client initialization fix: initialization request cancelled by poller on slow network Feb 9, 2026
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.

1 participant