File size: 12,120 Bytes
358dd8b | 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | import asyncio
import json
import os
import platform
import signal
import tempfile
from asyncio.subprocess import Process
from datetime import datetime
from uuid import UUID, uuid4
import psutil
from kimina_client import (
Command,
CommandResponse,
Diagnostics,
Error,
Infotree,
ReplResponse,
Snippet,
)
from loguru import logger
from rich.syntax import Syntax
from .db import db
from .errors import LeanError, ReplError
from .logger import console
from .models import ReplStatus
from .prisma_client import prisma
from .settings import Environment, settings
from .utils import is_blank
log_lock = asyncio.Lock()
async def log_snippet(uuid: UUID, snippet_id: str, code: str) -> None:
if settings.environment == Environment.prod:
header = f"[{uuid.hex[:8]}] Running snippet {snippet_id}:"
async with log_lock:
logger.info(header)
# Log the code as part of the message or in a separate log entry
logger.info(f"Code snippet:\n{code or '<empty>'}")
else:
header = f"\\[{uuid.hex[:8]}] Running snippet [bold magenta]{snippet_id}[/bold magenta]:"
syntax = Syntax(
code or "<empty>",
"lean",
theme="monokai",
line_numbers=False,
word_wrap=True,
)
async with log_lock:
logger.info(header)
if console:
console.print(syntax)
class Repl:
def __init__(
self,
uuid: UUID,
created_at: datetime,
header: str = "",
*,
max_repl_mem: int,
max_repl_uses: int,
) -> None:
self.uuid = uuid
self.header = header
self.use_count = 0
self.created_at = created_at
self.last_check_at = created_at
# Stores the response received when running the import header.
self.header_cmd_response: ReplResponse | None = None
self.proc: Process | None = None
self.error_file = tempfile.TemporaryFile("w+")
self.max_memory_bytes = max_repl_mem * 1024 * 1024
self.max_repl_uses = max_repl_uses
self._loop: asyncio.AbstractEventLoop | None = None
# REPL statistics
self.cpu_per_exec: dict[int, float] = {}
self.mem_per_exec: dict[int, int] = {}
# Vars that hold max CPU / mem usage per proof.
self._cpu_max: float = 0.0 # CPU as a percentage of a single core
self._mem_max: int = 0
self._ps_proc: psutil.Process | None = None
self._cpu_task: asyncio.Task[None] | None = None
self._mem_task: asyncio.Task[None] | None = None
@classmethod
async def create(cls, header: str, max_repl_uses: int, max_repl_mem: int) -> "Repl":
if db.connected:
record = await prisma.repl.create(
data={
"header": header,
"max_repl_uses": max_repl_uses,
"max_repl_mem": max_repl_mem,
}
)
return cls(
uuid=UUID(record.uuid),
created_at=record.created_at,
header=record.header,
max_repl_uses=record.max_repl_uses,
max_repl_mem=record.max_repl_mem,
)
return cls(
uuid=uuid4(),
created_at=datetime.now(),
header=header,
max_repl_uses=max_repl_uses,
max_repl_mem=max_repl_mem,
)
@property
def exhausted(self) -> bool:
if self.max_repl_uses < 0:
return False
if self.header and not is_blank(self.header):
# Header does not count towards uses.
return self.use_count >= self.max_repl_uses + 1
return self.use_count >= self.max_repl_uses
async def start(self) -> None:
# TODO: try/catch this bit and raise as REPL startup error.
self._loop = asyncio.get_running_loop()
def _preexec() -> None:
import resource
# Memory limit
if platform.system() != "Darwin": # Only for Linux
resource.setrlimit(
resource.RLIMIT_AS, (self.max_memory_bytes, self.max_memory_bytes)
)
# No CPU limit on REPL, most Lean proofs take up to one core.
# The adjustment variables are the maximum number of REPLs and the timeout.
# See https://github.com/leanprover-community/repl/issues/91
os.setsid()
self.proc = await asyncio.create_subprocess_exec(
"lake",
"env",
settings.repl_path,
cwd=settings.project_dir,
env=os.environ,
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
preexec_fn=_preexec,
)
self._ps_proc = psutil.Process(self.proc.pid)
now = self._loop.time()
self._last_check = now
self._last_cpu_time = self._sum_cpu_times(self._ps_proc)
self._cpu_max = 0.0
self._mem_max = 0
self._cpu_task = self._loop.create_task(self._cpu_monitor())
self._mem_task = self._loop.create_task(self._mem_monitor())
logger.info(f"\\[{self.uuid.hex[:8]}] Started")
@staticmethod
def _sum_cpu_times(proc: psutil.Process) -> float:
total = proc.cpu_times().user + proc.cpu_times().system
for c in proc.children(recursive=True):
t = c.cpu_times()
total += t.user + t.system
return float(total)
async def _cpu_monitor(self) -> None:
while self.is_running and self._ps_proc and self._loop:
await asyncio.sleep(1)
now = self._loop.time()
cur_cpu = self._sum_cpu_times(self._ps_proc)
delta_cpu = cur_cpu - self._last_cpu_time
delta_t = now - self._last_check
usage_pct = (delta_cpu / delta_t) * 100
self._cpu_max = max(self._cpu_max, usage_pct)
self._last_cpu_time = cur_cpu
self._last_check = now
async def _mem_monitor(self) -> None:
while self.is_running and self._ps_proc:
await asyncio.sleep(1)
total = self._ps_proc.memory_info().rss
for child in self._ps_proc.children(recursive=True):
total += child.memory_info().rss
self._mem_max = max(self._mem_max, total)
@property
def is_running(self) -> bool:
if not self.proc:
return False
return self.proc.returncode is None
async def send_timeout(
self,
snippet: Snippet,
timeout: float,
is_header: bool = False,
infotree: Infotree | None = None,
) -> ReplResponse:
cmd_response = None
elapsed_time = (
0.0 # TODO: check what's the best time to check elapsed time, time lib?
)
diagnostics = Diagnostics(repl_uuid=str(self.uuid))
try:
cmd_response, elapsed_time, diagnostics = await asyncio.wait_for(
self.send(snippet, is_header=is_header, infotree=infotree),
timeout=timeout,
)
except TimeoutError as e:
logger.error(
"\\[{}] Lean REPL command timed out in {} seconds",
self.uuid.hex[:8],
timeout,
)
raise e
except LeanError as e:
logger.exception("Lean REPL error: %s", e)
raise e
except ReplError as e:
logger.exception("REPL error: %s", e)
raise e
return ReplResponse(
id=snippet.id,
response=cmd_response,
time=elapsed_time,
diagnostics=diagnostics if len(diagnostics) > 0 else None,
)
async def send(
self,
snippet: Snippet,
is_header: bool = False,
infotree: Infotree | None = None,
) -> tuple[CommandResponse | Error, float, Diagnostics]:
await log_snippet(self.uuid, snippet.id, snippet.code)
self._cpu_max = 0.0
self._mem_max = 0
if not self.proc or self.proc.returncode is not None:
logger.error("REPL process not started or shut down")
raise ReplError("REPL process not started or shut down")
loop = self._loop or asyncio.get_running_loop()
if self.proc.stdin is None:
raise ReplError("stdin pipe not initialized")
if self.proc.stdout is None:
raise ReplError("stdout pipe not initialized")
input: Command = {"cmd": snippet.code}
if self.use_count != 0 and not is_header: # remove is_header
input["env"] = 0
input["gc"] = True
if infotree:
input["infotree"] = infotree
payload = (json.dumps(input, ensure_ascii=False) + "\n\n").encode("utf-8")
start = loop.time()
logger.debug("Sending payload to REPL")
try:
self.proc.stdin.write(payload)
await self.proc.stdin.drain()
except BrokenPipeError:
logger.error("Broken pipe while writing to REPL stdin")
raise LeanError("Lean process broken pipe")
except Exception as e:
logger.error("Failed to write to REPL stdin: %s", e)
raise LeanError("Failed to write to REPL stdin")
logger.debug("Reading response from REPL stdout")
raw = await self._read_response()
elapsed = loop.time() - start
logger.debug("Raw response from REPL: %r", raw)
try:
resp: CommandResponse | Error = json.loads(raw)
except json.JSONDecodeError:
logger.error("JSON decode error: %r", raw)
raise ReplError("JSON decode error")
self.error_file.seek(0)
err = self.error_file.read().strip()
self.error_file.seek(0)
self.error_file.truncate(0)
if err:
logger.error("Stderr: %s", err)
raise LeanError(err)
elapsed_time = round(elapsed, 6)
diagnostics: Diagnostics = {
"repl_uuid": str(self.uuid),
"cpu_max": self._cpu_max,
"memory_max": self._mem_max,
}
self.cpu_per_exec[self.use_count] = self._cpu_max
self.mem_per_exec[self.use_count] = self._mem_max
self.use_count += 1
return resp, elapsed_time, diagnostics
async def _read_response(self) -> bytes:
if not self.proc or self.proc.stdout is None:
logger.error("REPL process not started or stdout pipe not initialized")
raise ReplError("REPL process not started or stdout pipe not initialized")
lines: list[bytes] = []
try:
while True:
chunk = await self.proc.stdout.readline()
# EOF or blank line as terminator
if not chunk or not chunk.strip():
break
lines.append(chunk)
except Exception as e:
logger.error("Failed to read from REPL stdout: %s", e)
raise LeanError("Failed to read from REPL stdout")
return b"".join(lines)
async def close(self) -> None:
if self.proc:
self.last_check_at = datetime.now()
assert self.proc.stdin is not None, "stdin pipe not initialized"
self.proc.stdin.close()
os.killpg(os.getpgid(self.proc.pid), signal.SIGKILL)
await self.proc.wait()
if self._cpu_task:
self._cpu_task.cancel()
if self._mem_task:
self._mem_task.cancel()
if db.connected:
await prisma.repl.update(
where={"uuid": str(self.uuid)},
data={"status": ReplStatus.STOPPED}, # type: ignore
)
async def close_verbose(repl: Repl) -> None:
uuid = repl.uuid
logger.info(f"Closing REPL {uuid.hex[:8]}")
await repl.close()
del repl
logger.info(f"Closed REPL {uuid.hex[:8]}")
|