Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch unaligned 64-bit atomic operation panic #37

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion announce.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (a *Announce) String() string {

// Returns the number of distinct remote addresses the announce has queried.
func (a *Announce) NumContacted() int64 {
return atomic.LoadInt64(&a.traversal.stats.NumAddrsTried)
return atomic.LoadInt64(a.traversal.stats.NumAddrsTriedAddress())
}

type AnnounceOpt *struct{}
Expand Down
21 changes: 16 additions & 5 deletions traversal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dht
import (
"fmt"
"sync/atomic"
"unsafe"

"github.com/anacrolix/dht/v2/int160"
"github.com/anacrolix/dht/v2/krpc"
Expand All @@ -13,9 +14,19 @@ import (

type TraversalStats struct {
// Count of (probably) distinct addresses we've sent traversal queries to. Accessed with atomic.
NumAddrsTried int64
NumAddrsTried [15]byte
// Number of responses we received to queries related to this traversal. Accessed with atomic.
NumResponses int64
NumResponses [15]byte
}

func (stats *TraversalStats) NumAddrsTriedAddress() *int64 {
// The return must be 8-byte aligned.
return (*int64)(unsafe.Pointer((uintptr(unsafe.Pointer(&stats.NumAddrsTried)) + 7)/8*8))
}

func (stats *TraversalStats) NumResponsesAddress() *int64 {
// The return must be 8-byte aligned.
return (*int64)(unsafe.Pointer((uintptr(unsafe.Pointer(&stats.NumResponses)) + 7)/8*8))
}

func (me TraversalStats) String() string {
Expand All @@ -24,7 +35,6 @@ func (me TraversalStats) String() string {

// Prioritizes addrs to try by distance from target, disallowing repeat contacts.
type traversal struct {
stats TraversalStats
targetInfohash int160.T
triedAddrs *stm.Var // Settish of krpc.NodeAddr.String
nodesPendingContact *stm.Var // Settish of addrMaybeId sorted by distance from the target
Expand All @@ -38,6 +48,7 @@ type traversal struct {
query func(Addr) QueryResult
// A hook to a begin a query on the server, that expects to receive the number of writes back.
serverBeginQuery func(Addr, string, func() numWrites) stm.Operation
stats TraversalStats
}

func newTraversal(targetInfohash int160.T) traversal {
Expand Down Expand Up @@ -112,10 +123,10 @@ func (a *traversal) responseNode(node krpc.NodeInfo) {
}

func (a *traversal) wrapQuery(addr Addr) QueryResult {
atomic.AddInt64(&a.stats.NumAddrsTried, 1)
atomic.AddInt64(a.stats.NumAddrsTriedAddress(), 1)
res := a.query(addr)
if res.Err == nil {
atomic.AddInt64(&a.stats.NumResponses, 1)
atomic.AddInt64(a.stats.NumResponsesAddress(), 1)
}
m := res.Reply
// Register suggested nodes closer to the target info-hash.
Expand Down