IT lexicon › Programming › Xlib error handling

Xlib error handling

Programming På svenska → Updated: 2026-10-09

Why an X program can die from an error in a request it sent twenty calls ago, and what XSetErrorHandler and -sync do about it.

Xlib buffers its requests and does not wait for replies unless it has to, so when the server rejects one – BadWindow for a window that has already been destroyed, BadMatch for a pixmap of the wrong depth, BadAccess, BadAlloc, BadValue – the error comes back as an event long after the call that caused it. The default handler prints "X Error of failed request" with the error code, the opcode (Major opcode 20 (X_GetProperty)), the resource ID and two serial numbers, then exits the program. The serials are the clue: the failing request's number against the current one tells you how far behind you are.

XSetErrorHandler swaps in your own handler, and nearly every window manager does, because a client can destroy its window between the moment the manager learns of it and the moment it acts; that race cannot be won, only ignored. XSetIOErrorHandler covers the other case, a broken connection, from which there is no return. For debugging, XSynchronize or the Xt flag -sync makes every request wait for its reply, so the error lands in the right call and a breakpoint on _XError in gdb shows where; the price is a round trip per request. XCB rebuilt the model: errors come back as the reply to the request that caused them, collected with xcb_request_check or from the event queue, and there is no global handler to forget to install.

← Back to the lexicon