From 2969c8807e898fa09b1eadb5664d73ef7a80e6f5 Mon Sep 17 00:00:00 2001 From: hanzhijian Date: Mon, 25 May 2026 13:02:02 +0800 Subject: [PATCH] arch/sim: Stop publishing stale X11 display during teardown. sim_x11events() polls g_display from the idle loop while the framebuffer teardown path closes the X connection. Clear the global Display handle before teardown so the event path stops using it, but keep the saved local Display pointer for XShmDetach(), XUngrabButton(), and XCloseDisplay(). This only changes the sim X11 framebuffer shutdown ordering and does not change user-visible APIs or build configuration. Signed-off-by: hanzhijian --- arch/sim/src/sim/posix/sim_x11framebuffer.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/arch/sim/src/sim/posix/sim_x11framebuffer.c b/arch/sim/src/sim/posix/sim_x11framebuffer.c index 68173ff5ee4..6b58be5d522 100644 --- a/arch/sim/src/sim/posix/sim_x11framebuffer.c +++ b/arch/sim/src/sim/posix/sim_x11framebuffer.c @@ -217,15 +217,24 @@ static int sim_x11untraperrors(Display *display) static void sim_x11uninit(void) { + Display *display; + if (g_display == NULL) { return; } + /* Publish shutdown before tearing down the X connection so the event + * polling path stops touching a stale Display pointer. + */ + + display = g_display; + g_display = NULL; + #ifndef CONFIG_SIM_X11NOSHM if (g_shmcheckpoint > 4) { - XShmDetach(g_display, &g_xshminfo); + XShmDetach(display, &g_xshminfo); } if (g_shmcheckpoint > 3) @@ -251,10 +260,10 @@ static void sim_x11uninit(void) #if defined(CONFIG_SIM_TOUCHSCREEN) || defined(CONFIG_SIM_AJOYSTICK) || \ defined(CONFIG_SIM_BUTTONS) - XUngrabButton(g_display, Button1, AnyModifier, g_window); + XUngrabButton(display, Button1, AnyModifier, g_window); #endif - XCloseDisplay(g_display); + XCloseDisplay(display); } /****************************************************************************