Skip to content

Bound HTTPEmbedder requests with a timeout and honor context cancellation - #16

Closed
bfs2021 wants to merge 1 commit into
priompt:mainfrom
bfs2021:fix/embedder-timeout-context
Closed

bfs2021 wants to merge 1 commit into
priompt:mainfrom
bfs2021:fix/embedder-timeout-context

Conversation

@bfs2021

@bfs2021 bfs2021 commented Nov 15, 2026

Copy link
Copy Markdown

Problem

HTTPEmbedder sent every embeddings request through http.DefaultClient, which has no timeout, and the Embedder interface carried no context.Context. If the embeddings endpoint accepted the connection but never answered, Analyze blocked forever and the caller's deadline or cancellation could not stop it. Fixes #14.

Root cause

semdiff/semdiff.go built the request with http.NewRequest and sent it with http.DefaultClient.Do, so neither a request deadline nor the caller's cancellation reached the outbound HTTP call, and Embed(texts []string) had no context parameter to propagate one.

Fix

  • Added HTTPEmbedder.Timeout time.Duration (zero means the package default of 30s). Embed now derives a deadline from it via context.WithTimeout, so both the round trip and the response body read are bounded.
  • Added an opt-in ContextEmbedder interface (EmbedContext(ctx, texts)) plus AnalyzeContext(ctx, ...), and HTTPEmbedder implements it with http.NewRequestWithContext. Analyze stays as a wrapper around context.Background(), so existing Embedder implementations (LexicalEmbedder, the server, promptctl) compile unchanged.
  • Requests now go through a shared package-level http.Client instead of http.DefaultClient; the client sets no timeout of its own because the per-request deadline lives in the context.
  • Threaded the context through the internal analyzeChange / expand / deltaText / embedOne path.

Testing

  • Added TestEmbedderTimeout (the repro from the issue, with a 100ms configured timeout): it fails on the old code with Embed still blocked after 5s: no timeout and passes after the fix.
  • Added TestEmbedderDefaultTimeout (zero value resolves to a finite timeout) and TestAnalyzeContextCancellation (cancelling the context aborts the in-flight request with context.Canceled).
  • Existing tests updated only to pass a context into the internal deltaText helper.
  • go test ./... green on Windows/amd64, Go 1.27.3; go vet ./... clean; gofmt -l semdiff/ empty. CI runs go test ./... and buf lint; proto files untouched.

HTTPEmbedder sent every request with http.DefaultClient, which has no
timeout, and the Embedder interface took no context, so a hung embeddings
endpoint blocked Analyze forever.

Add a Timeout field (zero = 30s default) enforced through the request
context, and an opt-in ContextEmbedder interface plus AnalyzeContext so
callers can cancel an in-flight request without breaking existing
Embedder implementations. Analyze remains a wrapper around
context.Background().
@sujal-maheshwari2004

Copy link
Copy Markdown
Member

Hi @bfs2021, thank you for taking the time to send this. We're closing it, and it's not about the quality of the change.

The issue it fixes is labelled good first issue. In this organisation those are reserved for people making their first open-source contributions: we're onboarding a group of students who will learn the workflow on exactly these issues. We've now written this down in our contribution guide.

If you'd like to keep contributing, we'd welcome help on our help wanted issues. That's where experienced and AI-assisted effort makes a real difference. Please claim one by commenting, wait to be assigned before opening a pull request, and keep to at most two open pull requests at a time.

Thanks again!

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.

HTTPEmbedder has no timeout and ignores request context

2 participants