Skip to contentSkip to conversation

bash: Fix a rare make race build failure

PATCH New Richard Purdie 1 participant 2 messages Jun 30, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 bash: Fix a rare make race build failure

There is a rare make race that occurs in bash due to the way it constructs certain headers and a build tool. Restructure the creation to remove the race.

[YOCTO #14227]

---
 .../bash/bash/makerace2.patch                 | 98 +++++++++++++++++++
 meta/recipes-extended/bash/bash_5.1.8.bb      |  1 +
 2 files changed, 99 insertions(+)
 create mode 100644 meta/recipes-extended/bash/bash/makerace2.patch
meta/recipes-extended/bash/bash/makerace2.patch new file +98 -0
new file mode 100644
index 00000000000..43cdd041579
@@ -0,0 +1,98 @@
+The main makefile can call mkbuiltins from multiple different codepaths in parallel.
+When called, it moves the existing files out the way and creates new ones, then
+compares which will break the build if timing is unlucky.
+
+The root of the problem is mkbuiltins.c creating a file but also referencing that
+file under the same name. By modifing it to allow the final name and the temp name
+to be specified, we can avoid the original reason for the moving of files around.
+This allows them to be created under a new name and then replaced if changed,
+removing any race windows around accessing the files whilst they've been
+moved or are being rewritten.
+
+See [YOCTO #14227]
+
+Upstream-Status: Pending
+Signed-off-by: Richard Purdie <[email protected]>
+
+Index: bash-5.1.8/builtins/Makefile.in
+===================================================================
+--- bash-5.1.8.orig/builtins/Makefile.in
++++ bash-5.1.8/builtins/Makefile.in
+@@ -185,19 +185,17 @@ gen-helpfiles:	tmpbuiltins.o gen-helpfil
+ 	$(CC_FOR_BUILD) ${CCFLAGS_FOR_BUILD} $(LDFLAGS_FOR_BUILD) -o $@ gen-helpfiles.o tmpbuiltins.o $(LIBS_FOR_BUILD)
+ 
+ builtext.h builtins.c: $(MKBUILTINS) $(DEFSRC)
+-	@-if test -f builtins.c; then mv -f builtins.c old-builtins.c; fi
+-	@-if test -f builtext.h; then mv -f builtext.h old-builtext.h; fi
+-	./$(MKBUILTINS) -externfile builtext.h -structfile builtins.c \
++	./$(MKBUILTINS) -externfile builtext-new.h -externfinalfile builtext.h -structfile builtins-new.c \
+ 	    -noproduction $(DIRECTDEFINE) $(HELPDIRDEFINE) $(HELPSTRINGS) $(DEFSRC)
+-	@-if cmp -s old-builtext.h builtext.h 2>/dev/null; then \
+-		mv old-builtext.h builtext.h; \
++	@-if ! cmp -s builtext.h builtext-new.h 2>/dev/null; then \
++		mv builtext-new.h builtext.h; \
+ 	 else \
+-		$(RM) old-builtext.h; \
++		$(RM) builtext-new.h; \
+ 	 fi
+-	@-if cmp -s old-builtins.c builtins.c 2>/dev/null; then \
+-		mv old-builtins.c builtins.c; \
++	@-if ! cmp -s builtins.c builtins-new.c 2>/dev/null; then \
++		mv builtins-new.c builtins.c; \
+ 	 else \
+-		$(RM) old-builtins.c; \
++		$(RM) builtins-new.c; \
+ 	 fi
+ 
+ helpdoc:	gen-helpfiles
+Index: bash-5.1.8/builtins/mkbuiltins.c
+===================================================================
+--- bash-5.1.8.orig/builtins/mkbuiltins.c
++++ bash-5.1.8/builtins/mkbuiltins.c
+@@ -113,6 +113,9 @@ char *struct_filename = (char *)NULL;
+ /* The name of the external declaration file. */
+ char *extern_filename = (char *)NULL;
+ 
++/* The final name of the external declaration file. */
++char *extern_final_filename = (char *)NULL;
++
+ /* Here is a structure for manipulating arrays of data. */
+ typedef struct {
+   int size;		/* Number of slots allocated to array. */
+@@ -230,6 +233,8 @@ main (argc, argv)
+ 
+       if (strcmp (arg, "-externfile") == 0)
+ 	extern_filename = argv[arg_index++];
++      else if (strcmp (arg, "-externfinalfile") == 0)
++	extern_final_filename = argv[arg_index++];
+       else if (strcmp (arg, "-structfile") == 0)
+ 	struct_filename = argv[arg_index++];
+       else if (strcmp (arg, "-noproduction") == 0)
+@@ -273,6 +278,9 @@ main (argc, argv)
+ 	}
+     }
+ 
++  if (!extern_final_filename)
++    extern_final_filename = extern_filename;
++
+   /* If there are no files to process, just quit now. */
+   if (arg_index == argc)
+     exit (0);
+@@ -1174,7 +1182,7 @@ write_file_headers (structfile, externfi
+ 	fprintf (structfile, "%s\n", structfile_header[i]);
+ 
+       fprintf (structfile, "#include \"%s\"\n",
+-	       extern_filename ? extern_filename : "builtext.h");
++	       extern_final_filename ? extern_final_filename : "builtext.h");
+ 
+       fprintf (structfile, "#include \"bashintl.h\"\n");
+ 
+@@ -1184,7 +1192,7 @@ write_file_headers (structfile, externfi
+   if (externfile)
+     fprintf (externfile,
+ 	     "/* %s - The list of builtins found in libbuiltins.a. */\n",
+-	     extern_filename ? extern_filename : "builtext.h");
++	     extern_final_filename ? extern_final_filename : "builtext.h");
+ }
+ 
+ /* Write out any necessary closing information for
meta/recipes-extended/bash/bash_5.1.8.bb +1 -0
index 55d3d0b16ea..5d7704af17b 100644
@@ -14,6 +14,7 @@ SRC_URI = "${GNU_MIRROR}/bash/${BP}.tar.gz;name=tarball \
            file://fix-run-builtins.patch \
            file://use_aclocal.patch \
            file://makerace.patch \
+           file://makerace2.patch \
            "
 
 SRC_URI[tarball.sha256sum] = "0cfb5c9bb1a29f800a97bd242d19511c997a1013815b805e0fdd32214113d6be"
Signature · 1 line
2.30.2
#1 lore raw

PATCH 2/2 sstate/staging: Handle directory creation race issue

The sstate code tries to be careful about racing around directory creation. In particular, the copyhardlinktree code creates the directory tree first allowing for "already exists" errors and ignoring them, then hardlinks the files in.

Unfortunately the sstate removal code can race against this since it will try and remove empty directories. If there is some bad timing, a newly created directory can be removed before it was populated, leading to build failures.

We could try and add locking but this would damage performance, we've been there before. It is also unclear where to actually place locks just based on the contents of a manifest file which may cover multiple sstate install locations for a given task.

Instead, lets disable directory removal in the problematic "shared" core path. This could result in a few more empty directories being left on disk but those should be harmless and better than locking hurting performance or rare build races.

[YOCTO #13999]
[YOCTO #14379]

---
 meta/classes/sstate.bbclass  | 8 +++++---
 meta/classes/staging.bbclass | 6 +++---
 2 files changed, 8 insertions(+), 6 deletions(-)
meta/classes/sstate.bbclass +5 -3
index 72eebd70c91..6c8e64ed00d 100644
@@ -483,7 +483,7 @@ def sstate_clean_cachefiles(d):
         ss = sstate_state_fromvars(ld, task)
         sstate_clean_cachefile(ss, ld)
 
-def sstate_clean_manifest(manifest, d, prefix=None):
+def sstate_clean_manifest(manifest, d, canrace=False, prefix=None):
     import oe.path
 
     mfile = open(manifest)
@@ -501,7 +501,9 @@ def sstate_clean_manifest(manifest, d, prefix=None):
             if entry.endswith("/"):
                 if os.path.islink(entry[:-1]):
                     os.remove(entry[:-1])
-                elif os.path.exists(entry) and len(os.listdir(entry)) == 0:
+                elif os.path.exists(entry) and len(os.listdir(entry)) == 0 and not canrace:
+                    # Removing directories whilst builds are in progress exposes a race. Only
+                    # do it in contexts where it is safe to do so.
                     os.rmdir(entry[:-1])
             else:
                 os.remove(entry)
@@ -539,7 +541,7 @@ def sstate_clean(ss, d):
         for lock in ss['lockfiles']:
             locks.append(bb.utils.lockfile(lock))
 
-        sstate_clean_manifest(manifest, d)
+        sstate_clean_manifest(manifest, d, canrace=True)
 
         for lock in locks:
             bb.utils.unlockfile(lock)
meta/classes/staging.bbclass +3 -3
index 806a85773af..29005ac0b62 100644
@@ -409,7 +409,7 @@ python extend_recipe_sysroot() {
         if os.path.islink(f) and not os.path.exists(f):
             bb.note("%s no longer exists, removing from sysroot" % f)
             lnk = os.readlink(f.replace(".complete", ""))
-            sstate_clean_manifest(depdir + "/" + lnk, d, workdir)
+            sstate_clean_manifest(depdir + "/" + lnk, d, True, workdir)
             os.unlink(f)
             os.unlink(f.replace(".complete", ""))
 
@@ -454,7 +454,7 @@ python extend_recipe_sysroot() {
             fl = depdir + "/" + l
             bb.note("Task %s no longer depends on %s, removing from sysroot" % (mytaskname, l))
             lnk = os.readlink(fl)
-            sstate_clean_manifest(depdir + "/" + lnk, d, workdir)
+            sstate_clean_manifest(depdir + "/" + lnk, d, True, workdir)
             os.unlink(fl)
             os.unlink(fl + ".complete")
 
@@ -475,7 +475,7 @@ python extend_recipe_sysroot() {
                 continue
             else:
                 bb.note("%s exists in sysroot, but is stale (%s vs. %s), removing." % (c, lnk, c + "." + taskhash))
-                sstate_clean_manifest(depdir + "/" + lnk, d, workdir)
+                sstate_clean_manifest(depdir + "/" + lnk, d, True, workdir)
                 os.unlink(depdir + "/" + c)
                 if os.path.lexists(depdir + "/" + c + ".complete"):
                     os.unlink(depdir + "/" + c + ".complete")
Signature · 1 line
2.30.2