[Bug]: Codex response adapter does not close per-request HTTP clients, causing descriptor accumulation
Issue Origin
Observed or reproduced in a real environment
Bug Description
CodexCompletionsAdapter._create_response() creates a fresh HTTP client through self._client_factory() for each request. Its finally block closes the response stream but never closes the owning client. The HTTP 401 retry path also replaces the first client without closing it.
The production factory in CodexVLM._build_completions_adapter() creates a new openai.OpenAI client, so these are per-request clients, not shared externally owned clients.
Affected files:
- openviking/models/vlm/backends/codex_responses_adapter.py
- openviking/models/vlm/backends/codex_vlm.py
Steps to Reproduce
- Run the synthetic reproduction below: the response stream is closed but its owning client is not. No network or credentials are needed.
- For an integration reproduction, configure the openai-codex VLM provider and process a sufficiently large synthetic document collection.
- Observe open network descriptors over repeated requests.
- Compare with deterministic cleanup of every created client, including the 401 retry client.
Expected Behavior
Close every per-request HTTP client on successful completion, request creation failure, stream iteration failure, and both successful and failed HTTP 401 retries. Preserve stream cleanup. Resource use should remain bounded as requests complete.
Actual Behavior
Repeated large semantic-processing jobs failed after several minutes with VLMConfig validation reporting unavailable Codex OAuth credentials, although the credential file was present and unexpired; small jobs succeeded.
In a diagnostic run, open descriptors grew from approximately 66 to over 200, predominantly network sockets. After adding deterministic client cleanup, descriptors remained approximately 75–87 over six minutes of sampling and processing continued beyond the previous failure window.
The original failing process descriptor limit was not captured. Descriptor exhaustion as the cause of that exact error is therefore an inference supported by connection growth, the isolated experiment below, and before/after behavior.
Minimal Reproducible Example
from types import SimpleNamespace
from openviking.models.vlm.backends.codex_responses_adapter import CodexCompletionsAdapter
class Stream:
def __init__(self):
self.closed = False
def __iter__(self):
return iter([])
def close(self):
self.closed = True
class Client:
def __init__(self):
self.closed = False
self.stream = Stream()
self.responses = SimpleNamespace(create=lambda **kwargs: self.stream)
def close(self):
self.closed = True
client = Client()
adapter = CodexCompletionsAdapter(lambda: client, "test-model")
adapter.create(messages=[{"role": "user", "content": "test"}])
assert client.stream.closed
assert client.closed # Fails before the fix.
Error Logs
Sanitized error summary: VLMConfig validation failed because Codex OAuth credentials were reported unavailable.
A separate isolated-process experiment lowered RLIMIT_NOFILE, opened /dev/null until EMFILE, then called has_codex_auth_available(): True before exhaustion, False while exhausted, True after closing descriptors. No credential content was logged or modified.
OpenViking Version
0.5.1.1
Python Version
3.14
Operating System
macOS
Model Backend
Other
Additional Context
Suggested fix: wrap the complete request lifecycle in contextlib.ExitStack and register client.close immediately after creating each client, including the auth-retry client. Keep the existing stream cleanup. Adding cleanup only to the stream-iteration finally block would miss failures before a stream is returned.
Local patch validation: five focused tests passed (success, request creation failure, stream failure, successful 401 retry, failed 401 retry). After restart, live processing ran for more than six minutes and completed 495 embedding operations with zero new queue errors; retrieval of previously indexed content succeeded. The full corpus had not finished indexing at validation time.
VLM provider: openai-codex.
Related but distinct: #91 (AGFS port-check socket leak), #4539 (external OAuth token rotation/resynchronization).
This report contains synthetic examples and aggregate diagnostics only; no local corpus content, absolute local paths, task identifiers, account identifiers, tokens, or raw logs.
Implemented in #6416: the adapter now owns every client it creates and closes it through contextlib.ExitStack, including the refreshed client used for the 401 retry, while the stream is still closed first.