From 4c273310109b8d134d96b35d66d7231e8c54a05b Mon Sep 17 00:00:00 2001 From: Grigorii Khvatskii Date: Mon, 26 Feb 2024 16:12:18 -0500 Subject: [PATCH 1/2] Fix unaligned load error on 32-bit architectures On some 32-bit architectures, 64-bit atomic operations panic when the value is not aligned properly. In this package, this causes netConn operations to panic when compiling with GOARCH=386, since netConn does atomic operations with int64 values in the netConn struct (namely, with readExpired and writeExpired). This commit fixes this by moving readExpired and writeExpired to the beginning of the struct, which makes them properly aligned. --- netconn.go | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/netconn.go b/netconn.go index 1667f45c..133ba55f 100644 --- a/netconn.go +++ b/netconn.go @@ -94,22 +94,23 @@ func NetConn(ctx context.Context, c *Conn, msgType MessageType) net.Conn { } type netConn struct { + readExpired int64 + writeExpired int64 + c *Conn msgType MessageType - writeTimer *time.Timer - writeMu *mu - writeExpired int64 - writeCtx context.Context - writeCancel context.CancelFunc - - readTimer *time.Timer - readMu *mu - readExpired int64 - readCtx context.Context - readCancel context.CancelFunc - readEOFed bool - reader io.Reader + writeTimer *time.Timer + writeMu *mu + writeCtx context.Context + writeCancel context.CancelFunc + + readTimer *time.Timer + readMu *mu + readCtx context.Context + readCancel context.CancelFunc + readEOFed bool + reader io.Reader } var _ net.Conn = &netConn{} From 1cc90bb49096127bb51ee1bfe860bdee099e94d8 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Thu, 7 Mar 2024 11:39:07 -0800 Subject: [PATCH 2/2] netconn: Explain why we start with the two int64 atomics --- netconn.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/netconn.go b/netconn.go index 133ba55f..3324014d 100644 --- a/netconn.go +++ b/netconn.go @@ -94,6 +94,8 @@ func NetConn(ctx context.Context, c *Conn, msgType MessageType) net.Conn { } type netConn struct { + // These must be first to be aligned on 32 bit platforms. + // https://github.com/nhooyr/websocket/pull/438 readExpired int64 writeExpired int64