Skip to content

fix: bind post-error proxy verification to caller lifetime, not a detached context - #80

Closed
SK-DEV-AI wants to merge 1 commit into
jasonxu114514:mainfrom
SK-DEV-AI:pr/proxy-verify-cancel
Closed

SK-DEV-AI wants to merge 1 commit into
jasonxu114514:mainfrom
SK-DEV-AI:pr/proxy-verify-cancel

Conversation

@SK-DEV-AI

Copy link
Copy Markdown
Contributor

verifyProxyAfterError detaches its health check with context.WithoutCancel, so verifications outlive client disconnects and server shutdown: each runs the full 10s against a dead network while holding the checking flag, serializing later checks behind pointless work.

WithTimeout on the caller context keeps the timeout cap for hung checks; cancellation only ends checks nobody waits for.

Validation: go vet clean, go build -trimpath clean, gofmt clean. Failure-path only.

…ached context

verifyProxyAfterError detached its health check with
context.WithoutCancel, so verifications outlived client disconnects and
server shutdown: each ran the full 10s against a dead network while
holding the checking flag, serializing later checks behind pointless
work. WithTimeout on the caller context keeps the timeout cap for hung
checks; cancellation only ends checks nobody waits for.
@jasonxu114514

Copy link
Copy Markdown
Owner

Thanks for looking into this. I don't think we can take it as-is, because it reintroduces the failure WithoutCancel was there to prevent:

  1. The timeout is already applied. checkClaimedProxy wraps the context in its own context.WithTimeout(ctx, timeout) (pool.go:222), so a hung check is already capped at 10s.
  2. Cancelled requests make the check a no-op. ctx here is the request context. The handler usually returns right after the error, which cancels the check almost at once. context.Canceled is not a proxy failure, so the result is inconclusive. After rebindFailedProxy has marked the proxy unhealthy, it then stays out until the next 15-minute scheduled check, instead of being restored within seconds.
  3. It can evict a healthy proxy. The derived context inherits the request deadline. If that deadline fires mid-check, the check fails with context.DeadlineExceeded. isProxyFailure counts that as a proxy failure, so applyProxyHealthResult rebinds the keys away from a proxy that was fine. The only thing that ran out was the client's budget.

If the goal is to stop checks from outliving server shutdown, that's reasonable, but it should hang off the gateway's lifetime context (the ctx given to StartProxyHealthChecks), not the request's. Happy to take a PR along those lines.

@SK-DEV-AI

Copy link
Copy Markdown
Contributor Author

Closing per maintainer feedback: binding the check to the request context reintroduces the failure WithoutCancel prevents (cancelled requests make the check a no-op, and a fired request deadline can evict a healthy proxy). The right fix hangs off the gateway lifetime context, not the request's — will send that as a fresh PR if we get to it. Keeping our variant locally in the meantime.

@SK-DEV-AI SK-DEV-AI closed this Oct 15, 2026
SK-DEV-AI added a commit to SK-DEV-AI/opencode2api that referenced this pull request Oct 15, 2026
…ver-key usage)

Takes upstream f8e996d/be91f86/0204ca1 (client fingerprint + inference
endpoint + genuine tools), jasonxu114514#59 anon cooldown lockup, jasonxu114514#63 token floor,
jasonxu114514#64 whole-attempt deadline, jasonxu114514#62 tool images, jasonxu114514#58 server-key usage
split, jasonxu114514#65/jasonxu114514#66 models surface, plus our merged jasonxu114514#76/jasonxu114514#77/jasonxu114514#78.

Kept local (fork-owned, still no upstream equivalent): zero-delivery
replay + length rescue + SSE heartbeat + stop/tail census (stream
lanes), offline no_upstream_contact probe guard, single-marshal Zen
shaping with paid-model gate, tcp4 dial + TLS resumption, RandomID
math/rand fallback, cancel-detached refresh fix (PR jasonxu114514#80 superseded by
maintainer feedback; kept locally pending lifetime-context rework).
Telemetry takes upstream's session-burn + server-key split with their
LRU session cap (better than our smallest-first); CloneMap takes
upstream's merged comment. Metrics ring test retargeted at upstream's
concrete ring types. doUpstream keeps upstream's 3-value signature;
replay uses a new doUpstreamCounted wrapper for continuous legs.
SK-DEV-AI added a commit to SK-DEV-AI/opencode2api that referenced this pull request Oct 15, 2026
SK-DEV-AI added a commit to SK-DEV-AI/opencode2api that referenced this pull request Oct 17, 2026
Upstream rewrote the anonymous lane: doAnonymousUpstream now walks an
ordered upstream list per proxy and shapes the body per gate
(prepareAnonymousBody(body, protocol, legacy) for the legacy /zen/v1
origin vs the newer /inference origin), replacing the map-level
shapeAnonymousPayload. Both lanes were measured live from this box and
answer 200 for anonymous free-tier traffic, so the fork keeps upstream's
lane order.

Fork behaviour preserved:
- zero-delivery stream replay with attempt-count continuation
  (doUpstreamCounted restored on top of upstream's 3-value doUpstream,
  replay body wrapped in RenameStreamReader so tool aliases stay correct)
- SSE heartbeat, stop/tail census (ForwardStream/TranscodeStream outcome
  plumbing), length rescue on partial reset
- 499 client-cancel reporting in both the chat and System One paths
- offline probe guard (gotResponse) so a boot with no route reports
  no_upstream_contact instead of disabling a working free model
- tcp4 dial pin + TLS session resumption, merged with upstream's pool
  transport rules (ForceAttemptHTTP2/DisableKeepAlives/TLSNextProto)
- RandomID math/rand fallback when the host RNG fails

Fork behaviour retired (converged back to upstream):
- request-bound proxy health check (upstream PR jasonxu114514#80 was closed on this:
  proxy health is gateway-scoped, so a cancelled request must not abort
  the check); refresh_test now pins that semantics
- anonymousUpstreams only leads with the hardcoded public legacy origin
  when upstream.anonymous lives on the same host, so a custom mirror or
  test server is never bypassed toward a third-party origin

Gates: gofmt clean, go vet clean, go test ./... green, go build
-trimpath ./cmd/opencode2api.
SK-DEV-AI added a commit to SK-DEV-AI/opencode2api that referenced this pull request Oct 17, 2026
…h variant

- FORK.md: v1.4.1 merge section (took upstream's per-lane anon shaping,
  kept fork replay/heartbeat/census/probe-guard/transport pins, retired the
  request-bound proxy health check after PR jasonxu114514#80 closed), fresh fork header
  (upstream v1.4.1 a9470df, merge history, backups), live health numbers,
  explicit push-remote note.
- AGENT.md: HEAD + backup state, finding D superseded with the gateway-
  scoped reasoning, new finding E for the anonymous lane list, request path
  and attempt-loop shape post-merge (doUpstreamCounted, per-lane shaping,
  guarded 499 block).
- ZEN-FREE-TIER.md: the two anonymous lanes and their different tool bars.
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.

2 participants