modbus icon indicating copy to clipboard operation
modbus copied to clipboard

Error "too many open files" on ReadHoldingRegisters for RTUClient

Open hubertat opened this issue 3 years ago • 1 comments

Hi, I'm getting too many open files error when reading from RTUClient handler.

It happens when program is running for quite a long time. Example code below, please note that RTUClientHandler is created only once and exists for whole lifetime of my program. As you can see below, connection is closed (by defer) after each reading attempt. I'm not doing any concurrent readings.

func (qd *rtuDevice) Read() (err error) {
	err = qd.modbusHandler.Connect()
	defer qd.modbusHandler.Close()
	if err != nil {
		return
	}

	client := modbus.NewClient(qd.modbusHandler)
	var results []byte
	results, err = client.ReadHoldingRegisters(0, 62)
	if err != nil {
		return
	}

	qd.Data = &DataObject{}
	buf := bytes.NewReader(results)
	err = binary.Read(buf, binary.BigEndian, qd.Data)
	if err != nil {
		return
	}

	return
}

I think it's a bug, if it's not and it can be avoided please advise.

hubertat avatar Jun 15 '21 05:06 hubertat

Why do you create a new client on every read? that might leak?

jonaz avatar Mar 30 '22 18:03 jonaz