{"record":{"id":"1d2dbafb61c4012e","repo":"java-native-access/jna","slug":"cannot-send-msg-event","errorCode":null,"errorMessage":"Cannot send <msg> event.","messagePattern":"Cannot send <msg> event\\.","errorType":"exception","errorClass":"X11Exception","httpStatus":null,"severity":"error","filePath":"contrib/x11/src/jnacontrib/x11/api/X.java","lineNumber":1116,"sourceCode":"            event.serial = new NativeLong(0);\n            event.send_event = 1;\n            event.message_type = display.getAtom(msg);\n            event.window = x11Window;\n            event.format = 32;\n            event.data.setType(NativeLong[].class);\n            event.data.l[0] = data0;\n            event.data.l[1] = data1;\n            event.data.l[2] = data2;\n            event.data.l[3] = data3;\n            event.data.l[4] = data4;\n\n            X11.XEvent e = new X11.XEvent();\n            e.setTypedValue(event);\n\n            if (x11.XSendEvent(display.x11Display, display.getRootWindow().x11Window, 0, mask, e) != 0) {\n                return X11.Success;\n            } else {\n                throw new X11Exception(\"Cannot send \" + msg + \" event.\");\n            }\n        }\n\n        public Window[] getSubwindows() throws X11Exception {\n            WindowByReference root = new WindowByReference();\n            WindowByReference parent = new WindowByReference();\n            PointerByReference children = new PointerByReference();\n            IntByReference childCount = new IntByReference();\n\n            if (x11.XQueryTree(display.x11Display, x11Window, root, parent, children, childCount) == 0){\n                throw new X11Exception(\"Can't query subwindows\");\n            }\n\n            if( childCount.getValue() == 0 ){\n                return null;\n            }\n\n            Window[] retVal = new Window[ childCount.getValue() ];","sourceCodeStart":1098,"sourceCodeEnd":1134,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/x11/src/jnacontrib/x11/api/X.java#L1098-L1134","documentation":"Window.sendEvent wraps XSendEvent: when the call returns 0 (failure) instead of non-zero, the method throws \"Cannot send <msg> event.\" where <msg> is a caller-supplied label (e.g. _NET_CLOSE_WINDOW, _NET_ACTIVE_WINDOW). Failure usually means a bad X connection or bad event/parameters.","triggerScenarios":"Sending a client message such as _NET_ACTIVE_WINDOW or _NET_CLOSE_WINDOW via sendEvent when the X server rejects it: invalid target window id, malformed XEvent struct, or dead display connection.","commonSituations":"Sending an event to a window that was closed between lookup and send, running on a disconnected/terminating X session, or JNA XEvent layout mismatch producing an X server BadValue error.","solutions":["Re-acquire the target Window handle immediately before sending","Verify the X connection is alive (try a cheap call like getDefaultRootWindow first)","Check that the event struct/atom name matches what the WM expects (EWMH client messages need message_type + data.l[0..4])","Catch X11Exception around sendEvent and treat failure as a user-visible 'operation failed'"],"exampleFix":"// before\nwin.sendEvent(x11.XInternAtom(display, \"_NET_ACTIVE_WINDOW\", false), ...);\n// after\ntry {\n    win.sendEvent(x11.XInternAtom(display, \"_NET_ACTIVE_WINDOW\", false), ...);\n} catch (X11Exception e) {\n    // window may have closed; re-resolve and retry once\n    win = x.getWindowByName(title);\n    if (win != null) win.sendEvent(...);\n}","handlingStrategy":"try-catch","validationCode":"// ensure target window still exists before sending\nWindow[] wins = x.getWindows(); // re-resolve; null/absent means skip send\nboolean targetAlive = java.util.Arrays.stream(wins).anyMatch(w -> w == win);","typeGuard":null,"tryCatchPattern":"try {\n    win.sendEvent(atom, detail, mask);\n} catch (X11Exception e) {\n    Window fresh = x.getWindowByName(title);\n    if (fresh != null) fresh.sendEvent(atom, detail, mask);\n    else log.warn(\"target window gone, event not sent\");\n}","preventionTips":["Re-resolve window ids immediately before sending events","Validate EWMH client-message layout (message_type + data.l[0..4])","Detect dead X connections early with a cheap call","Make event sends idempotent/retryable"],"tags":["x11","xsendevent","client-message","ewmh"],"backgroundTag":"api-request-failed","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}