svc: hud: Protect window switches with a lock
Any time we need to switch Firefox windows, we need to use a lock to prevent multiple simultaneous requests. If we do not, interleaved Marionette commands may result in performing operations on the wrong window. For example, making two simultaneous requests for screenshots is liable to return the wrong window for one of them.master
parent
35eba74bfd
commit
fa1c9cb42a
|
@ -35,10 +35,12 @@ class HUDService:
|
|||
self.windows: Dict[str, str] = {}
|
||||
|
||||
self.urls_file = Path('urls.json')
|
||||
self.lock = asyncio.Lock()
|
||||
|
||||
async def get_screens(self) -> Dict[str, HUDScreen]:
|
||||
assert self.marionette
|
||||
screens = {}
|
||||
async with self.lock:
|
||||
for name, handle in self.windows.items():
|
||||
await self.marionette.switch_to_window(handle)
|
||||
title = await self.marionette.get_title()
|
||||
|
@ -56,6 +58,7 @@ class HUDService:
|
|||
raise NoMonitorConfig(
|
||||
'Cannot initialize display: No monitor config supplied'
|
||||
)
|
||||
async with self.lock:
|
||||
log.info(
|
||||
'Initializing display for %d monitors',
|
||||
len(self.monitor_config.monitors),
|
||||
|
@ -82,7 +85,9 @@ class HUDService:
|
|||
)
|
||||
await self.marionette.fullscreen()
|
||||
log.info('Screen %s: Opening URL %s', monitor.name, url)
|
||||
tasks.append(asyncio.create_task(self.marionette.navigate(url)))
|
||||
tasks.append(
|
||||
asyncio.create_task(self.marionette.navigate(url))
|
||||
)
|
||||
window = None
|
||||
if tasks:
|
||||
await asyncio.wait(tasks)
|
||||
|
@ -113,6 +118,7 @@ class HUDService:
|
|||
|
||||
async def refresh_screen(self, name: str) -> None:
|
||||
assert self.marionette
|
||||
async with self.lock:
|
||||
await self.marionette.switch_to_window(self.windows[name])
|
||||
await self.marionette.refresh()
|
||||
|
||||
|
@ -138,6 +144,7 @@ class HUDService:
|
|||
async def take_screenshot(self, screen: str) -> bytes:
|
||||
assert self.marionette
|
||||
handle = self.windows[screen]
|
||||
async with self.lock:
|
||||
await self.marionette.switch_to_window(handle)
|
||||
data = await self.marionette.take_screenshot()
|
||||
return base64.b64decode(data)
|
||||
|
|
Loading…
Reference in New Issue