The actual fix is the 2nd commit. The first commit adds some debugging that was useful to track this down. And the last two commits are small unrelated fixes noticed in the process of debugging this.
Turns out glamor is a good stress test of batch reordering.
Fixes #2992 (closed)