From f444fba6ea5073c4c18b9e25fb69a8d6c29bb1c1 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Thu, 16 Jan 2020 03:14:16 -0800 Subject: [PATCH] network: Add method to re-activate connections after an update - When a connection is updated with newer settings, the changes don't reflect unless the connection is deactivated and reactivated. Add a convenience method to achieve this. - Don't perform any operation if the connection is not active. - Wait until connection is deactivated before reactivating Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/network.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/plinth/network.py b/plinth/network.py index 7f47f36f4..7943f4fcb 100644 --- a/plinth/network.py +++ b/plinth/network.py @@ -22,6 +22,7 @@ import collections import logging import socket import struct +import time import uuid from django.utils.translation import ugettext_lazy as _ @@ -515,6 +516,33 @@ def deactivate_connection(connection_uuid): return active_connection +def reactivate_connection(connection_uuid): + """Find and re-activate a network connection to reflect new changes. + + If connection was not active to begin with, do nothing. + + """ + try: + deactivate_connection(connection_uuid) + except ConnectionNotFound: + return # Connection was not active to start with + + # deactivate_connection() is a synchronous call. However, it returns before + # the connection is fully deactivated. When re-activating such connections, + # sometimes, we get a "Authorization request cancelled" error. So, wait + # until the connection is fully deactivated. XXX: Perform proper + # asynchronous waiting instead of polling. Also find a way to avoid the + # problem altogether. + for index in range(10): # pylint: disable=unused-variable + try: + get_active_connection(connection_uuid) + time.sleep(0.1) + except ConnectionNotFound: + break + + activate_connection(connection_uuid) + + def delete_connection(connection_uuid): """Delete an exiting connection from network manager.