From ed9adafc0bdcfc2c79534282f888df3b250db329 Mon Sep 17 00:00:00 2001
From: Po Lu <luangruo@yahoo.com>
Date: Wed, 17 Aug 2022 10:20:25 +0800
Subject: [PATCH] Avoid disabling device multiple times while handling XI
 attachment events

* src/xterm.c (handle_one_xevent): Avoid disabling devices if we
notice it has been disabled while handling XISlaveDetached or
XISlaveAttached.
---
 src/xterm.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/src/xterm.c b/src/xterm.c
index 7487450d649..a40440e0dad 100644
--- a/src/xterm.c
+++ b/src/xterm.c
@@ -22653,13 +22653,16 @@ handle_one_xevent (struct x_display_info *dpyinfo,
 
 		      if (info)
 			{
-			  if (device && info->enabled)
+			  if (device)
 			    {
 			      device->use = info->use;
 			      device->attachment = info->attachment;
 			    }
-			  else if (device)
-			    disabled[n_disabled++] = hev->info[i].deviceid;
+
+			  /* device could have been disabled by now.
+			     But instead of removing it immediately,
+			     wait for XIDeviceDisabled, or internal
+			     state could be left inconsistent.  */
 
 			  XIFreeDeviceInfo (info);
 			}
-- 
2.39.5