@@ -61,6 +61,16 @@ public class Desugar {
6161 private static final int NUMBER_OF_ENTRIES_PER_SHARD = 100000 ;
6262 private static final Logger logger = Logger .getLogger (Desugar .class .getName ());
6363
64+ private static final Object mutex = new Object ();
65+
66+ // See b/172508621
67+ private static final ImmutableList <String > DESUGAR_JVM_FLAGS =
68+ ImmutableList .of (
69+ "com.android.tools.r8.sortMethodsOnCfWriting" ,
70+ "com.android.tools.r8.allowAllDesugaredInput" ,
71+ "com.android.tools.r8.noCfMarkerForDesugaredCode" ,
72+ "com.android.tools.r8.lambdaClassFieldsNotFinal" );
73+
6474 /** Commandline options for {@link com.google.devtools.build.android.r8.Desugar}. */
6575 @ Parameters (separators = "= " )
6676 public static class DesugarOptions {
@@ -584,10 +594,27 @@ private static void validateOptions(DesugarOptions options) {
584594
585595 private static int processRequest (List <String > args , PrintStream diagnosticsHandlerPrintStream )
586596 throws Exception {
587- DesugarOptions options = parseCommandLineOptions (args .toArray (new String [0 ]));
588- validateOptions (options );
589- new Desugar (options , diagnosticsHandlerPrintStream ).desugar ();
590- return 0 ;
597+ // Execute the desugar workload inside of an atomic mutex lock
598+ // Since JVM flags are applied JVM-wide, we want to ensure that it's impossible for another
599+ // Desugar workload to execute until after the JVM flags have already been reset.
600+ synchronized (mutex ) {
601+ setDesugarJvmFlags ();
602+ int exitCode = 0 ;
603+ try {
604+ DesugarOptions options = parseCommandLineOptions (args .toArray (new String [0 ]));
605+ validateOptions (options );
606+ new Desugar (options , diagnosticsHandlerPrintStream ).desugar ();
607+ exitCode = 0 ;
608+ } catch (Exception e ) {
609+ diagnosticsHandlerPrintStream .println (e .getMessage ());
610+ e .printStackTrace ();
611+ exitCode = 1 ;
612+ } finally {
613+ unsetDesugarJvmFlags ();
614+ }
615+
616+ return exitCode ;
617+ }
591618 }
592619
593620 private static int processRequest (
@@ -624,6 +651,18 @@ private static int runPersistentWorker() {
624651 return 0 ;
625652 }
626653
654+ private static void setDesugarJvmFlags () {
655+ for (String flag : DESUGAR_JVM_FLAGS ) {
656+ System .setProperty (flag , "" );
657+ }
658+ }
659+
660+ private static void unsetDesugarJvmFlags () {
661+ for (String flag : DESUGAR_JVM_FLAGS ) {
662+ System .clearProperty (flag );
663+ }
664+ }
665+
627666 public static void main (String [] args ) throws Exception {
628667 if (args .length > 0 && args [0 ].equals ("--persistent_worker" )) {
629668 System .exit (runPersistentWorker ());
0 commit comments