diff --git a/server/src/grading/grader.ts b/server/src/grading/grader.ts index bd73691..6c0857d 100644 --- a/server/src/grading/grader.ts +++ b/server/src/grading/grader.ts @@ -8,6 +8,8 @@ import type { Check, Expectation, Step } from './checks' import { ProgramDriver } from './driver' const GRADE_CONCURRENCY = 6 +/** 点数据表格的单元格前,最多等多久让那一行出现(程序可能还在查询数据库、刷新表格) */ +const GRID_WAIT = 5000 /** 程序启动后多久内应该显示出窗体 */ const START_TIMEOUT = 10_000 @@ -63,7 +65,11 @@ async function runCheck(dir: string, check: Check, sampleId?: string): Promise gridRows(need(driver, step.control)).length > step.row, GRID_WAIT) const rows = gridRows(node) if (step.row >= rows.length) throw new Failure(`数据表格 ${step.control} 里没有第 ${step.row + 1} 行(现在有 ${rows.length} 行数据)`) const column = typeof step.column === 'number' ? step.column : gridColumns(node).findIndex((c) => c.header === step.column || c.name === step.column) diff --git a/server/src/sandbox.ts b/server/src/sandbox.ts index c3821a8..66e5947 100644 --- a/server/src/sandbox.ts +++ b/server/src/sandbox.ts @@ -76,8 +76,9 @@ export function runCommand(projectDir: string, extraEnv: Record, const cmd = [ // 学生程序写出死循环时,降低它的优先级,网页服务和编译服务仍然流畅 'nice', '-n', '10', - // CPU 时间用完先收到 SIGXCPU 结束,过几秒还在就强制结束 - 'prlimit', `--cpu=${config.cpuLimitSeconds}:${config.cpuLimitSeconds + 5}`, + // CPU 时间用完直接 SIGKILL 结束(软硬上限相同)。软上限更小时内核先发 SIGXCPU,它默认生成 core dump: + // 学生每写一次死循环就留下一个转储,本机桌面还会弹出"程序崩溃" + 'prlimit', `--cpu=${config.cpuLimitSeconds}:${config.cpuLimitSeconds}`, '--core=0', ...bwrapArgs(projectDir), ...(dbDir ? ['--bind', dbDir, DB_DIR] : []), 'dotnet', `${RUNNER_DIR}/${config.runnerDll.split('/').pop()}`, 'run', APP_DIR, @@ -85,12 +86,15 @@ export function runCommand(projectDir: string, extraEnv: Record, return { cmd, env } } -const SIGXCPU = 24 +const SIGKILL = 9 -/** 运行进程退出时,如果是因为 CPU 时间用完,给学生的说明 */ +/** + * 运行进程退出时,如果是因为 CPU 时间用完,给学生的说明。 + * CPU 上限到时内核发 SIGKILL,bwrap 把子进程"被信号结束"转成退出码 128 + 信号编号; + * 平台自己结束程序(停止、超时、关闭页面)用的是 SIGTERM,不会误报。 + */ export function cpuLimitMessage(code: number | null, signal: string | number | null) { - // bwrap 把子进程"被信号结束"转成退出码 128 + 信号编号 - return signal === 'SIGXCPU' || code === 128 + SIGXCPU + return signal === 'SIGKILL' || code === 128 + SIGKILL ? `\n程序占用 CPU 太多,已被结束(累计超过 ${config.cpuLimitSeconds} 秒)。检查是否有死循环:循环里要有退出条件,等待请用 Timer 或 Thread.Sleep。\n` : undefined }