Commit a69f861
fix: fix lifecycle issues with credentials in BigQuery Agent Analytics Plugin
### Changes
**1. Pickle safety — try-preserve, fallback-drop**
`__getstate__` tests whether `_user_credentials` is picklable:
- **Picklable** (service-account, `AnonymousCredentials`): preserved — survives pickle and restored via `__setstate__` so the plugin uses the user's identity after unpickle.
- **Non-picklable** (`compute_engine.Credentials` with `requests.Session`): dropped gracefully — falls back to ADC after unpickle.
`_credentials` (the active/resolved credentials) is always cleared since it may hold resolved ADC state. On unpickle, `__setstate__` restores it from `_user_credentials` when available.
**2. Fork safety — documented user-provided credential limitation**
`_reset_runtime_state()` sets `_credentials = _user_credentials`. For ADC-resolved credentials (`_user_credentials is None`), this clears stale credentials for re-resolution. For user-provided credentials, the original object is kept — we cannot re-create it. The comment documents this: the user is responsible for providing fork-safe credentials.
**3. GCS client — credentials passed correctly**
`GCSOffloader.__init__` always creates a `storage.Client` eagerly (`storage_client or storage.Client(...)` at line 1329). This was also true before the credentials commit. The fix passes explicit credentials when available and lets ADC resolve when not, matching the `bigquery.Client` and `BigQueryWriteAsyncClient` patterns.
**4. Benign race on `_credentials` resolution documented**
When multiple event loops call `_create_loop_state()` concurrently, both can resolve ADC redundantly. This is idempotent and benign, now documented.
Co-authored-by: Haiyuan Cao <haiyuan@google.com>
PiperOrigin-RevId: 9051110761 parent b848390 commit a69f861
2 files changed
Lines changed: 87 additions & 3 deletions
File tree
- src/google/adk/plugins
- tests/unittests/plugins
Lines changed: 37 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1988 | 1988 | | |
1989 | 1989 | | |
1990 | 1990 | | |
| 1991 | + | |
1991 | 1992 | | |
1992 | 1993 | | |
1993 | 1994 | | |
| |||
2106 | 2107 | | |
2107 | 2108 | | |
2108 | 2109 | | |
| 2110 | + | |
| 2111 | + | |
| 2112 | + | |
| 2113 | + | |
2109 | 2114 | | |
2110 | 2115 | | |
2111 | 2116 | | |
| |||
2196 | 2201 | | |
2197 | 2202 | | |
2198 | 2203 | | |
| 2204 | + | |
| 2205 | + | |
| 2206 | + | |
| 2207 | + | |
| 2208 | + | |
| 2209 | + | |
| 2210 | + | |
2199 | 2211 | | |
2200 | 2212 | | |
2201 | 2213 | | |
2202 | 2214 | | |
2203 | | - | |
2204 | | - | |
2205 | | - | |
| 2215 | + | |
2206 | 2216 | | |
2207 | 2217 | | |
2208 | 2218 | | |
| |||
2536 | 2546 | | |
2537 | 2547 | | |
2538 | 2548 | | |
| 2549 | + | |
| 2550 | + | |
| 2551 | + | |
| 2552 | + | |
| 2553 | + | |
| 2554 | + | |
| 2555 | + | |
| 2556 | + | |
| 2557 | + | |
| 2558 | + | |
| 2559 | + | |
| 2560 | + | |
2539 | 2561 | | |
2540 | 2562 | | |
2541 | 2563 | | |
2542 | 2564 | | |
2543 | 2565 | | |
2544 | 2566 | | |
2545 | 2567 | | |
| 2568 | + | |
| 2569 | + | |
| 2570 | + | |
| 2571 | + | |
| 2572 | + | |
| 2573 | + | |
| 2574 | + | |
2546 | 2575 | | |
2547 | 2576 | | |
2548 | 2577 | | |
| |||
2597 | 2626 | | |
2598 | 2627 | | |
2599 | 2628 | | |
| 2629 | + | |
| 2630 | + | |
| 2631 | + | |
| 2632 | + | |
| 2633 | + | |
2600 | 2634 | | |
2601 | 2635 | | |
2602 | 2636 | | |
| |||
Lines changed: 50 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2093 | 2093 | | |
2094 | 2094 | | |
2095 | 2095 | | |
| 2096 | + | |
| 2097 | + | |
| 2098 | + | |
| 2099 | + | |
| 2100 | + | |
| 2101 | + | |
| 2102 | + | |
| 2103 | + | |
| 2104 | + | |
| 2105 | + | |
| 2106 | + | |
| 2107 | + | |
| 2108 | + | |
| 2109 | + | |
| 2110 | + | |
| 2111 | + | |
| 2112 | + | |
| 2113 | + | |
| 2114 | + | |
| 2115 | + | |
| 2116 | + | |
| 2117 | + | |
| 2118 | + | |
| 2119 | + | |
| 2120 | + | |
| 2121 | + | |
| 2122 | + | |
| 2123 | + | |
| 2124 | + | |
| 2125 | + | |
| 2126 | + | |
| 2127 | + | |
| 2128 | + | |
| 2129 | + | |
| 2130 | + | |
| 2131 | + | |
| 2132 | + | |
| 2133 | + | |
| 2134 | + | |
| 2135 | + | |
| 2136 | + | |
| 2137 | + | |
| 2138 | + | |
| 2139 | + | |
| 2140 | + | |
| 2141 | + | |
| 2142 | + | |
| 2143 | + | |
| 2144 | + | |
| 2145 | + | |
2096 | 2146 | | |
2097 | 2147 | | |
2098 | 2148 | | |
| |||
0 commit comments