i3-companion: move dampening/retry logic inside a decorator

There is some nasty bug we can run into:

```
ERROR: Task was destroyed but it is pending!
task: <Task pending name='Task-44' coro=<dampen.<locals>.decorator.<locals>.fn_now() running at /home/bernat/.config/i3/bin/i3-companion:115> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f687353e2b0>()]>>
```

This is often followed by a segfault. It's a bit difficult to
understand where it comes from. Sleeping a bit even if we don't want
to dampen seems to workaround this issue. It seems we have to keep a
reference to a task until it is cancelled properly.
This commit is contained in:
Vincent Bernat 2021-07-12 22:15:57 +02:00
parent fb0bd7fcca
commit 2012ba0c15

View file

@ -98,6 +98,53 @@ def on(*events):
return decorator return decorator
def dampen(sleep, *, unless=None, retry=0):
"""Dampen a function call."""
def decorator(fn):
async def fn_now(retry, *args, **kwargs):
try:
if unless is not None and unless(*args, **kwargs):
# see https://github.com/ldo/dbussy/issues/15
await asyncio.sleep(0.1)
else:
await asyncio.sleep(sleep)
except asyncio.CancelledError:
return
fn.running = None
try:
return await fn(*args, **kwargs)
except Exception as e:
if not retry:
logger.exception(f"while executing {fn}: %s", e)
return
retry -= 1
logger.warning(
f"while executing {fn} (remaining tries: %d): %s",
retry,
str(e),
)
if fn.running is not None:
return
fn.running = asyncio.create_task(
fn_now(retry, *args, **kwargs)
)
@functools.wraps(fn)
async def wrapper(*args, **kwargs):
if fn.running is not None:
logger.debug(f"cancel call to previous {fn}")
fn.running.cancel()
fn.running = None
logger.debug(f"dampening call to {fn}")
fn.running = asyncio.create_task(fn_now(retry, *args, **kwargs))
fn.running = None
return wrapper
return decorator
async def notify(i3, **kwargs): async def notify(i3, **kwargs):
"""Send a notification with notify-send.""" """Send a notification with notify-send."""
peer = i3.session_bus["org.freedesktop.Notifications"] peer = i3.session_bus["org.freedesktop.Notifications"]
@ -379,20 +426,9 @@ async def workspace_info(i3, event):
@on(I3Event.OUTPUT) @on(I3Event.OUTPUT)
@dampen(2)
async def output_update(i3, event): async def output_update(i3, event):
"""React to a XRandR change.""" """React to a XRandR change."""
running = getattr(output_update, "running", None)
if running is not None:
running.cancel()
output_update.running = None
async def output_update_now():
try:
await asyncio.sleep(2)
except asyncio.CancelledError:
return
output_update.running = None
logger.info("XRandR change detected") logger.info("XRandR change detected")
cmds = ( cmds = (
"systemctl --user reload --no-block xsettingsd.service", "systemctl --user reload --no-block xsettingsd.service",
@ -403,9 +439,6 @@ async def output_update(i3, event):
if proc.returncode != 0: if proc.returncode != 0:
logger.warning(f"{cmd} exited with {proc.returncode}") logger.warning(f"{cmd} exited with {proc.returncode}")
logger.debug("schedule XRandR change")
output_update.running = asyncio.create_task(output_update_now())
@on( @on(
DBusSignal( DBusSignal(
@ -421,7 +454,7 @@ async def bluetooth_notifications(
"""Display notifications related to Bluetooth state.""" """Display notifications related to Bluetooth state."""
if interface != "org.bluez.Device1": if interface != "org.bluez.Device1":
return return
if not "Connected" in changed: if "Connected" not in changed:
return return
logger.info(changed["Connected"]) logger.info(changed["Connected"])
peer = i3.system_bus["org.bluez"][path] peer = i3.system_bus["org.bluez"][path]
@ -451,8 +484,8 @@ async def network_manager_notifications(i3, event, path, state, reason):
ofnm = "org.freedesktop.NetworkManager" ofnm = "org.freedesktop.NetworkManager"
logger.debug(f"from {path} state: {state}, reason: {reason}") logger.debug(f"from {path} state: {state}, reason: {reason}")
if state not in {NM_ACTIVE_CONNECTION_STATE_ACTIVATED}: if state not in {NM_ACTIVE_CONNECTION_STATE_ACTIVATED}:
# We cannot get proper state unless the connection is # Deactivated state does not contain enough information,
# activated, unless we maintain state. # unless we maintain state.
return return
peer = i3.system_bus[ofnm][path] peer = i3.system_bus[ofnm][path]
try: try:
@ -497,39 +530,21 @@ async def network_manager_notifications(i3, event, path, state, reason):
signature="a{sv}", signature="a{sv}",
), ),
) )
@dampen(
1,
unless=lambda i3, event, *args: (
isinstance(event, DBusSignal) and event.interface.endswith(".Active")
),
retry=2,
)
async def network_manager_status(i3, event, *args): async def network_manager_status(i3, event, *args):
"""Compute network manager status.""" """Compute network manager status."""
ofnm = "org.freedesktop.NetworkManager" ofnm = "org.freedesktop.NetworkManager"
# Dampen updates
running = getattr(network_manager_status, "running", None)
if running is not None:
running.cancel()
network_manager_status.running = None
async def network_manager_status_now(sleep=1):
try:
await asyncio.sleep(sleep)
except asyncio.CancelledError:
return
network_manager_status.running = None
try:
await _network_manager_status_now()
except Exception as e:
logger.warning("while updating network status: %s", str(e))
if network_manager_status.running is None:
network_manager_status.running = asyncio.create_task(
network_manager_status_now(5)
)
async def _network_manager_status_now():
status = [] status = []
# Build status from devices # Build status from devices
bus = i3.system_bus[ofnm] bus = i3.system_bus[ofnm]
nm = await bus["/org/freedesktop/NetworkManager"].get_async_interface( nm = await bus["/org/freedesktop/NetworkManager"].get_async_interface(ofnm)
ofnm
)
devices = await nm.AllDevices devices = await nm.AllDevices
for device in devices: for device in devices:
nmd = await bus[device].get_async_interface(f"{ofnm}.Device") nmd = await bus[device].get_async_interface(f"{ofnm}.Device")
@ -573,9 +588,7 @@ async def network_manager_status(i3, event, *args):
# Build status for VPN connection # Build status for VPN connection
connections = await nm.ActiveConnections connections = await nm.ActiveConnections
for conn in connections: for conn in connections:
nma = await bus[conn].get_async_interface( nma = await bus[conn].get_async_interface(f"{ofnm}.Connection.Active")
f"{ofnm}.Connection.Active"
)
vpn = await nma.Vpn vpn = await nma.Vpn
if vpn: if vpn:
state = await nma.State state = await nma.State
@ -610,16 +623,6 @@ async def network_manager_status(i3, event, *args):
network_manager_status.last = status network_manager_status.last = status
if (
isinstance(event, DBusSignal)
and event.interface == f"{ofnm}.Connection.Active"
):
await network_manager_status_now(0)
else:
network_manager_status.running = asyncio.create_task(
network_manager_status_now()
)
async def main(options): async def main(options):
i3 = await Connection().connect() i3 = await Connection().connect()