{"record":{"id":"3cda9ae58cd0d4c5","repo":"java-native-access/jna","slug":"can-t-query-subwindows","errorCode":null,"errorMessage":"Can't query subwindows","messagePattern":"Can't query subwindows","errorType":"exception","errorClass":"X11Exception","httpStatus":null,"severity":"error","filePath":"contrib/x11/src/jnacontrib/x11/api/X.java","lineNumber":1127,"sourceCode":"\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() ];\n            //Depending on if we're running on 64-bit or 32-bit systems,\n            //the Window ID size may be different; we need to make sure that\n            //we get the data properly no matter what\n            if (X11.XID.SIZE == 4) {\n                int[] windows = children.getValue().getIntArray( 0, childCount.getValue() );\n                for( int x = 0; x < retVal.length; x++ ){\n                    X11.Window win = new X11.Window( windows [ x ] );\n                    retVal[ x ] = new Window( display, win );\n                }\n            }\n            else {","sourceCodeStart":1109,"sourceCodeEnd":1145,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/x11/src/jnacontrib/x11/api/X.java#L1109-L1145","documentation":"Window.getSubwindows calls XQueryTree to enumerate child windows; a return value of 0 indicates the query failed, so \"Can't query subwindows\" is thrown. The most common underlying cause is an invalid window id (the window no longer exists) rather than a permission issue.","triggerScenarios":"Calling getSubwindows() on a Window whose x11Window id is stale/destroyed, or when the X connection is broken.","commonSituations":"Enumerating windows in a loop where targets get destroyed (apps closing) mid-iteration; screensaver/lock switching displays; X server restarts invalidating all window ids.","solutions":["Re-resolve the Window before querying; skip windows that vanish (catch X11Exception per window)","Check the display connection with a lightweight call before batch operations","Filter candidate windows (e.g. via XQueryTree from root) freshly each time rather than caching ids","Catch X11Exception in enumeration loops so one dead child doesn't abort listing"],"exampleFix":"// before\nWindow[] kids = win.getSubwindows();\n// after\nWindow[] kids;\ntry {\n    kids = win.getSubwindows();\n} catch (X11Exception e) {\n    kids = new Window[0]; // window likely destroyed\n}","handlingStrategy":"try-catch","validationCode":"// skip windows that no longer exist before querying children\n// cheap liveness probe:\ntry { win.getParent(); } catch (X11Exception e) { /* window gone: skip */ }","typeGuard":null,"tryCatchPattern":"try {\n    Window[] kids = win.getSubwindows();\n    process(kids);\n} catch (X11Exception e) {\n    log.debug(\"window vanished before XQueryTree: {}\", e.getMessage());\n}","preventionTips":["Never cache Window ids across long operations; re-resolve","Per-window try-catch in enumeration loops so one stale id doesn't abort the batch","Refresh the window list after known churn events (app launch/close)","Expect failures in XQueryTree during window teardown; treat as benign"],"tags":["x11","xquerytree","window-destroyed","stale-handle"],"backgroundTag":"resource-not-found","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}