Skip to content

[Python] Avoid ZeroDivisionError in ConcatRangeTracker for zero weights - #40549

Open
raams95 wants to merge 4 commits into
apache:masterfrom
raams95:fix-concat-source-zero-weights
Open

raams95 wants to merge 4 commits into
apache:masterfrom
raams95:fix-concat-source-zero-weights

Conversation

@raams95

@raams95 raams95 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #21316

ConcatRangeTracker._compute_cumulative_weights fills in missing bundle weights with the average of the known weights. A weight of 0 is already treated as missing (s.weight or avg), but zeros were still counted when computing the average. If every bundle has weight 0, the average is 0, the total is 0, and this line raises ZeroDivisionError: float division by zero:

running_total.append(max(min_diff, min(1, running_total[-1] + w / total)))

This happens when a source's split() returns bundles with weight 0, for example when the weights are sizes rounded down to whole MB for bundles smaller than 1 MB, which was the case in the issue. #12142 fixed the MongoDB source on its own, but ConcatSource still fails for any source that does this.

This change leaves zero weights out of the average, in the same way as None weights. When every weight is 0 or missing, the tracker falls back to equal weights of 1.0, which it already does when no weights are given.

I added test_zero_weight_bundles to concat_source_test.py. It raises ZeroDivisionError before the change and passes after it.

Testing:

  • pytest apache_beam/io/concat_source_test.py apache_beam/io/iobase_test.py apache_beam/io/filebasedsource_test.py apache_beam/io/textio_test.py
  • yapf --diff and ruff check on the changed files

Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123 or fixes #456).
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement. (Not needed, small change.)

See the Contributor Guide for more tips on how to make review process smoother.

When every SourceBundle passed to a ConcatSource had weight 0, the
average used to fill in missing weights was also 0, so computing the
cumulative weights divided by zero. Treat zero weights like unknown
weights when computing that average.

Fixes apache#21316
@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @jrmccluskey for label python.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@Abacn

Abacn commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Thanks, this was an old issue for an edge case. To understand if it's still relavant, is there any example pipeline that could trigger this issue?

@codecov

codecov Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.92%. Comparing base (76f6e6d) to head (1db5308).
⚠️ Report is 163 commits behind head on master.

Additional details and impacted files
@@              Coverage Diff              @@
##             master   #40549       +/-   ##
=============================================
+ Coverage     56.16%   79.92%   +23.75%     
=============================================
  Files          1124      487      -637     
  Lines        178336    82297    -96039     
  Branches       1489        0     -1489     
=============================================
- Hits         100170    65776    -34394     
+ Misses        75645    16521    -59124     
+ Partials       2521        0     -2521     
Flag Coverage Δ
go ?
java ?
python 79.92% <100.00%> (+0.36%) ⬆️

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.

@jrmccluskey jrmccluskey 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.

+1 to Yi's request for a repro pipeline as a regression test.

@raams95

raams95 commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for taking a look. Built-in file sources don't hit this anymore (empty files are skipped and their bundles have no weights), but reading any ConcatSource whose bundles have weight 0 still fails on master, e.g.:

source = ConcatSource([
    iobase.SourceBundle(0, RangeSource(0, 4), None, None),
    iobase.SourceBundle(0, RangeSource(4, 8), None, None),
])
with TestPipeline() as p:
  assert_that(p | beam.io.Read(source), equal_to(list(range(8))))

On master this raises ZeroDivisionError: float division by zero [while running 'Read/SDFBoundedSourceReader/ParDo(SDFBoundedSourceDoFn)/PairWithRestriction']. That's the case from the issue, where weights come from sizes rounded down to whole MB.

I replaced the unit test with this pipeline as the regression test. It fails without the change and passes with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ZeroDivisionError if source bundle smaller than 1mb

3 participants