Fix apps crashing when quickly plugging and unplugging a monitor

When we plug in a monitor, we register a global kde_output_device_v2 to clients. If we unplug the monitor and delete the global while a client is trying to bind it, we would send a client a global_remove message before messages to the bound object ID. This caused Wayland clients to not recognize the object IDs the server tried sending to the client. The next time the server tried sending the client an object ID, it would be greater than the last object ID the client saw + 1, causing the client app to exit with errors like:

not a valid new object id (4278190164), message mode(n)
The Wayland connection experienced a fatal error: Invalid argument

Fix this bug by not sending messages from objects belonging to globals we've already removed.

This is safe because both binding (from wl_event_loop_dispatch()) and removing outputs (from KWin::DrmBackend::handleUdevEvent()) run in the same main thread.

BUG: 510734

Status

This is a quick and dirty pull request based on my current understanding of Wayland protocols and the underlying bug. See https://bugs.kde.org/show_bug.cgi?id=510734#:~:text=%23%23%20Next%20Steps for open questions. I do not have strong confidence that the program logic/lifetimes/concurrency is watertight for all situations, and this PR should be reviewed (and documented?) before merging.

  • When a global is removed, why can't the client parse messages from its bound object IDs? Did it see the global_remove() and invalidate (forget the interface/type of) all object IDs based on those globals, or were the messages sent from the server corrupted in some way?
  • Is it invalid to send messages from an object ID after the global is deleted?

I think that when a client requests to bind a global to an object, but we've destroyed it on our side, the best thing to do is to successfully return an object but never send any messages from it, and wait for the client to delete the object. (Does destroying a global delete the object? IDK.) https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_registry-event-global_remove covers the other direction (clients sending messages to a removed global).

Testing

In my testing (patch applied to v6.4.91), this MR successfully prevents crashing when we remove a global before sending metadata to the client (if we remove the global before replying to the object binding, we never reply to the bind at all):

(unfixed) Leaking object IDs

Reading these logs, it appears that the server removes the globals for kde_output_device_v2 and wl_output, the client frees the objects for zxdg_output_v1 and wl_output, then 4.5 seconds later the server deletes their object IDs. But neither the client nor the server frees kde_output_device_v2#66, so the object is leaked for the lifetime of the application.

kde_output_device_v2 is still leaked even when not unplugging the monitor quickly:

Upon plugging in the VGA DAC (apparently it didn't recognize the monitor I2C EDID emulator?), we allocate kde_output_device_v2#66. I wait 10 seconds before unplugging the monitor, and 10 more seconds before closing the window, but we never see delete_id(66) and #66 is never reused again (whereas zxdg_output_v1#67 is deleted and reused).

I noticed that 5 seconds after unplugging a monitor, I get a series of discarded [unknown]#4278190226.[event 3](0 fd, 8 byte) from various kde_output_device_mode_v2 which were recognized by the client? Is it an attempt to free the mode object? In any case it doesn't appear to work, as if I disconnect a monitor then reconnect it again, these IDs are never reused and we start counting up from the largest previous ID:

Edited by Méven Car

Merge request reports

Loading
Loading