ABAO77 commited on
Commit
ea1e28c
·
verified ·
1 Parent(s): cacee39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -45
app.py CHANGED
@@ -537,7 +537,7 @@ class CodeExecutor:
537
 
538
  logger.info("Using existing .class files, skipping compilation")
539
 
540
- # Execute Java with optimized settings
541
  results = []
542
  for main_file in main_files:
543
  # Determine class name
@@ -562,62 +562,104 @@ class CodeExecutor:
562
  )
563
  continue
564
 
565
- try:
566
- start_time = asyncio.get_event_loop().time()
567
-
568
- # Optimized JVM settings for container environments
569
- java_cmd = [
570
- "java",
571
- "-XX:+UseSerialGC", # Use simple GC for small heap
572
- "-Xmx64m", # Max heap 64MB
573
- "-Xms8m", # Initial heap 8MB
574
- "-XX:MaxMetaspaceSize=64m", # Limit metaspace
575
- "-XX:MaxDirectMemorySize=32m", # Limit direct memory
576
- "-XX:-TieredCompilation", # Disable tiered compilation
577
- "-XX:CICompilerCount=1", # Single compiler thread
578
- "-Djava.awt.headless=true", # Headless mode
579
- "-Dfile.encoding=UTF-8", # UTF-8 encoding
580
- class_name
581
- ]
582
-
583
- stdout, stderr, returncode = await self._execute_with_input(
584
- java_cmd, workspace, input_data
585
- )
586
-
587
- execution_time = asyncio.get_event_loop().time() - start_time
588
-
589
- results.append(
590
- ExecutionResult(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
591
  success=returncode == 0,
592
  stdout=stdout.decode("utf-8", errors="replace"),
593
- stderr=stderr.decode("utf-8", errors="replace"),
594
  execution_time=execution_time,
595
  exit_code=returncode,
596
  )
597
- )
598
-
599
- except asyncio.TimeoutError:
600
- results.append(
601
- ExecutionResult(
 
602
  success=False,
603
  stdout="",
604
  stderr="Execution timeout exceeded",
605
  execution_time=MAX_EXECUTION_TIME,
606
  exit_code=-1,
607
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
608
  )
609
- except Exception as e:
610
- logger.error(f"Java execution error: {str(e)}")
611
- results.append(
612
- ExecutionResult(
613
- success=False,
614
- stdout="",
615
- stderr=str(e),
616
- execution_time=0,
617
- exit_code=-1,
618
- error=str(e),
619
- )
620
- )
621
 
622
  return self._combine_results(results, main_files)
623
 
 
537
 
538
  logger.info("Using existing .class files, skipping compilation")
539
 
540
+ # Execute Java with multiple fallback strategies
541
  results = []
542
  for main_file in main_files:
543
  # Determine class name
 
562
  )
563
  continue
564
 
565
+ # Try different JVM configurations
566
+ jvm_configs = [
567
+ # Config 1: Minimal memory with explicit small code cache
568
+ [
569
+ "-XX:+UseSerialGC",
570
+ "-Xmx32m",
571
+ "-Xms4m",
572
+ "-XX:ReservedCodeCacheSize=16m",
573
+ "-XX:InitialCodeCacheSize=4m",
574
+ "-XX:MaxMetaspaceSize=32m",
575
+ "-XX:CompressedClassSpaceSize=8m",
576
+ "-XX:-TieredCompilation",
577
+ "-XX:CICompilerCount=1",
578
+ "-Xss256k",
579
+ "-XX:+UseStringDeduplication",
580
+ "-XX:MaxDirectMemorySize=16m",
581
+ "-Djava.awt.headless=true",
582
+ "-Dfile.encoding=UTF-8",
583
+ ],
584
+ # Config 2: Even more minimal
585
+ [
586
+ "-Xmx16m",
587
+ "-Xms2m",
588
+ "-XX:ReservedCodeCacheSize=8m",
589
+ "-XX:MaxMetaspaceSize=16m",
590
+ "-XX:-TieredCompilation",
591
+ "-Djava.awt.headless=true",
592
+ ],
593
+ # Config 3: Absolute minimum
594
+ [
595
+ "-Xmx8m",
596
+ "-XX:ReservedCodeCacheSize=4m",
597
+ "-Djava.awt.headless=true",
598
+ ],
599
+ # Config 4: Let JVM decide with hints
600
+ [
601
+ "-XX:+UseSerialGC",
602
+ "-XX:-TieredCompilation",
603
+ "-Djava.awt.headless=true",
604
+ ],
605
+ ]
606
+
607
+ result = None
608
+ for config_idx, jvm_opts in enumerate(jvm_configs):
609
+ try:
610
+ logger.info(f"Trying JVM config {config_idx + 1} for {class_name}")
611
+ start_time = asyncio.get_event_loop().time()
612
+
613
+ java_cmd = ["java"] + jvm_opts + [class_name]
614
+
615
+ stdout, stderr, returncode = await self._execute_with_input(
616
+ java_cmd, workspace, input_data
617
+ )
618
+
619
+ execution_time = asyncio.get_event_loop().time() - start_time
620
+
621
+ # Check if it's a memory-related error
622
+ stderr_text = stderr.decode("utf-8", errors="replace")
623
+ if "Could not reserve" in stderr_text or "insufficient memory" in stderr_text:
624
+ logger.warning(f"Config {config_idx + 1} failed with memory error, trying next...")
625
+ continue
626
+
627
+ result = ExecutionResult(
628
  success=returncode == 0,
629
  stdout=stdout.decode("utf-8", errors="replace"),
630
+ stderr=stderr_text,
631
  execution_time=execution_time,
632
  exit_code=returncode,
633
  )
634
+
635
+ logger.info(f"Java execution completed with config {config_idx + 1}")
636
+ break
637
+
638
+ except asyncio.TimeoutError:
639
+ result = ExecutionResult(
640
  success=False,
641
  stdout="",
642
  stderr="Execution timeout exceeded",
643
  execution_time=MAX_EXECUTION_TIME,
644
  exit_code=-1,
645
  )
646
+ break
647
+ except Exception as e:
648
+ logger.error(f"Error with config {config_idx + 1}: {str(e)}")
649
+ continue
650
+
651
+ # If all configs failed, return error
652
+ if result is None:
653
+ result = ExecutionResult(
654
+ success=False,
655
+ stdout="",
656
+ stderr="Failed to execute Java code with all memory configurations. The container environment may have insufficient memory for JVM.",
657
+ execution_time=0,
658
+ exit_code=-1,
659
+ error="All JVM configurations failed"
660
  )
661
+
662
+ results.append(result)
 
 
 
 
 
 
 
 
 
 
663
 
664
  return self._combine_results(results, main_files)
665