Fix context cancellation in Anthropic usage reporting goroutine#244
Fix context cancellation in Anthropic usage reporting goroutine#244
Conversation
…age goroutine The sendAnthropicUsageToServer goroutine was reusing the parent's ctx, which gets canceled via defer when fetchRateLimit returns — before the HTTP request can complete. Give the goroutine its own background context with a 10s timeout so it outlives the caller. https://claude.ai/code/session_01Aq7v3ogLxqe5XUM32rVqJY
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
Summary of ChangesHello @AnnatarHe, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a bug where the Anthropic usage reporting goroutine could be prematurely canceled if the caller's context was canceled. The change ensures reliable delivery of usage data for push notification scheduling by isolating the reporting logic within its own background context, complete with a timeout, thereby preventing its termination due to external context cancellations. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly fixes a bug where a background task for reporting usage could be prematurely canceled due to context propagation. The solution of creating a new detached context for the goroutine is appropriate. I have one suggestion to improve maintainability by replacing a hardcoded timeout value with a named constant.
| go s.sendAnthropicUsageToServer(ctx, usage) | ||
| // Use a separate context so the goroutine isn't canceled when the caller returns. | ||
| go func() { | ||
| bgCtx, bgCancel := context.WithTimeout(context.Background(), 10*time.Second) |
There was a problem hiding this comment.
Codecov Report❌ Patch coverage is
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
Summary
Fixed a bug where the Anthropic usage reporting goroutine could be prematurely canceled when the caller's context is canceled, preventing usage data from being sent to the server for push notification scheduling.
Key Changes
sendAnthropicUsageToServercall in a separate goroutine with its own background context instead of reusing the caller's contextImplementation Details
The change converts a fire-and-forget operation from:
To:
This ensures that usage data is reliably sent to the server for push notification scheduling, even if the original request context is canceled before the goroutine completes.
https://claude.ai/code/session_01Aq7v3ogLxqe5XUM32rVqJY