Prevent potential racing conditions.

Prevent abort if superflous signal is received.
This commit is contained in:
redxef 2022-10-29 02:52:41 +02:00
parent c10b9fdb66
commit 6f49d819ac
Signed by: redxef
GPG key ID: 7DAC3AA211CBD921

View file

@ -272,7 +272,7 @@ def parse(s: str) -> Expression:
tokens = tokenize(s) tokens = tokenize(s)
return build_expression(tokens) return build_expression(tokens)
def window_new(configs: list[dict], debug: bool): def window_new(configs: list[dict], debug: bool, *, lock):
async def callback(ipc: i3ipc.aio.Connection, e: i3ipc.WorkspaceEvent): async def callback(ipc: i3ipc.aio.Connection, e: i3ipc.WorkspaceEvent):
assert e.change == 'new' assert e.change == 'new'
if debug: if debug:
@ -282,9 +282,11 @@ def window_new(configs: list[dict], debug: bool):
workspace = cfg['workspace'] workspace = cfg['workspace']
if filter.reduce(e.ipc_data): if filter.reduce(e.ipc_data):
container_id = e.ipc_data['container']['id'] container_id = e.ipc_data['container']['id']
await ipc.command(f'for_window [con_id="{container_id}"] focus') async with lock:
await ipc.command(f'move container to workspace {workspace}') await ipc.command(f'for_window [con_id="{container_id}"] focus')
configs.pop(i) await ipc.command(f'move container to workspace {workspace}')
await asyncio.sleep(1)
configs.pop(i)
if not configs: if not configs:
ipc.main_quit() ipc.main_quit()
return callback return callback
@ -295,11 +297,17 @@ async def wait_signal(s):
await event.wait() await event.wait()
event.clear() event.clear()
asyncio.get_running_loop().remove_signal_handler(s) asyncio.get_running_loop().remove_signal_handler(s)
asyncio.get_running_loop().add_signal_handler(s, lambda: None)
async def coro_wait_signal(coro, s):
await coro
await wait_signal(s)
async def run(configs: list[dict], *, timeout: int, debug: bool): async def run(configs: list[dict], *, timeout: int, debug: bool):
window_configs = [c for c in configs if c.get('workspace') is not None] window_configs = [c for c in configs if c.get('workspace') is not None]
lock = asyncio.Lock()
ipc = await i3ipc.aio.Connection().connect() # we only wait for configs which spawn a window ipc = await i3ipc.aio.Connection().connect() # we only wait for configs which spawn a window
ipc.on('window::new', window_new(window_configs, debug=debug)) ipc.on('window::new', window_new(window_configs, debug=debug, lock=lock))
variables = { variables = {
'pid': os.getpid(), 'pid': os.getpid(),
@ -313,10 +321,9 @@ async def run(configs: list[dict], *, timeout: int, debug: bool):
p = p.format(**variables) p = p.format(**variables)
coro = ipc.command(f'exec {p}') coro = ipc.command(f'exec {p}')
if cfg.get('signal_continue', False): if cfg.get('signal_continue', False):
await coro sig = signal.Signals(cfg.get('signal_continue'))
await wait_signal(signal.Signals(cfg.get('signal_continue'))) coro = coro_wait_signal(coro, sig)
else: coroutines += [coro]
coroutines += [coro]
await asyncio.gather(*coroutines) await asyncio.gather(*coroutines)
try: try:
if window_configs: if window_configs: