goburrow / modbus

Fault-tolerant implementation of modbus protocol in Go (golang)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Idle close of a serial connection is racy and may cause a crash

rojer opened this issue · comments

the crash happens inside Send invoked here and is due to nul dereference of mb.port, which was nulled just as the goroutine was sleeping one line above.
sometimes it's the mb.port.Write that fails as the socket is being closed.
this requires precise timing and happens if the polling interval is the same as idle timeout (60 s).
the function does take steps to reset the idle timer at the beginning, but the implementation is racy and the closeIdle callback may already have been fired. Send does not acquire a mutex to update lastActivity and make sure connection is alive, and so the closeIdle and Send race each other.