printoperation: fix another case where operation may complete twice
authorMichael Catanzaro <mcatanzaro@redhat.com>
Tue, 26 Sep 2023 23:52:37 +0000 (18:52 -0500)
committerMatthias Clasen <mclasen@redhat.com>
Thu, 28 Sep 2023 00:42:45 +0000 (20:42 -0400)
commit39560c0914d0fc5ffcb026bff13926c94d05e341
treea06ca45394b22dc6a3c206f8ccde76db4bc69b0a
parent29a243e3d78f13e94e25517bf95323c3537a118c
printoperation: fix another case where operation may complete twice

This is a little tricky. At first, I thought we had a codepath where we
fail to schedule the idle that completes the print operation: if we take
the gtk_print_backend_printer_list_is_done path for each printer
backend, then printer_list_done_cb() is never executed and we never
schedule the idle. But in fact, in this case, then backends == NULL at
the bottom of find_printer(), and we'll schedule the idle there, so it's
OK. Except it's not really OK, because we'll schedule it even if a
printer was already found, resulting in the callback completing twice
and a double free.

Simplify this. Schedule the idle in find_printer() only if there are
*initially* no backends, not also if all backends are immediately ready
and already removed from consideration. Instead, always call
printer_list_done_cb() for every backend in find_printer_init(). After
the previous commit, printer_list_done_cb() will schedule the idle when
appropriate.

printer_list_done_cb() additionally disconnects signals that we did not
connect in this codepath, but it does so using
g_signal_handlers_disconnect_by_func, which is harmless. Otherwise, the
only extra work it's doing is scheduling the idle, and that's exactly
what find_printer_init() is missing.
gtk/print/gtkprintoperation-unix.c