Skip to content

Commit

Permalink
Move uint64 fields that are accessed using sync/atomic to the beginni…
Browse files Browse the repository at this point in the history
…ng of struct to ensure proper alignment in memory

On 32-bit platforms (e.g. Raspberry Pi) it is caller's responsibility to arrange 64-bit alignment for sync/atomic operation parameters, see: https://golang.org/pkg/sync/atomic/#pkg-note-BUG. This commit fixes "SIGSEGV: segmentation violation" errors on Raspberry Pi 3.
  • Loading branch information
chacal authored and jkralik committed Nov 6, 2020
1 parent 9e0d42a commit b526118
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion tcp/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (
type EventFunc func()

type Session struct {
// This field needs to be the first in the struct to ensure proper word alignment on 32-bit platforms.
// See: https://golang.org/pkg/sync/atomic/#pkg-note-BUG
sequence uint64
connection *coapNet.Conn

maxMessageSize int
Expand All @@ -29,7 +32,6 @@ type Session struct {
errors ErrorFunc
closeSocket bool

sequence uint64
tokenHandlerContainer *HandlerContainer
midHandlerContainer *HandlerContainer
handler HandlerFunc
Expand Down
4 changes: 3 additions & 1 deletion udp/client/clientconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type Session interface {

// ClientConn represents a virtual connection to a conceptual endpoint, to perform COAPs commands.
type ClientConn struct {
// This field needs to be the first in the struct to ensure proper word alignment on 32-bit platforms.
// See: https://golang.org/pkg/sync/atomic/#pkg-note-BUG
sequence uint64
session Session
handler HandlerFunc
observationTokenHandler *HandlerContainer
Expand All @@ -51,7 +54,6 @@ type ClientConn struct {

tokenHandlerContainer *HandlerContainer
midHandlerContainer *HandlerContainer
sequence uint64
}

// NewClientConn creates connection over session and observation.
Expand Down

0 comments on commit b526118

Please sign in to comment.