From f4022ba872346278c9327c064b67c5308062767d Mon Sep 17 00:00:00 2001 From: whitequark Date: Sat, 20 Jan 2018 01:28:07 +0000 Subject: [PATCH] remoting: avoid a race condition. --- artiq/remoting.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/artiq/remoting.py b/artiq/remoting.py index 0c6a99899..aa3c27f3f 100644 --- a/artiq/remoting.py +++ b/artiq/remoting.py @@ -83,8 +83,16 @@ class SSHClient(Client): logger.debug("Using cached {}".format(filename)) else: logger.debug("Transferring {}".format(filename)) - with sftp.open(remote_filename, 'wb') as remote: + # Avoid a race condition by writing into a temporary file + # and atomically replacing + with sftp.open(remote_filename + ".~", "wb") as remote: remote.write(rewritten) + try: + sftp.rename(remote_filename + ".~", remote_filename) + except IOError: + # Either it already exists (this is OK) or something else + # happened (this isn't) and we need to re-raise + sftp.stat(remote_filename) return remote_filename def spawn_command(self, cmd, get_pty=False, **kws):