mirror of
https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
synced 2025-11-02 09:01:46 -05:00
x11: gracefully handle X11 connection error
Perform X11 connection recovery via XSetIOErrorExitHandler mechanism. Implementation is largely inspired by this change to GNOME/mutter https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1447 Part-of: <https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/441>
This commit is contained in:
parent
28f646fd87
commit
b6396dbe9c
8 changed files with 81 additions and 6 deletions
|
|
@ -93,6 +93,8 @@ static void x11_kill_cb(pa_x11_wrapper *w, void *userdata) {
|
|||
pa_assert(u);
|
||||
pa_assert(u->x11_wrapper == w);
|
||||
|
||||
pa_log_debug("X11 client kill callback called");
|
||||
|
||||
if (u->x11_client)
|
||||
pa_x11_client_free(u->x11_client);
|
||||
|
||||
|
|
|
|||
|
|
@ -66,6 +66,8 @@ static void x11_kill_cb(pa_x11_wrapper *w, void *userdata) {
|
|||
pa_assert(u);
|
||||
pa_assert(u->x11_wrapper == w);
|
||||
|
||||
pa_log_debug("X11 client kill callback called");
|
||||
|
||||
if (u->x11_client) {
|
||||
pa_x11_client_free(u->x11_client);
|
||||
u->x11_client = NULL;
|
||||
|
|
|
|||
|
|
@ -116,6 +116,8 @@ static void x11_kill_cb(pa_x11_wrapper *w, void *userdata) {
|
|||
pa_assert(u);
|
||||
pa_assert(u->x11_wrapper == w);
|
||||
|
||||
pa_log_debug("X11 client kill callback called");
|
||||
|
||||
if (u->x11_client)
|
||||
pa_x11_client_free(u->x11_client);
|
||||
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ static void die_cb(SmcConn connection, SmPointer client_data) {
|
|||
|
||||
pa_log_debug("Got die message from XSMP.");
|
||||
|
||||
pa_x11_wrapper_kill(u->x11);
|
||||
pa_x11_wrapper_kill_deferred(u->x11);
|
||||
|
||||
pa_x11_wrapper_unref(u->x11);
|
||||
u->x11 = NULL;
|
||||
|
|
|
|||
|
|
@ -33,6 +33,8 @@
|
|||
|
||||
#include "x11wrap.h"
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
|
||||
typedef struct pa_x11_internal pa_x11_internal;
|
||||
|
||||
struct pa_x11_internal {
|
||||
|
|
@ -51,6 +53,7 @@ struct pa_x11_wrapper {
|
|||
|
||||
pa_defer_event* defer_event;
|
||||
pa_io_event* io_event;
|
||||
pa_defer_event* cleanup_event;
|
||||
|
||||
PA_LLIST_HEAD(pa_x11_client, clients);
|
||||
PA_LLIST_HEAD(pa_x11_internal, internals);
|
||||
|
|
@ -64,6 +67,8 @@ struct pa_x11_client {
|
|||
void *userdata;
|
||||
};
|
||||
|
||||
static void x11_wrapper_kill(pa_x11_wrapper *w);
|
||||
|
||||
/* Dispatch all pending X11 events */
|
||||
static void work(pa_x11_wrapper *w) {
|
||||
pa_assert(w);
|
||||
|
|
@ -167,6 +172,38 @@ static void x11_watch(Display *display, XPointer userdata, int fd, Bool opening,
|
|||
x11_internal_remove(w, (pa_x11_internal*) *watch_data);
|
||||
}
|
||||
|
||||
static int x11_error_handler(Display* display, XErrorEvent* error_event) {
|
||||
pa_log_warn("X11 error handler called");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int x11_io_error_handler(Display* display) {
|
||||
pa_log_warn("X11 I/O error handler called");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void deferred_x11_teardown(pa_mainloop_api *m, pa_defer_event *e, void *userdata) {
|
||||
pa_x11_wrapper *w = userdata;
|
||||
|
||||
m->defer_enable(e, 0);
|
||||
|
||||
pa_log_debug("Start tearing down X11 modules after X11 I/O error");
|
||||
|
||||
x11_wrapper_kill(w);
|
||||
|
||||
pa_log_debug("Done tearing down X11 modules after X11 I/O error");
|
||||
}
|
||||
|
||||
#ifdef HAVE_XSETIOERROREXITHANDLER
|
||||
static void x11_io_error_exit_handler(Display* display, void *userdata) {
|
||||
pa_x11_wrapper *w = userdata;
|
||||
|
||||
pa_log_warn("X11 I/O error exit handler called, preparing to tear down X11 modules");
|
||||
|
||||
pa_x11_wrapper_kill_deferred(w);
|
||||
}
|
||||
#endif
|
||||
|
||||
static pa_x11_wrapper* x11_wrapper_new(pa_core *c, const char *name, const char *t) {
|
||||
pa_x11_wrapper*w;
|
||||
Display *d;
|
||||
|
|
@ -187,11 +224,20 @@ static pa_x11_wrapper* x11_wrapper_new(pa_core *c, const char *name, const char
|
|||
|
||||
w->defer_event = c->mainloop->defer_new(c->mainloop, defer_event, w);
|
||||
w->io_event = c->mainloop->io_new(c->mainloop, ConnectionNumber(d), PA_IO_EVENT_INPUT, display_io_event, w);
|
||||
w->cleanup_event = c->mainloop->defer_new(c->mainloop, deferred_x11_teardown, w);
|
||||
w->core->mainloop->defer_enable(w->cleanup_event, 0);
|
||||
|
||||
XSetErrorHandler(x11_error_handler);
|
||||
XSetIOErrorHandler(x11_io_error_handler);
|
||||
#ifdef HAVE_XSETIOERROREXITHANDLER
|
||||
XSetIOErrorExitHandler(d, x11_io_error_exit_handler, w);
|
||||
#endif
|
||||
XAddConnectionWatch(d, x11_watch, (XPointer) w);
|
||||
|
||||
pa_assert_se(pa_shared_set(c, w->property_name, w) >= 0);
|
||||
|
||||
pa_log_debug("Created X11 connection wrapper '%s'", w->property_name);
|
||||
|
||||
return w;
|
||||
}
|
||||
|
||||
|
|
@ -202,9 +248,12 @@ static void x11_wrapper_free(pa_x11_wrapper*w) {
|
|||
|
||||
pa_assert(!w->clients);
|
||||
|
||||
pa_log_debug("Destroying X11 connection wrapper '%s'", w->property_name);
|
||||
|
||||
XRemoveConnectionWatch(w->display, x11_watch, (XPointer) w);
|
||||
XCloseDisplay(w->display);
|
||||
|
||||
w->core->mainloop->defer_free(w->cleanup_event);
|
||||
w->core->mainloop->io_free(w->io_event);
|
||||
w->core->mainloop->defer_free(w->defer_event);
|
||||
|
||||
|
|
@ -261,7 +310,15 @@ xcb_connection_t *pa_x11_wrapper_get_xcb_connection(pa_x11_wrapper *w) {
|
|||
return XGetXCBConnection(pa_x11_wrapper_get_display(w));
|
||||
}
|
||||
|
||||
void pa_x11_wrapper_kill(pa_x11_wrapper *w) {
|
||||
void pa_x11_wrapper_kill_deferred(pa_x11_wrapper *w) {
|
||||
pa_assert(w);
|
||||
|
||||
/* schedule X11 display teardown */
|
||||
w->core->mainloop->defer_enable(w->cleanup_event, 1);
|
||||
}
|
||||
|
||||
/* Kill the connection to the X11 display */
|
||||
static void x11_wrapper_kill(pa_x11_wrapper *w) {
|
||||
pa_x11_client *c, *n;
|
||||
|
||||
pa_assert(w);
|
||||
|
|
|
|||
|
|
@ -48,8 +48,8 @@ Display *pa_x11_wrapper_get_display(pa_x11_wrapper *w);
|
|||
/* Return the XCB connection object for this connection */
|
||||
xcb_connection_t *pa_x11_wrapper_get_xcb_connection(pa_x11_wrapper *w);
|
||||
|
||||
/* Kill the connection to the X11 display */
|
||||
void pa_x11_wrapper_kill(pa_x11_wrapper *w);
|
||||
/* Initiate X11 connection teardown. */
|
||||
void pa_x11_wrapper_kill_deferred(pa_x11_wrapper *w);
|
||||
|
||||
/* Register an X11 client, that is called for each X11 event */
|
||||
pa_x11_client* pa_x11_client_new(pa_x11_wrapper *w, pa_x11_event_cb_t event_cb, pa_x11_kill_cb_t kill_cb, void *userdata);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue