@cryptotaxi247 / kubo / commits / ce2d0a225

fix: add lock to taskQueue

@whyrusleeping may wanna have a look and make sure i didn't screw anything up here BenchmarkInstantaneousAddCat1MB-4 200 10763761 ns/op 97.42 MB/s BenchmarkInstantaneousAddCat2MB-4 panic: runtime error: invalid memory address or nil pointer dereference [signal 0xb code=0x1 addr=0x0 pc=0xbedd] goroutine 14297 [running]: github.com/jbenet/go-ipfs/exchange/bitswap/decision.(*taskQueue).Remove(0xc2087553a0, 0xc2085ef200, 0x22, 0x56f570, 0xc208367a40) /Users/btc/go/src/github.com/jbenet/go-ipfs/exchange/bitswap/decision/taskqueue.go:66 +0x82 github.com/jbenet/go-ipfs/exchange/bitswap/decision.(*Engine).MessageSent(0xc20871b5c0, 0x56f570, 0xc208367a40, 0x570040, 0xc208753d40, 0x0, 0x0) /Users/btc/go/src/github.com/jbenet/go-ipfs/exchange/bitswap/decision/engine.go:177 +0x29e github.com/jbenet/go-ipfs/exchange/bitswap.(*bitswap).send(0xc20871b7a0, 0x56f4d8, 0xc208379800, 0x56f570, 0xc208367a40, 0x570040, 0xc208753d40, 0x0, 0x0) /Users/btc/go/src/github.com/jbenet/go-ipfs/exchange/bitswap/bitswap.go:352 +0x11c github.com/jbenet/go-ipfs/exchange/bitswap.(*bitswap).taskWorker(0xc20871b7a0, 0x56f4d8, 0xc208379800) /Users/btc/go/src/github.com/jbenet/go-ipfs/exchange/bitswap/bitswap.go:238 +0x165 created by github.com/jbenet/go-ipfs/exchange/bitswap.New /Users/btc/go/src/github.com/jbenet/go-ipfs/exchange/bitswap/bitswap.go:66 +0x49e

Brian Tiger Chow committed Dec 17, 2014 at 02:24 UTC ce2d0a2258bb714cf48e6fc445c696434db2f0d3
1 file changed +9
exchange/bitswap/decision/taskqueue.go
+9
@@ -1,6 +1,8 @@
1 package decision
2
3 import (
4 + "sync"
5 +
6 wantlist "github.com/jbenet/go-ipfs/exchange/bitswap/wantlist"
7 peer "github.com/jbenet/go-ipfs/peer"
8 u "github.com/jbenet/go-ipfs/util"
@@ -11,6 +13,7 @@ import (
13 // tasks (on getnext). For now, we are assuming a dumb/nice strategy.
14 type taskQueue struct {
15 // TODO: make this into a priority queue
16 + lock sync.Mutex
17 tasks []*task
18 taskmap map[string]*task
19 }
@@ -29,6 +32,8 @@ type task struct {
32
33 // Push currently adds a new task to the end of the list
34 func (tl *taskQueue) Push(entry wantlist.Entry, to peer.Peer) {
35 + tl.lock.Lock()
36 + defer tl.lock.Unlock()
37 if task, ok := tl.taskmap[taskKey(to, entry.Key)]; ok {
38 // TODO: when priority queue is implemented,
39 // rearrange this task
@@ -45,6 +50,8 @@ func (tl *taskQueue) Push(entry wantlist.Entry, to peer.Peer) {
50
51 // Pop 'pops' the next task to be performed. Returns nil no task exists.
52 func (tl *taskQueue) Pop() *task {
53 + tl.lock.Lock()
54 + defer tl.lock.Unlock()
55 var out *task
56 for len(tl.tasks) > 0 {
57 // TODO: instead of zero, use exponential distribution
@@ -63,10 +70,12 @@ func (tl *taskQueue) Pop() *task {
70
71 // Remove lazily removes a task from the queue
72 func (tl *taskQueue) Remove(k u.Key, p peer.Peer) {
73 + tl.lock.Lock()
74 t, ok := tl.taskmap[taskKey(p, k)]
75 if ok {
76 t.Trash = true
77 }
78 + tl.lock.Unlock()
79 }
80
81 // taskKey returns a key that uniquely identifies a task.