Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(serialport): data race on the isClosing bool status of the serial port #1009

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading