Skip to content

Fix nested conditionals running skipped branches in local execution - #3473

Open
jackiectl2 wants to merge 1 commit into
flyteorg:masterfrom
jackiectl2:nested-conditional-local-branch
Open

jackiectl2 wants to merge 1 commit into
flyteorg:masterfrom
jackiectl2:nested-conditional-local-branch

Conversation

@jackiectl2

Copy link
Copy Markdown

Tracking issue

Closes flyteorg/flyte#6426

Why are the changes needed?

In local execution, when a conditional is nested inside the active branch of another conditional (either inline in .then(...) or inside a subworkflow called from that branch), every branch of the nested conditional executes its tasks, not only the selected one. In the issue's example, top_workflow(input_value=True) runs do_test, do_nothing1 and do_nothing2, although do_test returns False and only do_nothing2 should run. The returned value is correct (the selected case's output is used), but the non-selected tasks still run, with their side effects and cost.

The cause is FlyteContext.Builder.enter_conditional_section: when already inside a condition whose branch is BRANCH_ACTIVE, it kept the parent's execution state object as-is. The nested LocalExecutedConditionalSection therefore started in BRANCH_ACTIVE, and since start_branch only ever sets the state to active, every branch ran.

What changes were proposed in this pull request?

  • In local execution, every conditional section now gets its own copy of the execution state starting in BRANCH_SKIPPED, exactly as a top-level conditional already did. The nested section then activates only its selected branch, and it no longer mutates the parent's state.
  • Conditionals nested in a skipped branch are unchanged: conditional() still checks the parent's state and returns a SkippedConditionalSection.
  • Compilation / remote execution is unaffected (this code path only applies to local execution).

How was this patch tested?

Added two unit tests in tests/flytekit/unit/core/test_conditions.py that record which tasks actually execute:

  • test_nested_condition_runs_only_selected_branch: inline nested conditional, checks the inner if / inner else / outer else paths.
  • test_condition_in_subworkflow_called_from_branch: the reproduction from the issue (conditional inside a subworkflow called from a branch).

Both fail on master and pass with this change. The other unit test modules that use conditionals (test_dynamic_conditional, test_type_hints, test_workflows, test_serialization, test_gate, test_context_manager, test_promise, test_node_creation, test_references, remote/test_wrapper_classes) pass. Running all of tests/flytekit/unit/core locally gave the same set of failures with and without this change (unrelated, environment-specific: docker / optional dependencies).

Setup process

# issue reproduction; run locally with `python repro.py`
top_workflow(input_value=True)
# before: do_test, do_nothing1, do_nothing2
# after:  do_test, do_nothing2

Screenshots

N/A

Check all the applicable boxes

  • I updated the documentation accordingly.
  • All new and existing tests passed.
  • All commits are signed-off.

Related PRs

None

Docs link

N/A

In local execution, a conditional nested inside the active branch of
another conditional (inline, or inside a subworkflow called from that
branch) reused the parent's execution state, which was already marked
BRANCH_ACTIVE. Every branch of the nested conditional therefore ran its
tasks, even the ones whose condition was false.

Give every local conditional section its own execution state starting
in BRANCH_SKIPPED, so only the selected branch of a nested conditional
runs. Sections nested in a skipped branch still become
SkippedConditionalSection, as before.

Closes flyteorg/flyte#6426

Signed-off-by: Tianlang (Jackie) Chen <305351710+jackiectl2@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Conditionals do not work correctly with nested workflows

1 participant