Skip to content

Commit

Permalink
Fix race condition in network page
Browse files Browse the repository at this point in the history
  • Loading branch information
matbme committed Dec 14, 2023
1 parent f2cbe9e commit 0103396
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions vanilla_first_setup/defaults/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

from vanilla_first_setup.utils.run_async import RunAsync


logger = logging.getLogger("FirstSetup::Network")

# Dictionary mapping security types to a tuple containing
Expand Down Expand Up @@ -260,14 +259,11 @@ def __init__(self, window, distro_info, key, step, **kwargs):
self.__wired_children = []
self.__wireless_children = {}

self.__last_wifi_scan = 0

# Prevent concurrency issues when re-scanning Wi-Fi devices.
# Since we reload the list every time there's a state change,
# there's a high change that it coincides with a periodic
# refresh operation.
self.__wifi_lock = Lock()
self.__scan_lock = Lock()

# Since we have a dedicated page for checking connectivity,
# we only need to make sure the user has some type of
Expand Down Expand Up @@ -360,11 +356,13 @@ def __refresh(self):

def __start_auto_refresh(self):
def run_async():
while True:
GLib.idle_add(self.__refresh)
time.sleep(10)
return time.sleep(10)

RunAsync(run_async, None)
def callback(*args):
self.__refresh()
RunAsync(run_async, callback)

RunAsync(run_async, callback)

def __device_status(self, conn: NM.Device):
connected = False
Expand Down Expand Up @@ -394,6 +392,8 @@ def __device_status(self, conn: NM.Device):
status = _("Unmanaged")
case NM.DeviceState.UNAVAILABLE:
status = _("Unavailable")
case _:
status = _("Unknown")

return status, connected

Expand All @@ -413,11 +413,9 @@ def __add_ethernet_connection(self, conn: NM.DeviceEthernet):
self.wired_group.add(eth_conn)
self.__wired_children.append(eth_conn)

def __poll_wifi_scan(self, conn: NM.DeviceWifi):
self.__scan_lock.acquire()
while conn.get_last_scan() == self.__last_wifi_scan:
def __poll_wifi_scan(self, conn: NM.DeviceWifi, last_known_scan: int):
while conn.get_last_scan() == last_known_scan:
time.sleep(0.25)
self.__scan_lock.release()

GLib.idle_add(self.__refresh_wifi_list, conn)

Expand Down Expand Up @@ -478,12 +476,10 @@ def __refresh_wifi_list(self, conn: NM.DeviceWifi):
self.__wifi_lock.release()

def __scan_wifi(self, conn: NM.DeviceWifi):
self.__scan_lock.acquire()
self.__last_wifi_scan = conn.get_last_scan()
self.__scan_lock.release()
last_known_scan = conn.get_last_scan()
conn.request_scan_async()

t = Timer(1.5, self.__poll_wifi_scan, [conn])
t = Timer(1.5, self.__poll_wifi_scan, [conn, last_known_scan])
t.start()

@property
Expand All @@ -500,4 +496,3 @@ def multisort(xs, specs):
[it[0] for it in list(self.__wireless_children.values())],
(("connected", True), ("signal_strength", True), ("ssid", True)),
)

0 comments on commit 0103396

Please sign in to comment.