Skip to content

Introduce context.Context support to prevent goroutine leaks#386

Merged
cinar merged 6 commits into
masterfrom
context-cancellation
May 29, 2026
Merged

Introduce context.Context support to prevent goroutine leaks#386
cinar merged 6 commits into
masterfrom
context-cancellation

Conversation

@cinar
Copy link
Copy Markdown
Owner

@cinar cinar commented May 28, 2026

Summary

Adds context.Context cancellation support to every channel-based operator in helper/ and every indicator Compute method. This prevents goroutine leaks when downstream consumers stop reading from output channels (e.g., early exit, timeout, strategy condition satisfied).

Changes

  • New methods: ComputeWithContext(ctx, ...) on all indicators, *WithContext(ctx, ...) on all helper functions
  • Backward compatibility: Old Compute(...) methods preserved as deprecated shims delegating to ComputeWithContext(context.Background(), ...)
  • Helper package: ~50 helper functions now select on ctx.Done() alongside channel operations
  • Strategy layer: StrategyWithContext interface with ComputeWithContext; ComputeStrategyWithContext type-asserts and falls back to old Strategy.Compute
  • Moving average: Named MaWithContext[T] interface for discoverable context-aware MA dispatch
  • Tests: All existing tests pass; added cancellation_test.go for helper cancellation coverage

Scope

216 files changed, ~7,770 insertions / ~3,303 deletions.

Fixes #372

cinar added 3 commits May 27, 2026 21:34
Add ComputeWithContext methods to all indicators and WithContext
variants to all helper functions, enabling cancellation support
through context.Context. Old Compute methods are preserved as
deprecated shims that delegate to ComputeWithContext with
context.Background().

Fixes #372
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 28, 2026

Codecov Report

❌ Patch coverage is 90.33408% with 217 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.90%. Comparing base (593f973) to head (8247ad0).

Files with missing lines Patch % Lines
momentum/td_sequential.go 59.18% 20 Missing ⚠️
trend/pivot_point.go 47.05% 14 Missing and 4 partials ⚠️
helper/operate5.go 76.00% 12 Missing ⚠️
helper/operate4.go 76.19% 10 Missing ⚠️
trend/ema.go 62.96% 9 Missing and 1 partial ⚠️
trend/rma.go 62.96% 9 Missing and 1 partial ⚠️
trend/smma.go 62.96% 9 Missing and 1 partial ⚠️
helper/csv.go 78.57% 9 Missing ⚠️
trend/kama.go 72.72% 8 Missing and 1 partial ⚠️
helper/operate3.go 76.47% 8 Missing ⚠️
... and 28 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #386      +/-   ##
==========================================
- Coverage   94.02%   91.90%   -2.13%     
==========================================
  Files         225      226       +1     
  Lines        6530     7162     +632     
==========================================
+ Hits         6140     6582     +442     
- Misses        309      490     +181     
- Partials       81       90       +9     

☔ View full report in Codecov by Sentry.
📢 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.

cinar added 3 commits May 27, 2026 21:53
- TestComputeMaWithContextFallback: covers the fallback path when Ma
  does not implement MaWithContext
- TestComputeStrategyWithContextFallback: covers the fallback path
  when Strategy does not implement StrategyWithContext
- TestCancellationWithApplyWindow, TestCancellationWithEchoSkip:
  extend cancellation coverage to more helper functions
- Propagate ctx through ChangeWithContext -> SubtractWithContext
- Propagate ctx through ChangeRatioWithContext -> DivideWithContext, ChangeWithContext
- Propagate ctx through ChangePercentWithContext -> MultiplyByWithContext, ChangeRatioWithContext
- Fix stdlib import ordering in connors_rsi.go and pivot_point.go
@cinar cinar merged commit d87f947 into master May 29, 2026
5 checks passed
@cinar cinar deleted the context-cancellation branch May 29, 2026 01:19
@github-actions github-actions Bot locked and limited conversation to collaborators May 29, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improvement: Introduce Context support in channel-based computations to prevent goroutine leaks

2 participants