Skip to content

fix: use native fetch for Ollama embedding to ensure AbortController works#383

Open
jlin53882 wants to merge 1 commit intoCortexReach:masterfrom
jlin53882:fix/ollama-native-fetch-v2
Open

fix: use native fetch for Ollama embedding to ensure AbortController works#383
jlin53882 wants to merge 1 commit intoCortexReach:masterfrom
jlin53882:fix/ollama-native-fetch-v2

Conversation

@jlin53882
Copy link
Copy Markdown
Contributor

Problem

When using Ollama as the embedding provider, the \embedQuery\ timeout does not reliably abort stalled Ollama HTTP requests, causing the gateway-level 120s timeout.

Root Cause

OpenAI SDK HTTP client does not reliably abort Ollama TCP connections when AbortController.abort() fires in Node.js.

Fix

Use native \ etch\ for Ollama endpoints. Node.js 18+ native fetch correctly respects AbortController.

  • \isOllamaProvider(): detects localhost:11434 / 127.0.0.1:11434 / /ollama URLs
  • \embedWithNativeFetch(): calls Ollama via native fetch with proper signal handling
  • \embedWithRetry()\ routes Ollama URLs through embedWithNativeFetch()

Test

  • Test 8 added to cjk-recursion-regression.test.mjs
  • Assertion updated to match Embedding provider unreachable error (AliceLJY review fix)

Fixes #361.

…works

Root cause: OpenAI SDK HTTP client does not reliably abort Ollama
TCP connections when AbortController.abort() fires in Node.js. This
causes stalled sockets that hang until the gateway-level 120s timeout.

Fix: Add isOllamaProvider() to detect localhost:11434 endpoints, and
embedWithNativeFetch() using Node.js 18+ native fetch instead of the
OpenAI SDK. Native fetch properly closes TCP connections on abort.

Also adds Test 8 to cjk-recursion-regression.test.mjs with assertion
updated to match Embedding provider unreachable error (AliceLJY review fix).

Fixes CortexReach#361.
@jlin53882
Copy link
Copy Markdown
Contributor Author

Summary of Changes

This is a clean rebase of PR #362 addressing your review feedback:

Applied fixes from your CI diagnosis:

  • Assertion regex updated — added \Embedding provider unreachable\ to match the error that CI produces when no Ollama is running
  • Standalone test files removed — \pr354-standalone.mjs\ and \pr354-30iter.mjs\ are not included (they required a running Ollama instance)

Retained:

  • \embedder.ts: \isOllamaProvider()\ + \embedWithNativeFetch()\ + \embedWithRetry()\ routing
  • \cjk-recursion-regression.test.mjs: Test 8 (in CI-safe form)

Excluded:

Original PR #362 has been closed. CI should now pass. Thanks for the detailed diagnosis @AliceLJY!

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6782a1dfee

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

// Use an unreachable port + localhost so isOllamaProvider() returns true
// (URL contains 127.0.0.1:11434) but nothing actually listens there.
// This forces native fetch to properly reject, validating the Ollama path.
const ollamaBaseURL = "http://127.0.0.1:11434/v1";
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Point Ollama abort test at the local mock server

This test is meant to verify that abort signals propagate through the Ollama native-fetch path, but it hardcodes http://127.0.0.1:11434/v1 instead of using the baseURL from withServer. As written, the request usually fails immediately with connection refusal, so the slow handler (and its abort/destroy behavior) is never exercised; the test can pass even if abort propagation is broken.

Useful? React with 👍 / 👎.

@jlin53882
Copy link
Copy Markdown
Contributor Author

Note on CI failure: reflection-bypass-hook.test.mjs

The cli-smoke failure in this PR comes from test/reflection-bypass-hook.test.mjs, which is not part of our changes (this PR only touches src/embedder.ts and est/cjk-recursion-regression.test.mjs).

The test failures in reflection-bypass-hook are pre-existing on master. The error:

The input did not match /<derived-focus>/. Input: ''

appears to be caused by a date-based seed: the test uses \Date.UTC(2026, 2, 12)\ for seed data, but the current date is 2026-03-26+. This causes derived memory entries to exceed the age filter threshold and get filtered out, returning empty results.

Suggested fix (for a separate PR)

Update the hardcoded seed date in \ est/reflection-bypass-hook.test.mjs\ to a date within the active window (e.g., \Date.UTC(2026, 2, 26)\ or use a relative date).

I'll open a separate issue + PR for this. The Ollama fix itself (embedder.ts changes) is ready and unrelated to this failure.

@jlin53882
Copy link
Copy Markdown
Contributor Author

PR Chain Update

See #362 for the full chain of related issues and PRs:

Recommended: merge #385 before #383 to clear the CI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Ollama embedding AbortController doesn't abort — causes 120s gateway timeout

1 participant