This prevents data from getting lost if we later crash. Fixes #17859 (cherry picked from commit d8d81623)