File size: 3,597 Bytes
672ecc3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import json,os,signal,subprocess,tempfile,time
from pathlib import Path
from common import ROOT,cached,sha
SOURCE=Path(__file__).parent
def execute_uncached(task,code,tests):
if not tests:return dict(results=[],wall_seconds=0.,total_tests=0)
timeout=int(task['timeout']);budget=(timeout+1)*len(tests)+20
io={'inputs':[t['input'] for t in tests],'outputs':[t['expected'] for t in tests]}
if task['fn_name']:io['fn_name']=task['fn_name']
request=dict(dataset=task['dataset'],input_output=io,timeout=timeout,code=code)
start=time.monotonic()
with tempfile.TemporaryDirectory(prefix='rsi_official_judge_') as td:
root=Path(td);root.chmod(0o755);(root/'judge_child.py').write_bytes((SOURCE/'judge_child.py').read_bytes())
cmd=['unshare','-mnpf','--kill-child=KILL','--','/usr/bin/python3','-I',str(SOURCE/'sandbox_launch.py'),td,str(budget),str(ROOT/'vendor')]
proc=subprocess.Popen(cmd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,start_new_session=True)
try:out,err=proc.communicate(json.dumps(request).encode(),timeout=budget+10)
except subprocess.TimeoutExpired:
os.killpg(proc.pid,signal.SIGKILL);out,err=proc.communicate(timeout=30)
value=dict(results=[-1] if task['dataset']=='apps' else [-3],metadata={'error':'global timeout'},seconds=budget,log='global timeout')
else:
if proc.returncode or not out:
if b'RSI_JUDGE_READY\n' not in err:raise RuntimeError(f'Judge infrastructure failed (exit {proc.returncode}): '+err.decode(errors='replace')[-2000:])
timed_out=proc.returncode in (-signal.SIGXCPU,128+signal.SIGXCPU) or (proc.returncode in (-signal.SIGKILL,128+signal.SIGKILL) and time.monotonic()-start>=budget*.9)
error='Execution process exceeded CPU timeout' if timed_out else f'Execution process exited without a result (exit {proc.returncode})'
raw_status=-1 if task['dataset']=='apps' else -3 if timed_out else -4
value=dict(results=[raw_status],metadata={'error':error,'process_exit_code':proc.returncode,'global_abort':True},seconds=time.monotonic()-start,log=error)
else:
try:value=json.loads(out)
except ValueError:raise RuntimeError('Malformed judge response: '+out.decode(errors='replace')[-300:])
raw=value['results'];meta=value['metadata'];results=[]
if task['dataset']=='apps' and len(raw)==1 and raw[0] is not True and isinstance(raw[0],int) and raw[0]<0:raw=raw*len(tests)
for i,x in enumerate(raw):
if i>=len(tests):raise RuntimeError('Too many official test results')
if x is True:status='AC'
elif x is False:status='WA'
elif task['dataset']=='apps':status='CE' if x==-2 else ('TLE' if 'timeout' in (value['log']+str(meta)).lower() or 'alarm went off' in value['log'] else 'RE')
else:status={-2:'WA',-3:'TLE',-4:'RE'}.get(x,'RE')
t=tests[i];actual=str(meta.get('output',''))[:1024] if i==len(raw)-1 else ''
results.append(dict(test_id=t['test_id'],status=status,actual=actual,expected=str(t['expected']),stderr=(str(meta.get('error_message',meta.get('error','')))+' '+value['log'])[-2048:] if status!='AC' else '',seconds=round(value['seconds']/max(1,len(raw)),4)))
return dict(results=results,wall_seconds=time.monotonic()-start,total_tests=len(tests),official_results=value['results'],official_metadata=meta)
def execute(task,code,tests):
key=dict(dataset=task['dataset'],fn_name=task['fn_name'],code=code,tests=tests,timeout=task['timeout'],runner_sha256=sha(SOURCE/'judge_child.py'),wrapper_sha256=sha(SOURCE/'executor.py'),checker_sha256=sha(ROOT/'vendor'/task['dataset']/'testing_util.py'))
return cached('execution',key,lambda:execute_uncached(task,code,tests))
|