[1.7.x] Fixed #22882 -- Fixed a UnicodeDecodeError in flush.

Thanks djbaldey for the report.

Backport of 0e4d79c4f0 from master