Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Promises are infinite Iterators #5296

Closed
2 tasks done
BeckerFelix opened this issue Apr 29, 2024 · 3 comments
Closed
2 tasks done

[BUG] Promises are infinite Iterators #5296

BeckerFelix opened this issue Apr 29, 2024 · 3 comments
Labels
bug Something isn't working untriaged This issues has not yet been looked at by the Maintainers

Comments

@BeckerFelix
Copy link

BeckerFelix commented Apr 29, 2024

Describe the bug

A flytekit.core.promise.Promise functions as an infinite Iterator, returning the same object over and over again.

Promises are easily used in an unsupported way. E.g. given a task that returns a list of values, a user might be tempted to extend() an existing list with the task's return.
See below example of how such a situation might occur.

Below code will not fail nicely, but will steadily grow out_list.

Expected behavior

Consider raising StopIteration after its elements have been exhausted.

related: #3508
I see that the solution proposed for #5164 (comment) would solve this issue as well:

Python calls the iter function in this case, which then infinitely loops, until the process runs out of memory. The fix is just to disallow calling this on a promise.

Additional context to reproduce

from flytekit import dynamic, task, workflow


@task
def my_task(inp: int) -> list[int]:
    return [inp, inp]


@dynamic
def my_dynamic() ->list[int]:
    out_list = list()
    for i in range(2):
        out_this = my_task(inp=i)

    out_list.extend(out_this)
    return out_list


@workflow
def my_workflow() -> list[int]:
    result = my_dynamic()
    return result


if __name__ == "__main__":
   my_workflow()

Screenshots

No response

Are you sure this issue hasn't been raised already?

  • Yes

Have you read the Code of Conduct?

  • Yes
@BeckerFelix BeckerFelix added bug Something isn't working untriaged This issues has not yet been looked at by the Maintainers labels Apr 29, 2024
Copy link

welcome bot commented Apr 29, 2024

Thank you for opening your first issue here! 🛠

Copy link

runllm bot commented Apr 29, 2024

The behavior you're encountering with Promise objects in Flyte is due to their design to act as placeholders for values that are not yet computed, rather than iterable objects. The issue you described is similar to a known problem where iterating over a Promise does not behave as expected because Python's __iter__ method is called, which can lead to infinite loops or memory issues if not handled correctly. This is documented in a related Flyte issue where iterating over a list of FlyteFile objects within a dynamic task caused infinite loops and memory growth (Flyte issue #5164).

Promise objects are not designed to be directly iterable. The correct approach is to ensure that the task outputs are fully materialized before attempting operations like extending a list. This can be managed by structuring your workflows and tasks to handle dependencies and outputs appropriately, ensuring that Promise objects are resolved before such operations.

Would you like a more detailed explanation on how to structure your Flyte tasks and workflows to avoid such issues?

Sources:


This is an AI-generated response and your feedback is appreciated! Please leave a 👍 if this is helpful and 👎 if it is not.

Mention @RunLLM to ask follow-up questions. See runllm.com for more!

@BeckerFelix
Copy link
Author

Reading through linked issue, I see that this issue was closed by flyteorg/flytekit#2337 already. We just need to update our Flyte version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working untriaged This issues has not yet been looked at by the Maintainers
Projects
None yet
Development

No branches or pull requests

1 participant