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): add mutex for thread-safe access of isClosing bool status #1009

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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: 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()
Comment on lines +90 to +98
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the break path is taken doesn't the mutex remain locked?


// 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