Title: BrokenPipeError when using Attached Service Account on GCE Instance · Issue #2553 · googleapis/google-api-python-client · GitHub
Open Graph Title: BrokenPipeError when using Attached Service Account on GCE Instance · Issue #2553 · googleapis/google-api-python-client
X Title: BrokenPipeError when using Attached Service Account on GCE Instance · Issue #2553 · googleapis/google-api-python-client
Description: Environment details OS type and version: Google Compute Engine (GCE) with Container-Optimized OS Python version: Python 3.7.16 pip version: pip 21.3.1 google-api-python-client== 1.12.8 google-auth==1.35.0 google-auth-httplib2==0.2.0 Step...
Open Graph Description: Environment details OS type and version: Google Compute Engine (GCE) with Container-Optimized OS Python version: Python 3.7.16 pip version: pip 21.3.1 google-api-python-client== 1.12.8 google-auth=...
X Description: Environment details OS type and version: Google Compute Engine (GCE) with Container-Optimized OS Python version: Python 3.7.16 pip version: pip 21.3.1 google-api-python-client== 1.12.8 google-auth=...
Opengraph URL: https://github.com/googleapis/google-api-python-client/issues/2553
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"BrokenPipeError when using Attached Service Account on GCE Instance","articleBody":"#### Environment details\n\n - OS type and version: Google Compute Engine (GCE) with Container-Optimized OS \n - Python version: `Python 3.7.16` \n - pip version: `pip 21.3.1` \n - `google-api-python-client`== 1.12.8\n - `google-auth`==1.35.0\n - `google-auth-httplib2`==0.2.0\n\n#### Steps to reproduce\n\n 1. Deploy a Python application on a GCE instance with an attached service account. \n 2. Use `google.auth.default()` for authentication. \n 3. Let the service run for a few hours to allow the token to expire. \n 4. Perform a Google Drive API call during token refresh (e.g., listing files or creating a folder). \n\n#### Code example\n\n```python\ngdrive_credentials: Credentials = (\n Credentials.from_service_account_info(\n gcp_credentials_dict,\n scopes=SCOPES,\n )\n if gcp_credentials_dict\n else google.auth.default(scopes=SCOPES)[0]\n)\n\ndef create_services(credentials, scopes):\n drive_service = build(\n \"drive\", \"v3\", credentials=credentials\n )\n docs_service = build(\n \"docs\", \"v1\", credentials=credentials\n )\n\n return credentials, drive_service, docs_service\n\ndef get_or_create_folder(self, drive_service, folder_name):\n \"\"\"\n Checks if a folder exists in the root directory and creates it if not.\n\n :param drive_service: Authorized Drive API client.\n :param folder_name: The name of the folder to check or create.\n :return: The folder ID.\n \"\"\"\n # Search for the folder in the root directory\n query = f\"name = '{folder_name}' and mimeType = 'application/vnd.google-apps.folder' and 'root' in parents and trashed = false\"\n response = (\n drive_service.files().list(q=query, fields=\"files(id, name)\").execute()\n )\n files = response.get(\"files\", [])\n\n if files:\n # Folder exists, return its ID\n return files[0][\"id\"]\n else:\n # Folder doesn't exist, create it\n folder_metadata = {\n \"name\": folder_name,\n \"mimeType\": \"application/vnd.google-apps.folder\",\n \"parents\": [\"root\"], # Place in the root directory\n }\n folder = (\n drive_service.files()\n .create(body=folder_metadata, fields=\"id\")\n .execute()\n )\n return folder[\"id\"]\n```\n\n#### Stacktrace\n\n```bash\nBrokenPipeError: [Errno 32] Broken pipe\n File \"/app/endpoints/webhooks.py\", line 292, in get_services_status\n current_app.config.get(\"BUILD_ENVIRONMENT\"),\n File \"/app/middleware/gdocsprinter.py\", line 43, in print_file_with_template\n template_id, gdocs_drive_service, replace_template, build_environment\n File \"/app/middleware/gdocsprinter.py\", line 111, in create_template_copy\n drive_service, f\"{build_environment}-prints\"\n File \"/app/middleware/gdocsprinter.py\", line 86, in get_or_create_folder\n drive_service.files().list(q=query, fields=\"files(id, name)\").execute()\n File \"googleapiclient/_helpers.py\", line 134, in positional_wrapper\n return wrapped(*args, **kwargs)\n File \"googleapiclient/http.py\", line 909, in execute\n headers=self.headers,\n File \"googleapiclient/http.py\", line 204, in _retry_request\n raise exception\n File \"googleapiclient/http.py\", line 177, in _retry_request\n resp, content = http.request(uri, method, *args, **kwargs)\n File \"google_auth_httplib2.py\", line 190, in request\n self._request, method, uri, request_headers)\n File \"/usr/local/lib/python3.7/site-packages/google/auth/credentials.py\", line 133, in before_request\n self.refresh(request)\n File \"/usr/local/lib/python3.7/site-packages/google/auth/compute_engine/credentials.py\", line 111, in refresh\n self._retrieve_info(request)\n File \"/usr/local/lib/python3.7/site-packages/google/auth/compute_engine/credentials.py\", line 88, in _retrieve_info\n request, service_account=self._service_account_email\n File \"/usr/local/lib/python3.7/site-packages/google/auth/compute_engine/_metadata.py\", line 234, in get_service_account_info\n return get(request, path, params={\"recursive\": \"true\"})\n File \"/usr/local/lib/python3.7/site-packages/google/auth/compute_engine/_metadata.py\", line 150, in get\n response = request(url=url, method=\"GET\", headers=_METADATA_HEADERS)\n File \"google_auth_httplib2.py\", line 117, in __call__\n url, method=method, body=body, headers=headers, **kwargs)\n File \"__init__.py\", line 1709, in request\n conn, authority, uri, request_uri, method, body, headers, redirections, cachekey,\n File \"__init__.py\", line 1424, in _request\n (response, content) = self._conn_request(conn, request_uri, method, body, headers)\n File \"__init__.py\", line 1347, in _conn_request\n conn.request(method, request_uri, body, headers)\n File \"http/client.py\", line 1281, in request\n self._send_request(method, url, body, headers, encode_chunked)\n File \"http/client.py\", line 1327, in _send_request\n self.endheaders(body, encode_chunked=encode_chunked)\n File \"http/client.py\", line 1276, in endheaders\n self._send_output(message_body, encode_chunked=encode_chunked)\n File \"http/client.py\", line 1036, in _send_output\n self.send(msg)\n File \"http/client.py\", line 997, in send\n self.sock.sendall(data)\n```\n\n#### Brief summary\n\nAfter switching the authentication method for our application from service account keys to an attached service account on a Google Compute Engine (GCE) instance, we are encountering a BrokenPipeError during API calls to Google Drive.\n\nThe issue only occurs during token refresh after the application has been running for a couple of hours. Following the first failure, the service works again without intervention.\n\nThanks\n","author":{"url":"https://github.com/dantebarba","@type":"Person","name":"dantebarba"},"datePublished":"2025-01-21T16:25:37Z","url":"https://github.com/googleapis/google-api-python-client/issues/2553"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:634078e4-71b8-b09d-7cef-a3f122ae24b4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 90EE:32975D:20D9A:2CA58:6B0093A7 |
| html-safe-nonce | bf3f593bc457a0485fb2746509d43bb567a8f2ba63c403f7fb2e7db3e16f2904 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MEVFOjMyOTc1RDoyMEQ5QToyQ0E1ODo2QjAwOTNBNyIsInZpc2l0b3JfaWQiOiI1MDYzNTA1NzMwNjc0Mzk4MTE5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 0b9e71c894f285d5c2d29af7108f009fd86cf32df9d686f8fc46fd8be305836c |
| hovercard-subject-tag | issue:2802294434 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/googleapis/google-api-python-client/2553/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ba779956a1276f040134d04427d65d6d4e837ce16590387b6a5c1fbcc3dbb53e/googleapis/google-api-python-client/issues/2553 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ba779956a1276f040134d04427d65d6d4e837ce16590387b6a5c1fbcc3dbb53e/googleapis/google-api-python-client/issues/2553 |
| og:image:alt | Environment details OS type and version: Google Compute Engine (GCE) with Container-Optimized OS Python version: Python 3.7.16 pip version: pip 21.3.1 google-api-python-client== 1.12.8 google-auth=... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dantebarba |
| hostname | github.com |
| expected-hostname | github.com |
| None | c710a5cb5c47be60abfe6e8410436d409906979dbed614ef09f29c60f5535b08 |
| turbo-cache-control | no-preview |
| go-import | github.com/googleapis/google-api-python-client git https://github.com/googleapis/google-api-python-client.git |
| octolytics-dimension-user_id | 16785467 |
| octolytics-dimension-user_login | googleapis |
| octolytics-dimension-repository_id | 15749269 |
| octolytics-dimension-repository_nwo | googleapis/google-api-python-client |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 15749269 |
| octolytics-dimension-repository_network_root_nwo | googleapis/google-api-python-client |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | be616a2a6ec1c609d6e002896dff5352d5c5f28b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width