Skip to content

Commit

Permalink
wl_list_remove leave wl_listener links invalid, not ffi.NULL
Browse files Browse the repository at this point in the history
A listener's link shouldn't be removed more than one time from the
signal's wl_list, but it can, and doing so leads to crashes. The
existing logic seems to imply that `wl_list_remove` leaves the link as a
NULL pointer, blocking subsequent removals. However, the Wayland docs
state "Note: This operation leaves elm [the link] in an invalid state.
" Instead of relying on that, we should set the object's pointer to
`None` and use that to block subsequent removals.
  • Loading branch information
m-col committed Feb 20, 2022
1 parent 471e874 commit 1a2c149
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pywayland/server/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def add_destroy_listener(self, listener: Listener) -> None:
:param listener: The listener object
:type listener: :class:`~pywayland.server.Listener`
"""
assert self._ptr is not None
assert self._ptr is not None and listener._ptr is not None
lib.wl_client_add_destroy_listener(self._ptr, listener._ptr)

@ensure_valid
Expand Down
8 changes: 5 additions & 3 deletions pywayland/server/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,18 @@ def __init__(self, function: Callable) -> None:
self.container = ffi.new("struct wl_listener_container *") # type: ignore[assignment]
self.container.handle = self._handle

self._ptr = ffi.addressof(self.container.destroy_listener)
self._ptr: ffi.ListenerCData | None = ffi.addressof(
self.container.destroy_listener
)
self._ptr.notify = lib.notify_func
self._notify = function
self._signal = None

def remove(self) -> None:
"""Remove the listener"""
if self._ptr.link != ffi.NULL:
if self._ptr and self._ptr.link != ffi.NULL:
lib.wl_list_remove(ffi.addressof(self._ptr.link))
self.link = None
self._ptr = None


class Signal:
Expand Down

0 comments on commit 1a2c149

Please sign in to comment.