Skip to content

Conversation

@gammazero
Copy link
Contributor

Closes #1009

@gammazero gammazero requested a review from a team as a code owner August 21, 2025 10:20
@gammazero gammazero marked this pull request as draft August 21, 2025 10:21
@codecov
Copy link

codecov bot commented Aug 21, 2025

Codecov Report

❌ Patch coverage is 91.66667% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 60.55%. Comparing base (f1390fd) to head (dacf423).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
bitswap/client/internal/session/session.go 87.50% 2 Missing and 1 partial ⚠️
bitswap/client/client.go 92.30% 1 Missing ⚠️

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1011      +/-   ##
==========================================
+ Coverage   60.49%   60.55%   +0.06%     
==========================================
  Files         267      268       +1     
  Lines       33386    33409      +23     
==========================================
+ Hits        20196    20232      +36     
+ Misses      11514    11502      -12     
+ Partials     1676     1675       -1     
Files with missing lines Coverage Δ
bitswap/client/internal/peermanager/peermanager.go 90.00% <100.00%> (ø)
...tswap/client/internal/session/sessionwantsender.go 97.85% <100.00%> (+0.47%) ⬆️
...p/client/internal/sessionmanager/sessionmanager.go 95.87% <100.00%> (-0.09%) ⬇️
bitswap/client/client.go 77.59% <92.30%> (+0.71%) ⬆️
bitswap/client/internal/session/session.go 89.25% <87.50%> (-0.97%) ⬇️

... and 8 files with indirect coverage changes

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

@gammazero gammazero marked this pull request as ready for review August 21, 2025 11:23
hsanjuan

This comment was marked as outdated.

Copy link
Contributor

@hsanjuan hsanjuan left a comment

Choose a reason for hiding this comment

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

Sorry, I believe that this is unrelated to blockservice.Session, which is the Session affecting merkledag etc. So this is only bitswap-internal. Sorry for the wall text.

@gammazero
Copy link
Contributor Author

gammazero commented Aug 22, 2025

I believe that this is unrelated to blockservice.Session, which is the Session affecting merkledag etc

Correct, but sessions are still ephemeral in nature, so your comments may still apply. Also, do we still want to control bitswap session lifetimes with a context to be consistent with how blockservice sessions are handled?

I will let this PR sit here for a bit so we and @lidel can think about it more. It is not hight priority.

@gammazero
Copy link
Contributor Author

@lidel Does a session need to accept a context to maintain retrieval state?

@lidel
Copy link
Member

lidel commented Sep 4, 2025

@gammazero yes, context is how we pass state across the stack, search where retrieval.StateFromContext is used:

boxo/retrieval/state.go

Lines 202 to 217 in 8b6b1d2

// StateFromContext retrieves the RetrievalState from the context.
// Returns nil if no RetrievalState is present in the context.
// This function is typically used by subsystems to check if retrieval tracking
// is enabled and to update the state if it is.
//
// Example:
//
// if retrievalState := retrieval.StateFromContext(ctx); retrievalState != nil {
// retrievalState.SetStage(retrieval.StageBlockRetrieval)
// retrievalState.ProvidersFound.Add(1)
// }
func StateFromContext(ctx context.Context) *RetrievalState {
if v := ctx.Value(ContextKey); v != nil {
return v.(*RetrievalState)
}
return nil

This feels like the least invasive way (optional, no overhead if not in context), but we can change it if there is better way (i did not see it at the time).

If we remove context, we need to explicitly pass RetrievalState to session constructor etc.

@gammazero
Copy link
Contributor Author

@lidel Updated to give session retrieval state.

- document that sessions manage their own lifecycle
- explain automatic cleanup via context.AfterFunc
- clarify retrieval state tracking purpose
- warn about resource leaks if Close() not called
@lidel lidel changed the title bitswap/client: close session with Close method instead of context refactor(bitswap/client/internal): close session with Close method instead of context Sep 5, 2025
Copy link
Member

@lidel lidel left a comment

Choose a reason for hiding this comment

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

Lgtm, pushed some comments about Close() being still automated when NewSession(ctx) is used. Feel free to adjust, otherwise ok to merge.

@gammazero gammazero merged commit c82e7d3 into main Sep 6, 2025
16 checks passed
@gammazero gammazero deleted the session-close branch September 6, 2025 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bitswap/client: sessions should close via a close method, not a context

4 participants