<@ULVA73B9P> While running my Prefect flows on a s...
# ask-marvin
g
@Marvin While running my Prefect flows on a self hosted setup, where prefect servers is hosted on GCP VM, and prefect workers are created as Cloud Run V2 Jobs, I face the following error:
Copy code
Traceback (most recent call last):
  File "/opt/venv/lib/python3.12/site-packages/prefect/runner/_observers.py", line 176, in __aexit__
    await self._consumer_task
  File "/opt/venv/lib/python3.12/site-packages/prefect/runner/_observers.py", line 74, in _consume_events
    self.on_cancelling(flow_run_id)
  File "/opt/venv/lib/python3.12/site-packages/prefect/runner/runner.py", line 1573, in <lambda>
    on_cancelling=lambda flow_run_id: self._runs_task_group.start_soon(
                                      ^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Runner' object has no attribute '_runs_task_group'. Did you mean: '_loops_task_group'?
Also there was the below error in one of the flow runs :
Copy code
An error occurred while monitoring flow run '385ffa45-6936-4f57-87d4-b6ddc1b4e8e3'. The flow run will not be marked as failed, but an issue may have occurred.
Traceback (most recent call last):
  File "/opt/venv/lib/python3.12/site-packages/prefect/workers/base.py", line 1288, in _submit_run_and_capture_errors
    result = await self.run(
             ^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/prefect_gcp/workers/cloud_run_v2.py", line 660, in run
    result = await run_sync_in_worker_thread(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/prefect/utilities/asyncutils.py", line 233, in run_sync_in_worker_thread
    result = await anyio.to_thread.run_sync(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/anyio/to_thread.py", line 56, in run_sync
    return await get_async_backend().run_sync_in_worker_thread(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 2485, in run_sync_in_worker_thread
    return await future
           ^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 976, in run
    result = context.run(func, *args)
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/prefect/utilities/asyncutils.py", line 243, in call_with_mark
    return call()
           ^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/prefect_gcp/workers/cloud_run_v2.py", line 891, in _watch_job_execution_and_get_result
    execution = self._watch_job_execution(
                ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/prefect_gcp/workers/cloud_run_v2.py", line 962, in _watch_job_execution
    execution = ExecutionV2.get(
                ^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/prefect_gcp/models/cloud_run_v2.py", line 363, in get
    response = request.execute()
               ^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/googleapiclient/http.py", line 938, in execute
    raise HttpError(resp, content, uri=self.uri)
Diagnose the issue and suggest a suitable fix
m
thought for 581.3 seconds
Thanks for the detailed traces — that helps a lot. You’re hitting two separate issues: 1) AttributeError: Runner object has no attribute _runs_task_group - Diagnosis: This is a Prefect 3.x bug in the flow runner’s cancellation/observer logic. The code path that handles “on_cancelling” tries to reference
_runs_task_group
but the internal attribute is
_loops_task_group
. It shows up during cancellation or cleanup and can surface even if the flow otherwise runs. - Fix: Upgrade Prefect in your flow image and your worker image to the latest 3.x. We’ve seen this resolved by moving to a newer 3.x release (current is 3.6.1). Be sure the server, worker, and flow runtime all use compatible/newer versions. Example upgrade steps in your images:
Copy code
pip install --upgrade 'prefect>=3.6.1'
# if you’re using integrations, upgrade them too
pip install --upgrade 'prefect-gcp>=0.6.0' google-api-python-client
After upgrading, rebuild/redeploy your Cloud Run job image(s) and restart the worker. Useful links: - Prefect release notes: Prefect 3.x release notes 2) googleapiclient HttpError in Cloud Run V2 ExecutionV2.get (while monitoring the job) - Diagnosis: This occurs when the Cloud Run V2 worker polls the Cloud Run Jobs API to monitor execution. The most common causes are: - Insufficient IAM on the worker’s service account to read executions - Wrong project/region configuration - Cloud Run API not enabled in the project - Credentials not being picked up correctly inside the worker container - Recommended checks/fixes: 1) Ensure the correct APIs are enabled in the target GCP project: - Cloud Run API (run.googleapis.com) 2) Verify the worker’s service account IAM. The worker SA needs to be able to create and monitor Cloud Run Job executions. At minimum: - roles/run.viewer (to read executions) — for monitoring only - If the worker is creating executions (it is), give it roles/run.admin - If the Cloud Run Job runs as a different service account, grant the worker SA roles/iam.serviceAccountUser on the job’s runtime SA 3) Confirm project and region are correct in your Cloud Run V2 worker variables: - The region in your worker configuration must match the region where the job executes (e.g., us-central1) 4) Ensure credentials are correctly available to the worker: - On Cloud Run Jobs, prefer using the job’s attached service account with the roles above - If using a key, confirm the JSON is mounted and GOOGLE_APPLICATION_CREDENTIALS points to it 5) Get the exact HttpError details: - The
googleapiclient.http.HttpError
includes an HTTP status code and message (e.g., 401/403/404). That narrows the root cause precisely: - 401: credentials invalid/unavailable - 403: credentials valid but missing permission (add roles as above) - 404: region/project/job name mismatch or resource not found 6) Quick validation outside Prefect (with the same SA):
Copy code
gcloud run jobs executions describe --region <region> <job-name> --project <project-id>
- If this fails with the worker’s SA, it’s an IAM or region/project issue. Docs: - Cloud Run v2 worker guide: Cloud Run V2 worker guide Next steps / what I need from you to confirm: - Which Prefect versions are in: - Prefect server (self-hosted API/Server) - Worker image(s) - Flow runtime image - Your prefect-gcp version - The full
HttpError
status code and message body from the Cloud Run monitoring failure - The IAM roles on the service account used by the Prefect Cloud Run V2 worker, and (if different) the SA used to run the Cloud Run Job In parallel, I recommend: - Upgrading Prefect and prefect-gcp as shown above and redeploying the images - Verifying the worker SA has roles/run.admin and roles/iam.serviceAccountUser on the job’s runtime SA
- Ensuring the region and project values in your worker configuration match the target Cloud Run Job If you share the exact HttpError code/message and your version/IAM specifics, I can pinpoint the minimal IAM needed and confirm whether anything else needs adjusting.