Skip to content

Fix context cancellation in Anthropic usage reporting goroutine#244

Merged
AnnatarHe merged 1 commit intomainfrom
claude/fix-daemon-timer-error-m4lPu
Feb 26, 2026
Merged

Fix context cancellation in Anthropic usage reporting goroutine#244
AnnatarHe merged 1 commit intomainfrom
claude/fix-daemon-timer-error-m4lPu

Conversation

@AnnatarHe
Copy link
Contributor

@AnnatarHe AnnatarHe commented Feb 26, 2026

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

  • Wrapped the sendAnthropicUsageToServer call in a separate goroutine with its own background context instead of reusing the caller's context
  • Added a 10-second timeout to the background context to ensure the operation completes in a reasonable timeframe
  • Added explanatory comment clarifying the intent to decouple the goroutine from the caller's context lifecycle

Implementation Details

The change converts a fire-and-forget operation from:

go s.sendAnthropicUsageToServer(ctx, usage)

To:

go func() {
    bgCtx, bgCancel := context.WithTimeout(context.Background(), 10*time.Second)
    defer bgCancel()
    s.sendAnthropicUsageToServer(bgCtx, usage)
}()

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


Open with Devin

…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
@chatgpt-codex-connector
Copy link

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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

  • Context Management: Wrapped the sendAnthropicUsageToServer call in a separate goroutine, utilizing its own background context instead of reusing the caller's context.
  • Timeout Implementation: Added a 10-second timeout to the newly created background context to ensure the operation completes within a reasonable timeframe.
  • Code Clarity: Included an explanatory comment to clarify the intent of decoupling the goroutine from the caller's context lifecycle.
Changelog
  • daemon/cc_info_timer.go
    • The sendAnthropicUsageToServer function call was refactored into an anonymous goroutine.
    • A new context.WithTimeout was introduced to provide an independent context for the goroutine.
    • A defer bgCancel() statement was added to ensure proper resource cleanup for the new context.
    • An inline comment was added to explain the purpose of using a separate context.
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@AnnatarHe AnnatarHe merged commit 78750a2 into main Feb 26, 2026
2 checks passed
@AnnatarHe AnnatarHe deleted the claude/fix-daemon-timer-error-m4lPu branch February 26, 2026 07:56
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

To improve readability and maintainability, it's best to avoid using magic numbers. Consider defining 10 * time.Second as a named constant at the package level, for example sendAnthropicUsageTimeout. This makes the purpose of the timeout clearer and easier to change in the future.

@codecov
Copy link

codecov bot commented Feb 26, 2026

Codecov Report

❌ Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
daemon/cc_info_timer.go 0.00% 5 Missing ⚠️
Flag Coverage Δ
unittests 37.54% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
daemon/cc_info_timer.go 73.72% <0.00%> (-1.03%) ⬇️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

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