fix: recover interrupted Web battles in 2.0.10
This commit is contained in:
@@ -1363,6 +1363,8 @@ class ExperimentalBattleRuntime:
|
||||
diagnostics=[{"code": "static_data", "message": str(exc)}])
|
||||
|
||||
def run(self, request, *, cancel_event=None, timeout=30.0):
|
||||
import tempfile
|
||||
|
||||
timeout = _number(timeout, "timeout")
|
||||
if timeout < 0:
|
||||
raise SimulationInputError("timeout must be nonnegative")
|
||||
@@ -1380,13 +1382,18 @@ class ExperimentalBattleRuntime:
|
||||
started = time.monotonic()
|
||||
process = None
|
||||
try:
|
||||
process = subprocess.Popen(
|
||||
[sys.executable, "-m", "ranch_assistant.battle_simulator", "--worker"],
|
||||
cwd=str(Path(__file__).resolve().parent.parent),
|
||||
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||
creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0),
|
||||
)
|
||||
pending_input = wire
|
||||
# POSIX communicate(None) retries do not resume a partial stdin write.
|
||||
# A bounded, rewound file gives the child all input and EOF once;
|
||||
# its inherited handle remains valid after the parent closes its copy.
|
||||
with tempfile.TemporaryFile(mode="w+b") as input_stream:
|
||||
input_stream.write(wire)
|
||||
input_stream.seek(0)
|
||||
process = subprocess.Popen(
|
||||
[sys.executable, "-m", "ranch_assistant.battle_simulator", "--worker"],
|
||||
cwd=str(Path(__file__).resolve().parent.parent),
|
||||
stdin=input_stream, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||
creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0),
|
||||
)
|
||||
while True:
|
||||
if cancel_event is not None and cancel_event.is_set():
|
||||
return _execution_failure("cancelled", "worker_cancelled")
|
||||
@@ -1394,10 +1401,10 @@ class ExperimentalBattleRuntime:
|
||||
if left <= 0:
|
||||
return _execution_failure("timeout", "worker_wall_clock_limit")
|
||||
try:
|
||||
stdout, stderr = process.communicate(input=pending_input, timeout=min(.05, left))
|
||||
stdout, stderr = process.communicate(timeout=min(.05, left))
|
||||
break
|
||||
except subprocess.TimeoutExpired:
|
||||
pending_input = None
|
||||
pass
|
||||
if len(stdout) > MAX_WIRE_BYTES or len(stderr) > 65536:
|
||||
return _execution_failure("error", "worker_output_exceeds_limit")
|
||||
result = json.loads(stdout)
|
||||
|
||||
Reference in New Issue
Block a user