@cryptotaxi247 / kubo / commits / 64c19cc90

WIP: fix wantlist clearing by closing down session

License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>

Jeromy committed Feb 5, 2018 at 12:14 UTC 64c19cc9044a834855691cceb2e460aecbaccedc
2 files changed +41
exchange/bitswap/session.go
+8
@@ -84,6 +84,14 @@ func (bs *Bitswap) NewSession(ctx context.Context) *Session {
84
85 func (bs *Bitswap) removeSession(s *Session) {
86 s.notif.Shutdown()
87 +
88 + live := make([]*cid.Cid, 0, len(s.liveWants))
89 + for c := range s.liveWants {
90 + cs, _ := cid.Cast([]byte(c))
91 + live = append(live, cs)
92 + }
93 + bs.CancelWants(live, s.id)
94 +
95 bs.sessLk.Lock()
96 defer bs.sessLk.Unlock()
97 for i := 0; i < len(bs.sessions); i++ {
exchange/bitswap/session_test.go
+33
@@ -285,3 +285,36 @@ func TestMultipleSessions(t *testing.T) {
285 }
286 _ = blkch
287 }
288 +
289 +func TestWantlistClearsOnCancel(t *testing.T) {
290 + ctx, cancel := context.WithCancel(context.Background())
291 + defer cancel()
292 +
293 + vnet := getVirtualNetwork()
294 + sesgen := NewTestSessionGenerator(vnet)
295 + defer sesgen.Close()
296 + bgen := blocksutil.NewBlockGenerator()
297 +
298 + blks := bgen.Blocks(10)
299 + var cids []*cid.Cid
300 + for _, blk := range blks {
301 + cids = append(cids, blk.Cid())
302 + }
303 +
304 + inst := sesgen.Instances(1)
305 +
306 + a := inst[0]
307 +
308 + ctx1, cancel1 := context.WithCancel(ctx)
309 + ses := a.Exchange.NewSession(ctx1)
310 +
311 + _, err := ses.GetBlocks(ctx, cids)
312 + if err != nil {
313 + t.Fatal(err)
314 + }
315 + cancel1()
316 +
317 + if len(a.Exchange.GetWantlist()) > 0 {
318 + t.Fatal("expected empty wantlist")
319 + }
320 +}