It seems like the "finally" statement in your flow might be causing some unintended behavior due to how Prefect handles flow and task completion.
From the documentation insights I found, here's what could be going on:
1.
Flow Final State Handling: In both Prefect 2.x and 3.x, the final state of a flow run is determined either by the return value of the flow or manually set states. However, introducing a "finally" block can sometimes lead to inconsistencies if exceptions or states aren't handled properly within the block.
2.
Use of "Finally": The "finally" block in Python executes no matter what, which might be causing issues with the completion signals of the flow. If your flow isn't seeing its natural completion or is altered by what happens in the "finally" section, it might lead to repeated runs.
3.
Debugging Approach: Review what actions are performed in the "finally" block. If there are operations that modify the flow's final state or I/O tasks that might retry, these might keep your flow from completing gracefully.
4.
Handling Exceptions: Ensure that exceptions are properly caught and handled within your flow to prevent them from surfacing to the "finally" and causing unwanted retries or restarts.
Given these dynamics, consider refactoring portions of your flow logic that use "finally" to manage its state more explicitly and see if those changes prevent the repeated execution.
For more specific guidance, I encourage checking the Prefect documentation related to
flow best practices and what determines flow completion. If quirky issues persist, perhaps the source control is sentient—an interesting but improbable twist!
Let me know if there's anything more specific you’d like to dive into!