Skip to content

NUT-Monitor: Add automatic reconnection mechanism for Qt5 and Qt6#3523

Closed
developersorli wants to merge 2 commits into
networkupstools:masterfrom
developersorli:nut-monitor-reconnect
Closed

NUT-Monitor: Add automatic reconnection mechanism for Qt5 and Qt6#3523
developersorli wants to merge 2 commits into
networkupstools:masterfrom
developersorli:nut-monitor-reconnect

Conversation

@developersorli

Copy link
Copy Markdown
Contributor

Summary

This PR adds an automatic reconnection mechanism to NUT-Monitor-py3qt5 and NUT-Monitor-py3qt6. When the connection to the NUT server is lost (e.g., after system suspend/resume, network interruption, or NUT server restart), the client will automatically attempt to reconnect.

Changes

  1. Reconnection logic in gui_updater.__update():

    • Detects connection errors via specific exceptions
    • Stops the polling timer
    • Schedules reconnection attempt after 2 seconds
  2. New __attempt_reconnect() method:

    • Gets connection parameters from the current GUI state
    • Properly tears down old PyNUTClient instance (closes telnet connection)
    • Creates new connection and verifies UPS availability
    • Restores GUI state and resumes polling
  3. __reconnect_attempted flag - prevents multiple simultaneous attempts

  4. Proper exception handling:

    • socket.error - network/socket errors
    • ConnectionError - Python 3 built-in connection errors
    • EOFError - end of file / connection closed
    • OSError - operating system errors
    • PyNUT.PyNUTError - NUT protocol errors
  5. Proper teardown of old connection handler:

    • Closes underlying telnet connection
    • Deletes old handler reference
  6. was_online flag - tracks connection state changes

Testing

  • Qt5: ✅ Fully tested and confirmed working

    • System suspend/resume cycles
    • Network disconnection/reconnection
    • NUT server restart
    • Authentication and non-authentication modes
  • Qt6: ✅ Fully tested and confirmed working

    • Same tests performed as Qt5
    • All functionality verified

Files Modified

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

Related Issues

Closes #3509

Notes for Reviewers

  • Both versions use the same reconnection logic
  • Both versions have been tested and confirmed working
  • The implementation only affects the Qt versions, not GTK2 (Python 2, obsolete)
  • The PyNUTClient instance is properly cleaned up before reconnection

- Add automatic reconnection when connection to NUT server is lost
- Implement proper teardown of old PyNUTClient instances
- Use specific exceptions (socket.error, ConnectionError, EOFError, OSError, PyNUT.PyNUTError)
- Prevent multiple simultaneous reconnection attempts
- Add 'was_online' flag to track connection state changes
- Tested and confirmed working on both Qt5 and Qt6

Closes: networkupstools#3509
Signed-off-by: Programer <developer.sorli@gmail.com>
@github-actions

github-actions Bot commented Jul 5, 2026

Copy link
Copy Markdown

A ZIP file with standard source tarball and another tarball with pre-built docs for commit 4b9209f is temporarily available: NUT-tarballs-PR-3523.zip.

@AppVeyorBot

Copy link
Copy Markdown

Build nut 2.8.5.4914-master completed (commit 572b34b736 by @developersorli)

@jimklimov jimklimov added enhancement python Connection stability issues Issues about driver<->device and/or networked connections (upsd<->upsmon...) going AWOL over time labels Jul 13, 2026
@jimklimov jimklimov added this to the 2.8.6 milestone Jul 13, 2026
@jimklimov

jimklimov commented Jul 16, 2026

Copy link
Copy Markdown
Member

With a real X11 environment (LightDM) the Py3Qt5 variant reconnected at least once, but most of the time it flooded "Broken pipe" notifications every couple of seconds that the data server was down and even after it went back up:

  • image

The client was responsive, so I could open the menu or "Disconnect" and "Connect" it again with its UI buttons.

Can't really say what the Py3Qt6 variant did, as I ran it from WSL2 (Ubuntu) rendering to a VcXsrv on its Windows host, and it stalled (maybe trying to post a notification which has nowhere to go, and waits for some response that would never come?)

It did react to Ctrl+C on the console that I started it from, apparently breaking some loop and reacting to subsequent UI operations. Only then did the status bar update to also "Errno 32: Broken pipe" message (in one test the status bar remained "Connected to..." when it stalled, in another it went empty and remained that way until Ctrl+C). It did not auto-reconnect either after the data server restarted.

jimklimov added a commit to jimklimov/nut that referenced this pull request Jul 19, 2026
jimklimov added a commit to jimklimov/nut that referenced this pull request Jul 19, 2026
…tion modeled after PR networkupstools#3523 for Python3 siblings [networkupstools#3509]

Co-Authored-By: Programer <developer.sorli@gmail.com>
Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
@jimklimov

Copy link
Copy Markdown
Member

Closing in favor of #3530 which includes (a rebase of) these commits and more, to add same feature to Py2Gtk2 variant and hopefully fix the issues logged above.

Great thanks to @developersorli for the contribution!

@jimklimov jimklimov closed this Jul 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Connection stability issues Issues about driver<->device and/or networked connections (upsd<->upsmon...) going AWOL over time enhancement python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NUT-Monitor-py3qt5: Add D-Bus sleep handler for automatic reconnect after system wake-up

3 participants