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

Error "too many open files" on ReadHoldingRegisters for RTUClient #74

Open
hubertat opened this issue Jun 15, 2021 · 1 comment
Open

Comments

@hubertat
Copy link

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.

@jonaz
Copy link

jonaz commented Mar 30, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants