mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: fix judge race condition
This commit is contained in:
1 file changed
+17
-2
@@ -7,6 +7,7 @@ import sanitize from 'sanitize-filename';
|
|||||||
import {
|
import {
|
||||||
JudgeMeta, JudgeResultBody, ProblemConfigFile, TestCase,
|
JudgeMeta, JudgeResultBody, ProblemConfigFile, TestCase,
|
||||||
} from '@hydrooj/common';
|
} from '@hydrooj/common';
|
||||||
|
import { sleep } from '@hydrooj/utils';
|
||||||
import { Context } from '../context';
|
import { Context } from '../context';
|
||||||
import {
|
import {
|
||||||
BadRequestError, FileLimitExceededError, ForbiddenError, ProblemIsReferencedError, ValidationError,
|
BadRequestError, FileLimitExceededError, ForbiddenError, ProblemIsReferencedError, ValidationError,
|
||||||
@@ -281,7 +282,18 @@ export class JudgeConnectionHandler extends ConnectionHandler {
|
|||||||
|
|
||||||
async newTask(t: Task) {
|
async newTask(t: Task) {
|
||||||
const rid = t.rid.toHexString();
|
const rid = t.rid.toHexString();
|
||||||
this.tasks[rid] = new JudgeResultCallbackContext(this.ctx, t);
|
const context = new JudgeResultCallbackContext(this.ctx, t);
|
||||||
|
if (this.tasks[rid]) {
|
||||||
|
for (let i = 1; i <= 300; i++) {
|
||||||
|
if (!this.tasks[rid]) break;
|
||||||
|
if (i === 300) {
|
||||||
|
context.end({ message: 'Wait for previous judge timeout', status: STATUS.STATUS_SYSTEM_ERROR });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await sleep(1000); // eslint-disable-line no-await-in-loop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.tasks[rid] = context;
|
||||||
this.send({ task: t });
|
this.send({ task: t });
|
||||||
this.tasks[rid].next({ status: STATUS.STATUS_FETCHED });
|
this.tasks[rid].next({ status: STATUS.STATUS_FETCHED });
|
||||||
await this.tasks[rid];
|
await this.tasks[rid];
|
||||||
@@ -296,7 +308,10 @@ export class JudgeConnectionHandler extends ConnectionHandler {
|
|||||||
}
|
}
|
||||||
if (['next', 'end'].includes(msg.key)) {
|
if (['next', 'end'].includes(msg.key)) {
|
||||||
const t = this.tasks[msg.rid];
|
const t = this.tasks[msg.rid];
|
||||||
if (!t) return;
|
if (!t) {
|
||||||
|
logger.warn('Unknown judge rid reported: %s', msg.rid);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (msg.key === 'next') t.next(msg);
|
if (msg.key === 'next') t.next(msg);
|
||||||
if (msg.key === 'end') t.end(msg.nop ? undefined : { judger: this.user._id, ...msg });
|
if (msg.key === 'end') t.end(msg.nop ? undefined : { judger: this.user._id, ...msg });
|
||||||
} else if (msg.key === 'status') {
|
} else if (msg.key === 'status') {
|
||||||
|
|||||||
Reference in new issue
Block a user