Skip to content

Conversation

@wenyue
Copy link
Contributor

@wenyue wenyue commented Nov 3, 2025

Introduce the maxRetries and retryInterval in isFirstInstance

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 3, 2025

📝 Walkthrough

Summary by CodeRabbit

  • Refactor
    • Enhanced instance detection with configurable retry mechanism—adjust maximum retries and retry intervals to suit your application needs.
    • Improved error resilience during instance activation with optimized recovery logic.

Pre-merge checks and finishing touches

✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The pull request title 'feat: Add cache and retry support' accurately reflects the main changes in the changeset. The AI-generated summary confirms that the PR introduces caching semantics for _isFirstInstance (shifting from bool? to Future?) and adds retry logic with maxRetries and retryInterval parameters to the isFirstInstance method. The title captures both key aspects of the enhancement.
Description check ✅ Passed The pull request description 'Introduce the maxRetries and retryInterval in isFirstInstance' is directly related to the changeset. It accurately describes a core aspect of the changes—the introduction of configurable retry parameters to the isFirstInstance method. While the description doesn't mention the caching aspect, it is sufficiently related to the changeset and provides meaningful information about the primary enhancement.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2d367fc and a4922f5.

📒 Files selected for processing (1)
  • lib/flutter_single_instance.dart (4 hunks)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7162226 and b24dbf0.

📒 Files selected for processing (2)
  • lib/flutter_single_instance.dart (4 hunks)
  • lib/src/unsupported.dart (1 hunks)

@mcquenji mcquenji changed the title feat: enhance isFirstInstance method with retry logic and caching feat: Add cache and retry support Nov 3, 2025
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
wenyue and others added 2 commits November 3, 2025 23:08
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

♻️ Duplicate comments (1)
lib/flutter_single_instance.dart (1)

153-172: Loop still performs no retries when maxRetries=1.

With while (attempt < maxRetries) the default maxRetries = 1 yields exactly one attempt and zero retries, which is the same problem previously flagged. Please iterate from the initial attempt through the configured retry count (inclusive) so callers actually get the number of retries they requested.

-      bool result = false;
-      int attempt = 0;
-
-      while (attempt < maxRetries) {
+      bool result = false;
+
+      for (var attempt = 0; attempt <= maxRetries; attempt++) {
         // Wait before retrying (skip for first attempt).
         if (attempt > 0) {
           logger.finest(
               "Retrying activateInstance (attempt $attempt/$maxRetries)");
           await Future.delayed(retryInterval);
         }
 
-        result = await activateInstance(processName);
+        result = await activateInstance(resolvedProcessName);
 
         if (result) {
           break; // Exit loop if activation succeeds.
         }
-
-        attempt++;
       }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a6509f9 and 2d367fc.

📒 Files selected for processing (1)
  • lib/flutter_single_instance.dart (4 hunks)

Comment on lines 140 to 141
assert(maxRetries >= 1, 'maxRetries must be greater than or equal to 1');
assert(retryInterval != Duration.zero, 'retryInterval must be non-zero');
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Replace debug-only asserts with runtime argument validation.

assert disappears in release builds, so bad inputs like maxRetries = 0 reach production and the method never even attempts activation. Please guard these parameters with real checks (e.g., throw ArgumentError) so the runtime contract is enforced outside debug mode.(dart.dev)

-    if (_isFirstInstance != null) {
-      return _isFirstInstance!;
-    }
-
-assert(maxRetries >= 1, 'maxRetries must be greater than or equal to 1');
-assert(retryInterval != Duration.zero, 'retryInterval must be non-zero');
+    if (_isFirstInstance != null) {
+      return _isFirstInstance!;
+    }
+
+    if (maxRetries < 1) {
+      throw ArgumentError.value(
+        maxRetries,
+        'maxRetries',
+        'must be greater than or equal to 1',
+      );
+    }
+    if (retryInterval == Duration.zero) {
+      throw ArgumentError.value(
+        retryInterval,
+        'retryInterval',
+        'must be non-zero',
+      );
+    }
🤖 Prompt for AI Agents
In lib/flutter_single_instance.dart around lines 140 to 141, the code uses
debug-only asserts for validating maxRetries and retryInterval; replace those
asserts with runtime argument validation by throwing ArgumentError (e.g.,
ArgumentError.value) when maxRetries < 1 or retryInterval == Duration.zero,
providing clear messages and parameter names so the contract is enforced in
release builds.

Copy link
Contributor

@mcquenji mcquenji left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution!

@mcquenji mcquenji merged commit d1ff49f into necodeIT:main Nov 3, 2025
1 check passed
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.

2 participants