Skip to content

Commit c8f8269

Browse files
committed
scripts/python/app/NUT-Monitor-*.in, scripts/python/module/PyNUT.py.in: refactor with a graceful and harsher disconnect() handling [#3509]
Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
1 parent 99715db commit c8f8269

4 files changed

Lines changed: 22 additions & 12 deletions

File tree

scripts/python/app/NUT-Monitor-py2gtk2.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,10 @@ class gui_updater( threading.Thread ) :
995995
self.__parent_class.gui_status_message( _("Connection lost - attempting to reconnect...") )
996996
self.__parent_class.gui_status_notification( _("Connection lost"), "warning.png" )
997997

998+
# Close the socket to avoid "Broken pipe" in subsequent calls if any
999+
if self.__parent_class._interface__ups_handler:
1000+
self.__parent_class._interface__ups_handler.disconnect()
1001+
9981002
# Wait for 2 seconds before attempting reconnection
9991003
time.sleep( 2 )
10001004
self.__attempt_reconnect()

scripts/python/app/NUT-Monitor-py3qt5.in

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,10 +1056,8 @@ class gui_updater :
10561056
self.__timer.stop()
10571057

10581058
# Close the socket to avoid "Broken pipe" in subsequent calls if any
1059-
try:
1060-
self.__parent_class._interface__ups_handler._PyNUTClient__srv_handler.close()
1061-
except Exception:
1062-
pass
1059+
if self.__parent_class._interface__ups_handler:
1060+
self.__parent_class._interface__ups_handler.disconnect()
10631061

10641062
# Schedule reconnection attempt after 2 seconds
10651063
QTimer.singleShot(2000, self.__attempt_reconnect)

scripts/python/app/NUT-Monitor-py3qt6.in

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,10 +1079,8 @@ class gui_updater :
10791079
self.__timer.stop()
10801080

10811081
# Close the socket to avoid "Broken pipe" in subsequent calls if any
1082-
try:
1083-
self.__parent_class._interface__ups_handler._PyNUTClient__srv_handler.close()
1084-
except Exception:
1085-
pass
1082+
if self.__parent_class._interface__ups_handler:
1083+
self.__parent_class._interface__ups_handler.disconnect()
10861084

10871085
# Schedule reconnection attempt after 2 seconds
10881086
QTimer.singleShot(2000, self.__attempt_reconnect)

scripts/python/module/PyNUT.py.in

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -711,10 +711,20 @@ certhost : Optional dict or list of dict/lists: {hostname: [certname, certver
711711
# Try to disconnect cleanly when class is deleted ;)
712712
def __del__( self ) :
713713
""" Class destructor method """
714-
try :
715-
self.__send( b"LOGOUT\n" )
716-
except :
717-
pass
714+
self.disconnect()
715+
716+
def disconnect(self):
717+
""" Disconnects from the server cleanly """
718+
if hasattr(self, '__srv_handler') and self.__srv_handler:
719+
try:
720+
self.__send(b"LOGOUT\n")
721+
except:
722+
pass
723+
try:
724+
self.__srv_handler.close()
725+
except:
726+
pass
727+
self.__srv_handler = None
718728

719729
def __send(self, data):
720730
""" Internal method to send data through the socket with error handling """

0 commit comments

Comments
 (0)