# SDL: x11: Ignore BadWindow errors when handling a SelectionRequest

**URL:** <https://discourse.libsdl.org/t/sdl-x11-ignore-badwindow-errors-when-handling-a-selectionrequest/69225>\
**Category:** SDL Commits\
**Created:** [September 3, 2026, 2:20pm UTC](https://discourse.libsdl.org/t/sdl-x11-ignore-badwindow-errors-when-handling-a-selectionrequest/69225 "2026-09-03T14:20:42Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![SDLPushPostBot](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/sdlpushpostbot/32/4370_2.png) [@SDLPushPostBot](https://discourse.libsdl.org/u/SDLPushPostBot)\
**Post date:** [September 3, 2026, 2:20pm UTC](https://discourse.libsdl.org/t/sdl-x11-ignore-badwindow-errors-when-handling-a-selectionrequest/69225/1 "2026-09-03T14:20:42Z")

</div>

> <https://github.com/libsdl-org/SDL/commit/ec6790bd5e10aa25c1743eb96e48b6ffb2c18267>
>
> When handing a SelectionRequest event, XChangeProperty can generate a BadWindow …error if the requesting window was already destroyed. Register an error handler to catch this, and prevent it from being fatal.

```diff
From ec6790bd5e10aa25c1743eb96e48b6ffb2c18267 Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Wed, 2 Sep 2026 12:15:21 -0400
Subject: [PATCH] x11: Ignore BadWindow errors when handling a SelectionRequest

When handing a SelectionRequest event, XChangeProperty can generate a BadWindow error if the requesting window was already destroyed. Register an error handler to catch this, and prevent it from being fatal.
---
 src/video/x11/SDL_x11events.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/src/video/x11/SDL_x11events.c b/src/video/x11/SDL_x11events.c
index 7a8654582a86f..40cad72eb6bfd 100644
--- a/src/video/x11/SDL_x11events.c
+++ b/src/video/x11/SDL_x11events.c
@@ -792,6 +792,17 @@ static void X11_UpdateUserTime(SDL_WindowData *data, const unsigned long latest)
     }
 }
 
+static int SelectionRequestErrorHandler(Display *d, XErrorEvent *e)
+{
+ // Ignore BadWindow, as it can happen during XChangeProperty if the target window was already destroyed.
+ if (e->error_code != BadWindow) {
+ char err_msg[128];
+ X11_XGetErrorText(d, e->error_code, err_msg, sizeof(err_msg));
+ SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Failed to handle SelectionRequest: %hhu (%s)", e->error_code, err_msg);
+ }
+ return 0;
+}
+
 static void X11_HandleClipboardEvent(SDL_VideoDevice *_this, const XEvent *xevent)
 {
     int i;
@@ -805,6 +816,7 @@ static void X11_HandleClipboardEvent(SDL_VideoDevice *_this, const XEvent *xeven
         // Copy the selection from our own CUTBUFFER to the requested property
     case SelectionRequest:
     {
+ int (*prev_handler)(Display *, XErrorEvent *);
         const XSelectionRequestEvent *req = &xevent->xselectionrequest;
         XEvent sevent;
         int mime_formats;
@@ -823,6 +835,11 @@ static void X11_HandleClipboardEvent(SDL_VideoDevice *_this, const XEvent *xeven
         }
 #endif
 
+ /* If the requesting window was already destroyed, XChangeProperty can generate a BadWindow
+ * error. Register an error handler to catch this, and prevent it from being fatal.
+ */
+ prev_handler = X11_XSetErrorHandler(SelectionRequestErrorHandler);
+
         if (req->selection == XA_PRIMARY) {
             clipboard = &videodata->primary_selection;
         } else {
@@ -880,6 +897,8 @@ static void X11_HandleClipboardEvent(SDL_VideoDevice *_this, const XEvent *xeven
         }
         X11_XSendEvent(display, req->requestor, False, 0, &sevent);
         X11_XSync(display, False);
+
+ X11_XSetErrorHandler(prev_handler);
     } break;
 
     case SelectionNotify:

```
