Skip to content

fix(async): preserve caller context for message requests - #431

Open
dajiaohuang wants to merge 1 commit into
anthropics:mainfrom
dajiaohuang:fix/async-message-context-propagation
Open

dajiaohuang wants to merge 1 commit into
anthropics:mainfrom
dajiaohuang:fix/async-message-context-propagation

Conversation

@dajiaohuang

Copy link
Copy Markdown

Summary

Fixes #430. The main and beta async message services composed prepareAsync() with thenComposeAsync() at each of their three request call sites. With the current completed request-preparation future, that schedules HttpClient.executeAsync() on ForkJoinPool.commonPool() and drops caller thread-local context before transport instrumentation observes the request.

Use non-async thenCompose() for create, streaming create, and count-tokens requests in both services. This avoids the implicit common-pool hop and preserves the preparation/completion thread's context; streamHandlerExecutor remains dedicated to stream callbacks.

Validation

  • Added six synthetic tests using a fake HttpClient and a ThreadLocal; they cover all three methods in both main and beta services.
  • On frozen snapshot base afb2c556f90c2da15f5ebd63ee7be604cf59edaf, the two streaming regressions failed before the fix (context was null) and all six tests passed after it.
  • Rebasing onto current main (c0f5915f7dd34cef8d5f03fdc8cdd4f93933c47e) succeeded. A post-rebase focused Gradle run timed out after 600 seconds during :anthropic-java-core:compileTestKotlin; no test result was produced by that attempt. No provider API or external test endpoint was used.

@dajiaohuang
dajiaohuang requested a review from a team as a code owner October 24, 2026 08:05

This branch has not been deployed

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

Labels

None yet

Projects

None yet

1 participant