From ee638fedcba6d3c6c2e5984d1f2ce0dcbb977e24 Mon Sep 17 00:00:00 2001 From: gammazero <11790789+gammazero@users.noreply.github.com> Date: Mon, 27 Oct 2025 18:20:26 -1000 Subject: [PATCH 1/2] Propagate trace state when calling GetBlocks --- CHANGELOG.md | 1 + bitswap/client/client.go | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6991ba7a..3cb85a6c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ The following emojis are used to highlight certain changes: - `gateway`: Fixed duplicate peer IDs appearing in retrieval timeout error messages - `bitswap/client`: fix tracing by using context to pass trace and retrieval state to session [#1059](https://github.com/ipfs/boxo/pull/1059) + - Fix for `client.GetBlocks` ### Security diff --git a/bitswap/client/client.go b/bitswap/client/client.go index 53f2300b5..4319d6b01 100644 --- a/bitswap/client/client.go +++ b/bitswap/client/client.go @@ -24,7 +24,6 @@ import ( "github.com/ipfs/boxo/bitswap/tracer" blockstore "github.com/ipfs/boxo/blockstore" exchange "github.com/ipfs/boxo/exchange" - "github.com/ipfs/boxo/retrieval" rpqm "github.com/ipfs/boxo/routing/providerquerymanager" blocks "github.com/ipfs/go-block-format" "github.com/ipfs/go-cid" @@ -446,13 +445,10 @@ func (bs *Client) GetBlocks(ctx context.Context, keys []cid.Cid) (<-chan blocks. ctx, span := internal.StartSpan(ctx, "GetBlocks", trace.WithAttributes(attribute.Int("NumKeys", len(keys)))) defer span.End() - // Temporary session closed indepentendly of cancellation ctx. - sessCtx, cancelSession := context.WithCancel(context.Background()) + // Create a session context for the goroutine created here to use to close + // the session after reading all blocks. + sessCtx, cancelSession := context.WithCancel(ctx) - // Preserve retrieval.State from the original request context if present - if retrievalState := retrieval.StateFromContext(ctx); retrievalState != nil { - sessCtx = context.WithValue(sessCtx, retrieval.ContextKey, retrievalState) - } session := bs.sm.NewSession(sessCtx, bs.provSearchDelay, bs.rebroadcastDelay) blocksChan, err := session.GetBlocks(ctx, keys) @@ -461,6 +457,10 @@ func (bs *Client) GetBlocks(ctx context.Context, keys []cid.Cid) (<-chan blocks. return nil, err } + // The purpose of creating this goroutine to read the blocks from one + // channel and then put them on another channel, instead of simply + // returning the first channel, is to ensure that the session created here + // is closed after reading the blocks. out := make(chan blocks.Block) go func() { defer func() { From a7d795ad993a66067741260f099e62d555fb8eea Mon Sep 17 00:00:00 2001 From: gammazero <11790789+gammazero@users.noreply.github.com> Date: Mon, 27 Oct 2025 19:25:41 -1000 Subject: [PATCH 2/2] update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cb85a6c4..cf0434100 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,7 +30,7 @@ The following emojis are used to highlight certain changes: - `gateway`: Fixed duplicate peer IDs appearing in retrieval timeout error messages - `bitswap/client`: fix tracing by using context to pass trace and retrieval state to session [#1059](https://github.com/ipfs/boxo/pull/1059) - - Fix for `client.GetBlocks` + - `bitswap/client`: propagate trace state when calling GetBlocks [#1060](https://github.com/ipfs/boxo/pull/1060) ### Security