source: Fix some possible race conditions in Editor.replace_contents