Skip to content

rpcclient: add WithContext methods for context-aware HTTP cancellation#17

Open
simonzg wants to merge 1 commit intomasterfrom
simonzg/context-support
Open

rpcclient: add WithContext methods for context-aware HTTP cancellation#17
simonzg wants to merge 1 commit intomasterfrom
simonzg/context-support

Conversation

@simonzg
Copy link
Copy Markdown

@simonzg simonzg commented Mar 19, 2026

https://itbitwiki.atlassian.net/browse/CHAINS-6696

Summary

  • Adds ctx context.Context field to jsonRequest struct and uses http.NewRequestWithContext in handleSendPostMessage, allowing callers to cancel in-flight HTTP requests via context
  • Adds early bail-out on context cancellation during retry backoff
  • New methods: SendCmdWithContext, RawRequestWithContext, RawRequestAsyncWithContext, and WithContext variants for GetBlockCount, GetBlockHash, GetBlockVerbose, GetBlockVerboseTx, EstimateSmartFee, GetRawTransactionVerbose, CreateRawTransaction, SendRawTransaction, GetTransaction, GetNetworkInfo
  • All original methods remain unchanged (backwards-compatible)

Context

BTC-family RPC clients leak goroutines when RPCs hang. The timeout.RunWithTimeout() wrapper creates a goroutine for each RPC call, but when the timeout fires, the goroutine keeps running because the underlying HTTP request can't be cancelled. This change adds context support so that when RunWithTimeout cancels the context, the underlying HTTP request is immediately aborted.

Adds a ctx field to jsonRequest and uses http.NewRequestWithContext
in handleSendPostMessage, allowing callers to cancel in-flight HTTP
requests via context. Also adds early bail-out on context cancellation
during retry backoff.

New methods: SendCmdWithContext, RawRequestWithContext,
RawRequestAsyncWithContext, and WithContext variants for
GetBlockCount, GetBlockHash, GetBlockVerbose, GetBlockVerboseTx,
EstimateSmartFee, GetRawTransactionVerbose, CreateRawTransaction,
SendRawTransaction, GetTransaction, GetNetworkInfo.

CHAINS-6696

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

1 participant