# Phase 10 — Robustness & Cleanup --- ## Step 10.1 — Proper error handling - Set `XSetErrorHandler()` to a handler that logs but doesn't crash on `BadWindow`, `BadPixmap`, `BadDrawable` (windows can vanish between operations). - Wrap all X calls that reference a window in the handler's error-checked scope. - On fatal errors (`XSetIOErrorHandler`), clean up gracefully. **Verify:** Rapidly spawn and kill clients (a stress loop script). No crashes, no X error floods. Clean log output. --- ## Step 10.2 — Clean shutdown On `SIGINT`/`SIGTERM` or raylib `WindowShouldClose()`: 1. Unredirect all windows (`XCompositeUnredirectSubwindows`). 2. Free all pixmaps, textures, damage objects. 3. Release the overlay (if used later). 4. `XCloseDisplay()`. 5. `CloseWindow()` (raylib). **Verify:** Killing the WM with Ctrl+C cleanly restores windows in Xephyr (they become visible via X's normal rendering again). No orphaned resources. --- ## Step 10.3 — Stress test script Create `bin/stress-test.sh` that: 1. Starts Xephyr. 2. Starts the WM. 3. Rapidly spawns 20 `xterm` instances. 4. Randomly moves, resizes, and closes them via `xdotool`. 5. Checks that `wmctrl -l` count matches expectations. 6. Screenshots the result. 7. Tears everything down. **Verify:** No crashes, no hangs, no leaked windows. Screenshot looks sane.