Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -537,7 +537,7 @@ class CodeExecutor:
|
|
| 537 |
|
| 538 |
logger.info("Using existing .class files, skipping compilation")
|
| 539 |
|
| 540 |
-
# Execute Java with
|
| 541 |
results = []
|
| 542 |
for main_file in main_files:
|
| 543 |
# Determine class name
|
|
@@ -562,62 +562,104 @@ class CodeExecutor:
|
|
| 562 |
)
|
| 563 |
continue
|
| 564 |
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
"
|
| 571 |
-
"-
|
| 572 |
-
"-
|
| 573 |
-
"-
|
| 574 |
-
"-XX:MaxMetaspaceSize=
|
| 575 |
-
"-XX:
|
| 576 |
-
"-XX:-TieredCompilation",
|
| 577 |
-
"-XX:CICompilerCount=1",
|
| 578 |
-
"-
|
| 579 |
-
"-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 591 |
success=returncode == 0,
|
| 592 |
stdout=stdout.decode("utf-8", errors="replace"),
|
| 593 |
-
stderr=
|
| 594 |
execution_time=execution_time,
|
| 595 |
exit_code=returncode,
|
| 596 |
)
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
|
|
|
| 602 |
success=False,
|
| 603 |
stdout="",
|
| 604 |
stderr="Execution timeout exceeded",
|
| 605 |
execution_time=MAX_EXECUTION_TIME,
|
| 606 |
exit_code=-1,
|
| 607 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 608 |
)
|
| 609 |
-
|
| 610 |
-
|
| 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 |
|