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 2 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: 8 additions & 0 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"
"time"
"unicode/utf8"

Expand All @@ -45,6 +46,8 @@ type serport struct {
// just so we don't show scary error messages
isClosing bool

mu sync.Mutex

isClosingDueToError bool

// buffered channel containing up to 25600 outbound messages.
Expand Down Expand Up @@ -84,13 +87,15 @@ func (p *serport) reader(buftype string) {
n, err := p.portIo.Read(serialBuffer)
bufferPart := serialBuffer[:n]

p.mu.Lock()
//if we detect that port is closing, break out of this for{} loop.
if p.isClosing {
strmsg := "Shutting down reader on " + p.portConf.Name
log.Println(strmsg)
h.broadcastSys <- []byte(strmsg)
break
}
p.mu.Unlock()

// read can return legitimate bytes as well as an error
// so process the n bytes red, if n > 0
Expand Down Expand Up @@ -348,7 +353,10 @@ func spHandlerOpen(portname string, baud int, buftype string) {
}

func (p *serport) Close() {
p.mu.Lock()
p.isClosing = true
p.mu.Unlock()

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