I also experience this problem sometimes, and especially when I'm using Dropbox like so: edit a file locally which is synced via Dropbox to a machine on which I run sage. This really triggers the problem.
Salvatore Stella (CCed) also had this problem and tried using this setting in vim. It seems to help, but it didn't solve the problem. It appears to depend on the latency of the network (i.e., this still happens if you are saving to a network drive).
To fix it, he patched his version of sage like so:
diff --git a/src/sage/repl/attach.py b/src/sage/repl/attach.py
index 9ebc3ef..00df8d4 100644
--- a/src/sage/repl/attach.py
+++ b/src/sage/repl/attach.py
@@ -514,9 +514,12 @@ def modified_file_iterator():
for filename in attached.keys():
old_tm = attached[filename]
if not os.path.exists(filename):
- print('### detaching file {0} because it does not exist (deleted?) ###'.format(filename))
- detach(filename)
- continue
+ # check again in a while to prevent race conditions on some editors
+ time.sleep(0.1)
+ if not os.path.exists(filename):
+ print('### detaching file {0} because it does not exist (deleted?) ###'.format(filename))
+ detach(filename)
+ continue
new_tm = os.path.getmtime(filename)
if new_tm > old_tm:
modified[filename] = new_tm