Skip to content

Commit 1b94ccc

Browse files
authored
fix(serialport): data race on the isClosing bool status of the serial port (#1009)
* fix: use atomic boolean for thread-safe isClosing flag
1 parent f1ca3c9 commit 1b94ccc

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

Diff for: serialport.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"encoding/base64"
2121
"io"
2222
"strconv"
23+
"sync/atomic"
2324
"time"
2425
"unicode/utf8"
2526

@@ -43,7 +44,7 @@ type serport struct {
4344

4445
// Keep track of whether we're being actively closed
4546
// just so we don't show scary error messages
46-
isClosing bool
47+
isClosing atomic.Bool
4748

4849
isClosingDueToError bool
4950

@@ -85,7 +86,7 @@ func (p *serport) reader(buftype string) {
8586
bufferPart := serialBuffer[:n]
8687

8788
//if we detect that port is closing, break out of this for{} loop.
88-
if p.isClosing {
89+
if p.isClosing.Load() {
8990
strmsg := "Shutting down reader on " + p.portConf.Name
9091
log.Println(strmsg)
9192
h.broadcastSys <- []byte(strmsg)
@@ -348,7 +349,8 @@ func spHandlerOpen(portname string, baud int, buftype string) {
348349
}
349350

350351
func (p *serport) Close() {
351-
p.isClosing = true
352+
p.isClosing.Store(true)
353+
352354
p.bufferwatcher.Close()
353355
p.portIo.Close()
354356
serialPorts.MarkPortAsClosed(p.portName)

0 commit comments

Comments
 (0)