Skip to content

fix(pkg/backoff): support wrapped errors, prevent timer leak, and make exponential clone concurrency-safe - #7490

Open
Norway-02 wants to merge 2 commits into
pipe-cd:masterfrom
Norway-02:fix/backoff-error-handling-and-timer-leak
Open

Norway-02 wants to merge 2 commits into
pipe-cd:masterfrom
Norway-02:fix/backoff-error-handling-and-timer-leak

Conversation

@Norway-02

Copy link
Copy Markdown
Contributor

What this PR does:

This PR improves reliability, error handling, and concurrency safety in pkg/backoff:

  1. Support standard Go error unwrapping (Unwrap() error): Adds Unwrap() error to *Error so errors.Is(err, target) and errors.As(err, &target) work seamlessly across standard Go error trees.
  2. Support wrapped errors in Retry.Do(): Previously, Do() checked if e, ok := err.(*Error); ok, which only matched if *Error was returned directly. If an operation returned a wrapped error (e.g., fmt.Errorf("task failed: %w", backoff.NewError(err, false))), Do() failed to identify it as non-retriable and erroneously retried the operation. Using errors.As(err, &bErr) properly inspects wrapped error chains.
  3. Prevent timer resource leak in WaitNext(): WaitNext() created time.NewTimer(d) but never stopped it when <-ctx.Done() triggered. Adding defer t.Stop() ensures active timers are promptly cleared from the runtime timer heap when contexts are canceled.
  4. Make exponential.Clone() concurrency-safe: Previously, exponential.Clone() copied rand: b.rand. Because *rand.Rand is not safe for concurrent use, calling Next() on cloned backoffs across goroutines caused data races. Clone() now creates an independent rand.Rand instance.
  5. Guard against nil rand: In exponential.Next(), safely fall back to global rand.Float64() if b.rand is nil.

Why we need it:

  • Standard Go error inspection (errors.Is / errors.As) failed on errors returned or wrapped with backoff.Error.
  • Operations that wrap errors with contextual messages (e.g. fmt.Errorf("...: %w", err)) had their Retriable: false markers ignored by Retry.Do().
  • Long-running background operations in piped controllers and schedulers that loop on WaitNext(ctx) leaked timers on cancellation.
  • Concurrent workers sharing cloned exponential backoffs experienced data races on rand.Rand.

Which issue(s) this PR fixes:

Fixes #

Does this PR introduce a user-facing change?:

  • How are users affected by this change: More reliable retry behavior, lower timer overhead on cancellation, and thread-safe exponential backoff cloning.
  • Is this breaking change: No
  • How to migrate (if breaking change): N/A

cc @mohammedfirdouss

…e exponential clone concurrency-safe

- Implement Unwrap() error on *Error to support standard Go 1.13+ error unwrapping.
- Use errors.As in Retry.Do() so wrapped errors (e.g. via fmt.Errorf) properly respect retriable / non-retriable classification.
- Stop timer via defer t.Stop() in WaitNext() to eliminate timer resource leaks on context cancellation.
- Allocate independent rand.Rand instances in exponential.Clone() to avoid data races across cloned backoff instances.
- Guard against nil rand.Rand in exponential.Next().
- Add unit tests for Unwrap, wrapped errors in Do, timer cleanup on cancel, and concurrent clone execution.

Signed-off-by: Norway-02 <anshulkhetade02@gmail.com>
@Norway-02
Norway-02 requested a review from a team as a code owner October 17, 2026 05:13
@Norway-02
Norway-02 requested review from ffjlabo, khanhtc1202 and t-kikuc and a balanced review from Copilot October 17, 2026 05:13
@netlify

netlify Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

👷 Deploy request for pipecd-site pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 7c61d9b

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions

Copy link
Copy Markdown
Contributor

This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions Bot added the Stale label Nov 17, 2026
@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 31.50%. Comparing base (2c4d78d) to head (7c61d9b).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7490      +/-   ##
==========================================
+ Coverage   31.47%   31.50%   +0.03%     
==========================================
  Files         604      604              
  Lines       53115    53129      +14     
==========================================
+ Hits        16716    16739      +23     
+ Misses      34833    34823      -10     
- Partials     1566     1567       +1     
Flag Coverage Δ
. 25.19% <100.00%> (+0.05%) ⬆️
.-pkg-app-pipedv1-plugin-analysis 29.19% <ø> (ø)
.-pkg-app-pipedv1-plugin-ecs 35.98% <ø> (ø)
.-pkg-app-pipedv1-plugin-kubernetes 60.62% <ø> (ø)
.-pkg-app-pipedv1-plugin-kubernetes_multicluster 62.41% <ø> (ø)
.-pkg-app-pipedv1-plugin-scriptrun 58.22% <ø> (ø)
.-pkg-app-pipedv1-plugin-terraform 60.79% <ø> (ø)
.-pkg-app-pipedv1-plugin-wait 32.22% <ø> (ø)
.-pkg-app-pipedv1-plugin-waitapproval 55.14% <ø> (ø)
.-pkg-plugin-sdk 51.89% <ø> (-0.18%) ⬇️
.-tool-actions-gh-release 20.00% <ø> (ø)
.-tool-actions-plan-preview 31.08% <ø> (ø)
.-tool-codegen-protoc-gen-auth 0.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

Copy link
Copy Markdown
Contributor

This PR was closed because it has been stalled for 7 days with no activity. Feel free to reopen if still applicable.

@github-actions github-actions Bot closed this Nov 25, 2026
@github-actions github-actions Bot removed the Stale label Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants