feat: validate terminal steps with unmaterialized stream output at build time#37
Merged
Merged
Conversation
…ild time A terminal step (no consumers) whose output is Iterator/Generator or AsyncIterator/AsyncGenerator and whose output is not materialized (needs_materialize is False) produces a stream that nobody drains. At runtime this causes either a deadlock (bounded handoff pump blocks forever) or silent data loss (the pump discards items to deliver the EOF marker). Add validate_no_unmaterialized_terminal_streams in dag_steps.py, called from build_dag after materialized_deps are computed. Uses the existing needs_materialize flag -- no new materialization logic. Steps exported via sub-pipeline exports are skipped because they will have consumers in the parent. Also propagate force_materialize through sub-pipeline expansion (was missing). Fix existing tests with latent terminal Iterator-returning steps: - corpus complex_parallel_mixed: step3 -> list[int] - observer runtime: lazy_consumer/passthrough -> None (drain input) - dag_materializer: add force_materialize=True (test targets UNRUNNABLE) - dag_expansion: add force_materialize=True (test targets DAG structure) - materializers_ergonomics: add force_materialize=True on sub-pipeline gen - async_runner_basic: add force_materialize=True (test targets RuntimeError) Document in DESIGN_PHILOSOPHY that observers receiving an Iterator must consume it fully (application responsibility, causes tee buffer growth otherwise).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Reject at build time terminal steps whose output is a stream that will never be consumed. This is the root-cause fix for the
SyncFanout._put_terminaldata-loss/deadlock issue identified in #34.The problem
A terminal step (no consumers, not hidden) whose output type is
Iterator/Generator(sync) orAsyncIterator/AsyncGenerator(async) and whose output is not materialized (needs_materializeisFalse) produces a stream that nobody drains. At runtime this causes either:get_nowait()to force the EOF throughThe validation
validate_no_unmaterialized_terminal_streamsindag_steps.py, called frombuild_dagaftermaterialized_depsare computed. Uses the existingdag.needs_materialize()flag — no new materialization logic.The
exportsstep of a sub-pipeline is skipped because it will have consumers in the parent pipeline.Bug fix:
force_materializepropagationforce_materializewas not propagated through sub-pipeline expansion (dag_expansion.py). Fixed by addingforce_materialize=sub_step.force_materializeto the expandedStep.Test fixes
Existing tests with latent terminal Iterator-returning steps (the output was silently lost):
complex_parallel_mixed:step3changed from-> Generatorto-> list[int]lazy_consumer/passthroughchanged from-> Iteratorto-> None(drain input in body)force_materialize=True(tests target UNRUNNABLE error, not terminal stream)force_materialize=True(tests target DAG structure, not execution)force_materialize=Trueon sub-pipelinegenstepforce_materialize=True(test targets RuntimeError, not terminal stream)New tests
6 new tests in
test_dag_builder_validation.pycovering:Iteratoroutput → raisesAsyncIteratoroutput → raisesIterator+force_materialize=True→ buildsNoneoutput (consumer drains) → buildsIteratoroutput → buildsIteratorin child pipeline → buildsDocumentation
Updated
DESIGN_PHILOSOPHY.mdobserver contract: when output is anIterator/AsyncIterator, the observer receives it directly (viatee) and must consume it fully. An unconsumed iterator causesteebuffer growth and silently lost data. This is application responsibility.Verification
ruff format/ruff check --select F401: cleanpytest tests/: 451 passed in 2.23s