Skip to content

PyNUT and blocking calls #3094

@dvdesolve

Description

@dvdesolve

I use PyNUT module to easily connect to and get data from a set of UPSes. I've discovered that some of module's calls (I'm using only CheckUPSAvailable and GetUPSVars; not tested with other ones) are blocking. When remote UPS is lost (it's primary controller rebooted or so) calling such methods will lead to the deadlock. As for now I've mitigated the problem by using signal module:

...
import signal
...
def timeout_handler(signal, frame):
    raise Exception("timeout while querying UPS")
...
# check for UPS availability (with timeout)
signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(WAIT_TIME)

try:
    if not upsc.CheckUPSAvailable(nut_name):
        print("UPS is not available")
        return False
except Exception as e:
    print(f"Something wrong while checking UPS availability: {e}")
    signal.alarm(0)
    return False

signal.alarm(0)
return True
...
# get variables from UPS (with timeout)
signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(WAIT_TIME)

try:
    curr_raw = upsc.GetUPSVars(nut_name)
except Exception as e:
    print(f"Something wrong while getting variables from UPS: {e}")
    signal.alarm(0)
    return False

signal.alarm(0)
return True

I wonder whether there are more efficient ways to handle lockups. Or, may be, even plans to implement configurable timeouts in PyNUT...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Connection stability issuesIssues about driver<->device and/or networked connections (upsd<->upsmon...) going AWOL over timeNUT protocolsimpacts-release-2.8.4Issues reported against NUT release 2.8.4 (maybe vanilla or with minor packaging tweaks)python

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions