From ff63b191ded94e60736449335780d05025fd0dc7 Mon Sep 17 00:00:00 2001
From: Anmol Sethi <hi@nhooyr.io>
Date: Fri, 27 Sep 2019 13:39:17 -0500
Subject: [PATCH] Fix unaligned 64 bit atomic loads on 32 bit platforms

Closes #153
---
 conn.go         | 5 +++--
 conn_common.go  | 2 +-
 websocket_js.go | 2 +-
 3 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/conn.go b/conn.go
index 3d7d574..2d0339e 100644
--- a/conn.go
+++ b/conn.go
@@ -70,7 +70,8 @@ type Conn struct {
 	activeReader *messageReader
 	// readFrameLock is acquired to read from bw.
 	readFrameLock     chan struct{}
-	readClosed        int64
+	// Not int32 because of https://github.com/nhooyr/websocket/issues/153
+	readClosed        int32
 	readHeaderBuf     []byte
 	controlPayloadBuf []byte
 
@@ -341,7 +342,7 @@ func (c *Conn) handleControl(ctx context.Context, h header) error {
 // See https://github.com/nhooyr/websocket/issues/87#issue-451703332
 // Most users should not need this.
 func (c *Conn) Reader(ctx context.Context) (MessageType, io.Reader, error) {
-	if atomic.LoadInt64(&c.readClosed) == 1 {
+	if atomic.LoadInt32(&c.readClosed) == 1 {
 		return 0, nil, fmt.Errorf("websocket connection read closed")
 	}
 
diff --git a/conn_common.go b/conn_common.go
index ae0fe55..9a4f904 100644
--- a/conn_common.go
+++ b/conn_common.go
@@ -178,7 +178,7 @@ func (c *netConn) SetReadDeadline(t time.Time) error {
 // Use this when you do not want to read data messages from the connection anymore but will
 // want to write messages to it.
 func (c *Conn) CloseRead(ctx context.Context) context.Context {
-	atomic.StoreInt64(&c.readClosed, 1)
+	atomic.StoreInt32(&c.readClosed, 1)
 
 	ctx, cancel := context.WithCancel(ctx)
 	go func() {
diff --git a/websocket_js.go b/websocket_js.go
index 3822797..dcb0206 100644
--- a/websocket_js.go
+++ b/websocket_js.go
@@ -89,7 +89,7 @@ func (c *Conn) closeWithInternal() {
 // Read attempts to read a message from the connection.
 // The maximum time spent waiting is bounded by the context.
 func (c *Conn) Read(ctx context.Context) (MessageType, []byte, error) {
-	if atomic.LoadInt64(&c.readClosed) == 1 {
+	if atomic.LoadInt32(&c.readClosed) == 1 {
 		return 0, nil, fmt.Errorf("websocket connection read closed")
 	}
 
-- 
GitLab