Skip to content

Commit a4609a3

Browse files
authored
Move kernelguard precheck to background (#499)
* Run KernelGuard precheck in background jobs * Increase KernelGuard precheck timeout --------- Co-authored-by: Sinatras <SinatrasC@users.noreply.github.com>
1 parent 66bbcc0 commit a4609a3

5 files changed

Lines changed: 16 additions & 15 deletions

File tree

‎src/kernelbot/api/main.py‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from libkernelbot.background_submission_manager import BackgroundSubmissionManager
1616
from libkernelbot.consts import SubmissionMode
1717
from libkernelbot.db_types import IdentityType
18-
from libkernelbot.kernelguard import KernelGuardRejected, enforce_submission_precheck, should_precheck_submission
1918
from libkernelbot.leaderboard_db import LeaderboardDB, LeaderboardRankedEntry
2019
from libkernelbot.problem_sync import sync_problems
2120
from libkernelbot.submission import (
@@ -564,18 +563,6 @@ async def run_submission_async(
564563
if not req.gpus or len(req.gpus) != 1:
565564
raise HTTPException(status_code=400, detail="Invalid GPU type")
566565

567-
# run KernelGuard pre-check before enqueuing to avoid filling the queue with blocked submissions
568-
if should_precheck_submission(submission_mode_enum):
569-
try:
570-
await asyncio.wait_for(
571-
asyncio.to_thread(enforce_submission_precheck, req.code, req.file_name),
572-
timeout=20.0,
573-
)
574-
except asyncio.TimeoutError as e:
575-
raise HTTPException(status_code=504, detail="KernelGuard pre-check timed out") from e
576-
except KernelGuardRejected as e:
577-
raise HTTPException(status_code=400, detail=str(e)) from e
578-
579566
# put submission request to background manager to run in background
580567
sub_id, job_status_id = await enqueue_background_job(
581568
req, submission_mode_enum, backend_instance, background_submission_manager

‎src/libkernelbot/background_submission_manager.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ async def heartbeat():
234234
reporter = BackgroundSubmissionManagerReporter()
235235
await asyncio.wait_for(
236236
self.backend.submit_full(
237-
item.req, item.mode, reporter, sub_id, skip_precheck=True
237+
item.req, item.mode, reporter, sub_id, skip_precheck=False
238238
),
239239
timeout=HARD_TIMEOUT_SEC,
240240
)

‎src/libkernelbot/kernelguard.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
logger = setup_logging(__name__)
1212

1313
_TRUE_VALUES = {"1", "true", "yes", "on"}
14-
_DEFAULT_TIMEOUT_SEC = 30
14+
_DEFAULT_TIMEOUT_SEC = 5 * 60
1515
_GUARDED_MODES = frozenset(
1616
{
1717
SubmissionMode.BENCHMARK,

‎tests/test_background_submission_manager.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,11 @@ async def test_enqueue_and_run_job(mock_backend):
5757
)
5858
db_context.update_heartbeat_if_active = mock.Mock()
5959

60+
submit_calls = []
61+
6062
# mock submit_full
6163
async def fake_submit_full(req, mode, reporter, sub_id, skip_precheck=False):
64+
submit_calls.append((sub_id, skip_precheck))
6265
await asyncio.sleep(0.01) # simulate a long-running job
6366
return None, None
6467

@@ -90,6 +93,7 @@ async def fake_submit_full(req, mode, reporter, sub_id, skip_precheck=False):
9093
mock.call(42, status="succeeded", last_heartbeat=mock.ANY)
9194
in db_context.upsert_submission_job_status.call_args_list
9295
)
96+
assert submit_calls == [(42, False)]
9397

9498
await manager.stop()
9599

‎tests/test_kernelguard.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ def test_should_precheck_submission_disabled(monkeypatch):
2020
assert not kernelguard.should_precheck_submission(SubmissionMode.BENCHMARK)
2121

2222

23+
def test_timeout_sec_defaults_to_five_minutes(monkeypatch):
24+
monkeypatch.delenv("KERNELGUARD_TIMEOUT_SEC", raising=False)
25+
assert kernelguard._timeout_sec() == 5 * 60
26+
27+
28+
def test_timeout_sec_can_be_overridden(monkeypatch):
29+
monkeypatch.setenv("KERNELGUARD_TIMEOUT_SEC", "240")
30+
assert kernelguard._timeout_sec() == 240
31+
32+
2333
def test_enforce_submission_precheck_rejects_filtered_code(monkeypatch):
2434
monkeypatch.setenv("KERNELGUARD_ENABLED", "1")
2535
monkeypatch.setattr(

0 commit comments

Comments
 (0)