Open tyong920 opened 1 year ago
This issue is stale because it has been open 30 days with no activity. To keep this issue open remove stale label or comment.
I have experienced this as well.
sync_compatible
seems to be at fault in my case. I can be reasonable certain of this because forcing the offending sync_compatible
decorated function to run async via asyncio.run
works around this issue:
from prefect.something import offending_function
def call_offending_function_sync():
async def wrapper():
await offending_function()
asyncio.run(wrapper())
It's rather surprising to me that the sync compatibility decorator doesn't take a similar approach with asyncio.run
. Perhaps there should be some heuristic that attempts to do so before submitting a task to the global thread loop.
@zanieb here's the shortest example I've been able to come up with:
import multiprocessing as mp
from prefect_aws import S3Bucket
S3Bucket.load("bucket-name")
if __name__ == "__main__":
proc = mp.get_context("fork").Process(target=lambda: None, daemon=True)
proc.start()
proc.join()
@zanieb if you comment out this line it does not hang:
My earlier statement about this being related to sync_compatible
seem to be wrong. This still hangs:
import asyncio
import multiprocessing as mp
from prefect_aws import S3Bucket
async def func():
await S3Bucket.load("global-storage")
asyncio.run(func())
if __name__ == "__main__":
proc = mp.get_context("fork").Process(target=lambda: None, daemon=True)
proc.start()
proc.join()
Going one layer deeper, removing usage of this lock also prevents the hang:
Seems related to the problem in this article - the lock is likely being copied in an acquired state. The subprocess thus, can never acquire the lock.
The solution seem to just not use forked subprocesses. According to the article:
- Starting in Python 3.12, you will get a DeprecationWarning indicating that “fork” will stop being the default in 3.14.
- In Python 3.14, the default will be changed to either “spawn” or “forkserver” (a mostly safer alternative to “fork”).
First check
Bug summary
When calling a user defined task or sub-flow from entry point flow, if it invokes a Process Pool, Prefect might hangs forever.
Reproduction
Error
Versions
Additional context
No response