Skip to content

Commit b526118

Browse files
chacaljkralik
authored andcommitted
Move uint64 fields that are accessed using sync/atomic to the beginning 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.
1 parent 9e0d42a commit b526118

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

tcp/session.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import (
1818
type EventFunc func()
1919

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

2326
maxMessageSize int
@@ -29,7 +32,6 @@ type Session struct {
2932
errors ErrorFunc
3033
closeSocket bool
3134

32-
sequence uint64
3335
tokenHandlerContainer *HandlerContainer
3436
midHandlerContainer *HandlerContainer
3537
handler HandlerFunc

udp/client/clientconn.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ type Session interface {
3636

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

5255
tokenHandlerContainer *HandlerContainer
5356
midHandlerContainer *HandlerContainer
54-
sequence uint64
5557
}
5658

5759
// NewClientConn creates connection over session and observation.

0 commit comments

Comments
 (0)