Describe the problem
The Read function uses the port.handle without a lock:
err = windows.ReadFile(port.handle, p, &readed, ev)
https://github.com/bugst/go-serial/blob/master/serial_windows.go#L88
But, Close could be called concurrently to cancel the Read, which sets port.handle. This is a race condition. The solution is to have Read only read the handle with a lock:
port.mu.Lock()
h := port.handle
port.mu.Unlock()
// later... we use the handle
err = windows.ReadFile(h, p, &readed, ev)
To reproduce
On Windows, call Close on the serial port during a pending Read. Be sure that the Go race detector is turned on.
Please double-check that you have reported each of the following
before submitting the issue.
Expected behavior
No race.
Operating system and version
Windows 11 - go1.26.2 windows/amd64
Please describe your hardware setup
No response
Additional context
Related issue #105
Issue checklist
Describe the problem
The Read function uses the
port.handlewithout a lock:https://github.com/bugst/go-serial/blob/master/serial_windows.go#L88
But, Close could be called concurrently to cancel the Read, which sets
port.handle. This is a race condition. The solution is to have Read only read the handle with a lock:To reproduce
On Windows, call Close on the serial port during a pending Read. Be sure that the Go race detector is turned on.
Please double-check that you have reported each of the following
before submitting the issue.
Expected behavior
No race.
Operating system and version
Windows 11 - go1.26.2 windows/amd64
Please describe your hardware setup
No response
Additional context
Related issue #105
Issue checklist