-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: monitor cpu time usage from server process
- Loading branch information
1 parent
d98bbe5
commit 4102754
Showing
4 changed files
with
43 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,10 @@ | |
# The QuestionPy Server is free software released under terms of the MIT license. See LICENSE.md. | ||
# (c) Technische Universität Berlin, innoCampus <[email protected]> | ||
import resource | ||
import signal | ||
from collections.abc import Callable, Generator | ||
from contextlib import contextmanager | ||
from dataclasses import dataclass | ||
from functools import wraps | ||
from typing import Any, NoReturn, cast, TypeAlias, TypeVar | ||
from typing import NoReturn, cast, TypeAlias, TypeVar | ||
|
||
from questionpy_common.api.qtype import QuestionTypeInterface | ||
from questionpy_common.environment import ( | ||
|
@@ -33,7 +31,6 @@ | |
StartAttempt, | ||
ViewAttempt, | ||
WorkerError, | ||
WorkerTimeLimitExceededError, | ||
) | ||
from questionpy_server.worker.runtime.package import ImportablePackage, load_package | ||
|
||
|
@@ -57,28 +54,6 @@ def register_on_request_callback(self, callback: OnRequestCallback) -> None: | |
OnMessageCallback: TypeAlias = Callable[[M], MessageToServer] | ||
|
||
|
||
def timeout_after(seconds: float) -> Callable[[OnMessageCallback], OnMessageCallback]: | ||
def decorator(function: OnMessageCallback) -> OnMessageCallback: | ||
@wraps(function) | ||
def wrapper(msg: M) -> MessageToServer: | ||
def raise_time_limit_exceeded(*_: Any) -> NoReturn: | ||
raise WorkerTimeLimitExceededError | ||
|
||
# Create a timer that raises after the given amount of cpu time. | ||
signal.signal(signal.SIGVTALRM, raise_time_limit_exceeded) | ||
signal.setitimer(signal.ITIMER_VIRTUAL, seconds) | ||
|
||
try: | ||
return function(msg) | ||
finally: | ||
# Clear the timer. | ||
signal.setitimer(signal.ITIMER_VIRTUAL, 0) | ||
|
||
return wrapper | ||
|
||
return decorator | ||
|
||
|
||
class WorkerManager: | ||
def __init__(self, server_connection: WorkerToServerConnection): | ||
self._connection: WorkerToServerConnection = server_connection | ||
|
@@ -113,10 +88,6 @@ def bootstrap(self) -> None: | |
if self._limits: | ||
# Limit memory usage. | ||
resource.setrlimit(resource.RLIMIT_AS, (self._limits.max_memory, self._limits.max_memory)) | ||
# Limit cpu time usage. | ||
timeout = timeout_after(self._limits.max_cpu_time_seconds_per_call) | ||
for message_id, callback in self._message_dispatch.items(): | ||
self._message_dispatch[message_id] = timeout(callback) | ||
|
||
self._connection.send_message(InitWorker.Response()) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters