This patch by Ben Hutchings . xprop -spy should exit cleanly when the target window is destroyed. The current behaviour is that it sometimes dies with a BadWindow error and sometimes hangs around after the target has been destroyed. We fix this by listening for destroy events but also catching BadWindow errors (and BadMatch, which may sometimes be received instead of BadWindow). We print a new-line before exiting from the error handler, since we may have generated partial output for a property change. --- x11-utils.orig/xprop/xprop.c +++ x11-utils/xprop/xprop.c @@ -1596,6 +1596,19 @@ static int spy = 0; +static int (*old_error_handler)(Display *dpy, XErrorEvent *ev); + +static int spy_error_handler(Display *dpy, XErrorEvent *ev) +{ + if (ev->error_code == BadWindow || ev->error_code == BadMatch) { + /* Window was destroyed */ + puts(""); + exit(0); + } + + return old_error_handler(dpy, ev); +} + int main (int argc, char **argv) { @@ -1738,9 +1750,14 @@ XEvent event; const char *format, *dformat; - XSelectInput(dpy, target_win, PropertyChangeMask); + XSelectInput(dpy, target_win, PropertyChangeMask | StructureNotifyMask); + old_error_handler = XSetErrorHandler(spy_error_handler); for (;;) { XNextEvent(dpy, &event); + if (event.type == DestroyNotify) + break; + if (event.type != PropertyNotify) + continue; format = dformat = NULL; if (props) { int i;