diff --git a/CHANGES.md b/CHANGES.md index a3adbb3d7af9..def1ca9d0aa5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -167,6 +167,7 @@ ## Bugfixes +* (Python) Fixed `with_exception_handling(use_subprocess=True)` sending every element to the dead-letter output when the transform has an `AsIter` side input or receives large grouped values ([#25374](https://github.com/apache/beam/issues/25374)). * (Python) Fixed RunInference failing with `TypeError: cannot create weak reference to 'dict' object` when `ModelHandler.load_model()` returns a model that can't be weakly referenced, such as a `dict` or `list` ([#41351](https://github.com/apache/beam/issues/41351)). * (Java) Flink `OrderedListState` now preserves multiple values with the same timestamp when reading state or clearing a different timestamp range ([#39782](https://github.com/apache/beam/issues/39782)). * (Python) Fixed Vertex AI embedding transforms ignoring `project`, `location` and `credentials` on remote workers ([#41957](https://github.com/apache/beam/pull/41957)). diff --git a/sdks/python/apache_beam/transforms/core.py b/sdks/python/apache_beam/transforms/core.py index c1a216f81e10..893d19c41611 100644 --- a/sdks/python/apache_beam/transforms/core.py +++ b/sdks/python/apache_beam/transforms/core.py @@ -24,6 +24,7 @@ import copy import inspect import logging +import pickle import random import sys import time @@ -33,6 +34,7 @@ from collections import defaultdict from functools import wraps from itertools import dropwhile +from multiprocessing.reduction import ForkingPickler from apache_beam import coders from apache_beam import pvalue @@ -2686,7 +2688,12 @@ def start_bundle(self): pass def process(self, *args, **kwargs): - return self._call_remote(self._remote_process, *args, **kwargs) + # Pickle the arguments here instead of leaving it to the pool's queue + # feeder thread. Lazy, state-backed arguments (such as AsIter side inputs + # or large grouped values) read their contents through the state API when + # pickled, and that only works on the thread that is processing the bundle. + serialized_args = bytes(ForkingPickler.dumps((args, kwargs))) + return self._call_remote(self._remote_process, serialized_args) def finish_bundle(self): self._call_remote(self._remote_finish_bundle) @@ -2740,7 +2747,8 @@ def _remote_init(cls, serialized_fn): cls._started = False @classmethod - def _remote_process(cls, *args, **kwargs): + def _remote_process(cls, serialized_args): + args, kwargs = pickle.loads(serialized_args) if cls._fn is None: cls._fn = pickler.loads(cls._serialized_fn) cls._fn.setup() diff --git a/sdks/python/apache_beam/transforms/ptransform_test.py b/sdks/python/apache_beam/transforms/ptransform_test.py index 037619e8bb07..07f8acd6ed39 100644 --- a/sdks/python/apache_beam/transforms/ptransform_test.py +++ b/sdks/python/apache_beam/transforms/ptransform_test.py @@ -2788,6 +2788,10 @@ def die_if_less(cls, x, bound=0): else: return x, bound + @classmethod + def die_if_less_than_sum(cls, x, values): + return cls.die_if_less(x, sum(values)) + def test_error_messages(self): with TestPipeline() as p: good, bad = ( @@ -2855,6 +2859,22 @@ def test_side_inputs(self): equal_to([(100, 30)]), label='KeySideInput') + def test_iterable_side_input(self): + # Lazy, state-backed side inputs must be readable when the element is sent + # to the subprocess (https://github.com/apache/beam/issues/25374). + with TestPipeline() as p: + side = p | 'Side' >> beam.Create([5, 15]) + good, bad = ( + p + | beam.Create([-1, 10, 100]) + | beam.Map(self.die_if_less_than_sum, beam.pvalue.AsIter(side)) + .with_exception_handling(use_subprocess=self.use_subprocess)) + assert_that(good, equal_to([(100, 20)]), label='CheckGood') + assert_that( + bad | beam.MapTuple(lambda e, _: e), + equal_to([-1, 10]), + label='CheckBad') + def test_multiple_outputs(self): die = type(self).die