소스 검색

Startup: Avoid a race condition (#548)

* Startup: Avoid a race condition

* Just add an argument

* Bump up the copyright year

* Address Vaclav's comments
Huidae Cho 5 년 전
부모
커밋
f203dc40c0
1개의 변경된 파일12개의 추가작업 그리고 5개의 파일을 삭제
  1. 12 5
      lib/init/grass.py

+ 12 - 5
lib/init/grass.py

@@ -18,7 +18,7 @@
 #               command line options for setting the GISDBASE, LOCATION,
 #               command line options for setting the GISDBASE, LOCATION,
 #               and/or MAPSET. Finally it starts GRASS with the appropriate
 #               and/or MAPSET. Finally it starts GRASS with the appropriate
 #               user interface and cleans up after it is finished.
 #               user interface and cleans up after it is finished.
-# COPYRIGHT:    (C) 2000-2019 by the GRASS Development Team
+# COPYRIGHT:    (C) 2000-2020 by the GRASS Development Team
 #
 #
 #               This program is free software under the GNU General
 #               This program is free software under the GNU General
 #               Public License (>=v2). Read the file COPYING that
 #               Public License (>=v2). Read the file COPYING that
@@ -510,8 +510,10 @@ def read_env_file(path):
     return kv
     return kv
 
 
 
 
-def write_gisrc(kv, filename):
-    f = open(filename, 'w')
+def write_gisrc(kv, filename, append=False):
+    # use append=True to avoid a race condition between write_gisrc() and
+    # grass_prompt() on startup (PR #548)
+    f = open(filename, 'a' if append else 'w')
     for k, v in kv.items():
     for k, v in kv.items():
         f.write("%s: %s\n" % (k, v))
         f.write("%s: %s\n" % (k, v))
     f.close()
     f.close()
@@ -2318,9 +2320,14 @@ def main():
 
 
         # start GUI and register shell PID in rc file
         # start GUI and register shell PID in rc file
         start_gui(grass_gui)
         start_gui(grass_gui)
-        kv = read_gisrc(gisrc)
+        kv = {}
         kv['PID'] = str(shell_process.pid)
         kv['PID'] = str(shell_process.pid)
-        write_gisrc(kv, gisrc)
+
+        # grass_prompt() tries to read gisrc while write_gisrc() is adding PID
+        # to this file, so don't rewrite it; just append PID to make it
+        # available to grass_prompt() at all times (PR #548)
+        write_gisrc(kv, gisrc, append=True)
+
         exit_val = shell_process.wait()
         exit_val = shell_process.wait()
         if exit_val != 0:
         if exit_val != 0:
             warning(_("Failed to start shell '%s'") % os.getenv('SHELL'))
             warning(_("Failed to start shell '%s'") % os.getenv('SHELL'))