Commit b8fcc8b
Ran into #6057 while setting up a VertexAI provider — the server would
crash with `RuntimeError: Event loop is closed` on the first inference
request after startup.
Turns out the issue is that during `StackApp.__init__`, the stack
initialization runs in a temporary event loop, and
`refresh_registry_once()` triggers model listing which calls
`_get_client()` on the VertexAI adapter. The Google genai `Client`
eagerly creates an `httpx.AsyncClient` internal to itself, binding it to
that temporary loop. After the temp loop goes away and uvicorn starts on
a fresh loop, the cached client is still holding connections tied to the
dead loop.
Two things in this PR:
1. Added `_reset_client()` on `VertexAIInferenceAdapter` — clears the
cached default client and HTTP options. This is called from
`StackApp.__init__` right after `reset_sqlstore_engines()`, following
the exact same pattern that already exists for SQL engines.
2. Added a safety check in `_get_client()` itself — before returning the
cached default client, it checks whether the underlying httpx transport
has been closed. If it has (which happens when the event loop it was
created on is terminated), it logs and recreates the client. This is
defense-in-depth in case the reset isn't called.
Not entirely sure about the `is_closed` check — it relies on httpx's
internal state tracking which seems stable across recent versions but
could change. Happy to remove that part if you'd prefer to keep it
simpler.
## Test Plan
Ran `python3.12 -m py_compile` on both modified files — they compile
cleanly. The existing test suite should cover the normal code paths
since these changes only affect the initialization/recreation path. The
event loop simulation is tricky to unit test without bringing up a full
server, but the pattern mirrors the tested `reset_sqlstore_engines()`
flow exactly.<hr>This is an automatic backport of pull request #6072
done by [Mergify](https://mergify.com).
Signed-off-by: goingforstudying-ctrl <goingforstudying-ctrl@users.noreply.github.com>
Co-authored-by: goingforstudying-ctrl <goingforstudying@gmail.com>
Co-authored-by: goingforstudying-ctrl <goingforstudying-ctrl@users.noreply.github.com>
1 parent 557fed3 commit b8fcc8b
2 files changed
Lines changed: 58 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
147 | 147 | | |
148 | 148 | | |
149 | 149 | | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
150 | 161 | | |
151 | 162 | | |
152 | 163 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
202 | 202 | | |
203 | 203 | | |
204 | 204 | | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
205 | 228 | | |
206 | 229 | | |
207 | 230 | | |
| |||
315 | 338 | | |
316 | 339 | | |
317 | 340 | | |
318 | | - | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
319 | 365 | | |
320 | 366 | | |
321 | 367 | | |
| |||
0 commit comments