-
Notifications
You must be signed in to change notification settings - Fork 145
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
FD leak for serial port #69
Comments
Probably |
I also think it should help but I didn't try. It was easier for me to patch my local copy and add a destructor. |
It is possible to close the serial port using However, it would be interesting to know what caused the problem in the first place. |
That's a good question. Probably it's a buggy HW, I see Linux loses USB to Modbus converter and I have to reset it (dmesg output): Right before that happens minimalmodbus throws the following exception: Obviously after calling usbreset, FD is no longer valid (but still open which causes the leak) and I need to re-open the port. Any idea what to try before we conclude it's a HW issue? |
If there is an usb error, the port is recreated, but if the file is opened, then it can't reuse the same name, so e.g. if you are using If you are on the recent linux (or it is easy to obtain UDEV rule to do so), you should find symlink to the serial port in the directory With this setup (using exclusively import serial
import minimalmodbus
class SerialAutoOpen(serial.Serial):
def open(self):
try:
super().open()
except IOError:
pass
def close(self):
try:
super().close()
except IOError:
pass
self.is_open = False
def write(self, data):
self.open()
try:
return super().write(data)
except IOError:
self.close()
raise
minimalmodbus.serial.Serial = SerialAutoOpen
instrument = minimalmodbus.Instrument(...) |
I my script I need to re-initialize Instrument from time to time. After couple of months of uptime it failed with 'Too many open files' error. Investigation showed that serial devices were kept open thus causing a leak of file descriptors. Adding a destructor with self.serial.close() call inside solves the problem.
The text was updated successfully, but these errors were encountered: