Ji-Oh Yoo
03/13/2025, 11:03 PMEncountered exception during execution: RuntimeError('Cannot send a request, as the client has been closed.')
Traceback (most recent call last):
File "/home/worker/.local/lib/python3.10/site-packages/prefect/flow_engine.py", line 1316, in run_context
yield self
File "/home/worker/.local/lib/python3.10/site-packages/prefect/flow_engine.py", line 1378, in run_flow_async
await engine.call_flow_fn()
File "/home/worker/.local/lib/python3.10/site-packages/prefect/flow_engine.py", line 1331, in call_flow_fn
await self.handle_success(result)
File "/home/worker/.local/lib/python3.10/site-packages/prefect/flow_engine.py", line 923, in handle_success
await self.set_state(terminal_state)
File "/home/worker/.local/lib/python3.10/site-packages/prefect/flow_engine.py", line 873, in set_state
...
File "/home/worker/.local/lib/python3.10/site-packages/httpx/_client.py", line 1585, in request
return await self.send(request, auth=auth, follow_redirects=follow_redirects)
File "/home/worker/.local/lib/python3.10/site-packages/prefect/client/base.py", line 324, in send
response = await self._send_with_retry(
File "/home/worker/.local/lib/python3.10/site-packages/prefect/client/base.py", line 248, in _send_with_retry
response = await send(request, *send_args, **send_kwargs)
File "/home/worker/.local/lib/python3.10/site-packages/httpx/_client.py", line 1661, in send
raise RuntimeError("Cannot send a request, as the client has been closed.")
RuntimeError: Cannot send a request, as the client has been closed.
Marvin
03/13/2025, 11:03 PMhttpx
, make sure to manage the client's lifecycle properly. Use the client within a context manager (async with
) to ensure it is opened and closed correctly. For example:
python
async with get_client() as client:
# perform operations with client
This pattern ensures that the client is properly closed after its operations are complete.
2. Check for Premature Closure: Verify that the client isn't being closed prematurely in your code. Ensure that all operations requiring the client are completed before exiting the context manager.
3. Review Your Flow Code: The issue might be related to the flow's structure or how asynchronous operations are handled. Carefully review the flow code to ensure that asynchronous operations are awaited properly and the client isn't used outside its intended scope.
4. Update Prefect: Make sure you are using the latest version of Prefect. Sometimes, these issues are resolved in newer releases. You can check the latest updates and fixes in the release notes.
5. Review Related Issues: There might be similar issues reported by other users. For example, this GitHub issue discusses a related problem with client management.
If these steps don't resolve the issue, consider reaching out to Prefect support or checking the Prefect documentation on troubleshooting for further guidance.
If you need more specific guidance, feel free to share more details about your flow setup or any additional error logs.Nate
03/13/2025, 11:04 PMclient
into something? like Block.load
or something like thatJi-Oh Yoo
03/13/2025, 11:07 PMNate
03/13/2025, 11:07 PMJi-Oh Yoo
03/13/2025, 11:07 PMJi-Oh Yoo
03/13/2025, 11:11 PMJi-Oh Yoo
03/13/2025, 11:12 PMNate
03/13/2025, 11:13 PMdef thing_needing_client(*, client):
# closes client somewhere here
client = get_client()
thing_needing_client(..., client=client)
client.something() # raises
maybe its in your worker impl if you're using a custom worker?Ji-Oh Yoo
03/13/2025, 11:15 PM