Supervisor API reboot causing device to stall offline requiring power cycle

Running the following script causes the device to stall with no more logs from any services and the device goes offline never to be heard from again until a power cycle. It is possible that this command was running multiple times. Would that cause the issue? e.g. if the reboot command was giving while services were trying to shutdown?

When I call the script from an mqtt message from cloud to device, this error does not happen.

def reboot_device():'REBOOTING DEVICE')

    url = str(os.environ['BALENA_SUPERVISOR_ADDRESS']) + \
        "/v1/reboot?apikey=" + str(os.environ['BALENA_SUPERVISOR_API_KEY'])

    data = {'force': True}

    # sending post request and saving response as response object
    r =, data=data)

    # extracting response text
    res = r.text
    print("Reboot res is:%s" % res)