Skip to content

Commit

Permalink
fix(serialport): data race on the isClosing bool status of the seri…
Browse files Browse the repository at this point in the history
…al port (#1009)

* fix: use atomic boolean for thread-safe isClosing flag
  • Loading branch information
dido18 authored Jan 21, 2025
1 parent f1ca3c9 commit 1b94ccc
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions serialport.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/base64"
"io"
"strconv"
"sync/atomic"
"time"
"unicode/utf8"

Expand All @@ -43,7 +44,7 @@ type serport struct {

// Keep track of whether we're being actively closed
// just so we don't show scary error messages
isClosing bool
isClosing atomic.Bool

isClosingDueToError bool

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

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

func (p *serport) Close() {
p.isClosing = true
p.isClosing.Store(true)

p.bufferwatcher.Close()
p.portIo.Close()
serialPorts.MarkPortAsClosed(p.portName)
Expand Down

0 comments on commit 1b94ccc

Please sign in to comment.