Fix bugs with sync offloads

This makes server tests work, but doesn't retrieve client tests
correctly.

BUG=chromium:1008530
TEST=run with test_that

Change-Id: I8ce6047508ad20422a24b56fd8e80ecd8d54091e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/2137977
Commit-Queue: Jacob Kopczynski <[email protected]>
Tested-by: Jacob Kopczynski <[email protected]>
Reviewed-by: Alex Zamorzaev <[email protected]>
diff --git a/server/server_job.py b/server/server_job.py
index 258a942..7beda38 100644
--- a/server/server_job.py
+++ b/server/server_job.py
@@ -42,6 +42,7 @@
 from autotest_lib.server import site_gtest_runner
 from autotest_lib.server import subcommand
 from autotest_lib.server import test
+from autotest_lib.server.autotest import OFFLOAD_ENVVAR
 from autotest_lib.server import utils as server_utils
 from autotest_lib.server.cros.dynamic_suite import frontend_wrappers
 from autotest_lib.server import hosts
@@ -823,13 +824,16 @@
                     utils.open_write_close(server_control_file, control)
 
                 sync_dir = self._offload_dir_target_path()
-                namespace['synchronous_offload_dir'] = sync_dir
                 if self._sync_offload_dir:
                     logging.info("Preparing synchronous offload dir")
                     self.create_marker_file()
                     logging.info("Offload dir and marker file ready")
+                    logging.debug("Results dir is %s", self.resultdir)
+                    logging.debug("Synchronous offload dir is %s", sync_dir)
                 logging.info("Processing control file")
                 namespace['use_packaging'] = use_packaging
+                namespace['synchronous_offload_dir'] = sync_dir
+                os.environ[OFFLOAD_ENVVAR] = sync_dir
                 self._execute_code(server_control_file, namespace)
                 logging.info("Finished processing control file")
 
@@ -888,7 +892,7 @@
         return os.path.join(self.resultdir, self._sync_offload_dir)
 
 
-    def _client_offload_dir_lambda_generator(self, file_path, offload_path):
+    def _client_off_dir_lambda_generator(self, file_path, offload_path):
         """Generate a parallel_simple-runnable function to mirror offload dir
 
         @param file_path: File to copy as the marker file
@@ -926,26 +930,26 @@
             str(datetime.utcnow())
         )
         # Make the server-side directory regardless
-        os.makedirs(
-            self._offload_dir_target_path(force_server=True),
-            exist_ok=True
-        )
+        try:
+          os.makedirs(self._offload_dir_target_path(force_server=True))
+        except OSError as e:
+          if e.errno != errno.EEXIST:
+            raise
         offload_path = self._offload_dir_target_path()
         if self._client:
             _, path = tempfile.mkstemp()
             try:
                 utils.open_write_close(path, marker_string)
                 self.parallel_simple(
-                    self._client_offload_dir_lambda_generator(
-                        path, marker_string, offload_path
-                    ),
+                    self._client_off_dir_lambda_generator(path, offload_path),
                     self.machines
                 )
             finally:
                 os.remove(path)
         else:
             utils.open_write_close(
-                os.path.join(os.path.dirname(offload_path),"marker"),
+                os.path.join(os.path.dirname(offload_path),
+                             "sync_offloads_marker"),
                 marker_string
             )
         return self._offload_dir_target_path()