Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log KoreServer output #4501

Merged
merged 3 commits into from
Jul 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions pyk/src/pyk/kore/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from enum import Enum, auto
from pathlib import Path
from signal import SIGINT
from subprocess import Popen
from subprocess import DEVNULL, PIPE, Popen
from threading import Thread
from time import sleep
from typing import ClassVar # noqa: TC003
from typing import TYPE_CHECKING, ContextManager, NamedTuple, TypedDict, final
Expand All @@ -26,7 +27,7 @@

if TYPE_CHECKING:
from collections.abc import Iterable, Mapping
from typing import Any, Final, TextIO, TypeVar
from typing import IO, Any, Final, TypeVar

from typing_extensions import Required

Expand Down Expand Up @@ -115,7 +116,7 @@ class SingleSocketTransport(Transport):
_host: str
_port: int
_sock: socket.socket
_file: TextIO
_file: IO[str]

def __init__(
self,
Expand Down Expand Up @@ -1129,6 +1130,8 @@ class KoreServerInfo(NamedTuple):

class KoreServer(ContextManager['KoreServer']):
_proc: Popen
_stdout_reader: Thread
_stderr_reader: Thread
_info: KoreServerInfo

_kompiled_dir: Path
Expand Down Expand Up @@ -1208,21 +1211,41 @@ def start(self) -> None:
new_env['GHCRTS'] = f'-N{self._haskell_threads}'

_LOGGER.info(f'Starting KoreServer: {" ".join(cli_args)}')
self._proc = Popen(cli_args, env=new_env)
self._proc, self._stdout_reader, self._stderr_reader = self._create_proc(cli_args, new_env)
pid = self._proc.pid
host, port = self._get_host_and_port(pid)
if self._port:
assert port == self._port
self._info = KoreServerInfo(pid=pid, host=host, port=port)
_LOGGER.info(f'KoreServer started: {self.host}:{self.port}, pid={self.pid}')

@staticmethod
def _create_proc(args: list[str], env: dict[str, str]) -> tuple[Popen, Thread, Thread]:
popen = Popen(args, env=env, stdin=DEVNULL, stdout=PIPE, stderr=PIPE, text=True)

def reader(fh: IO[str], prefix: str) -> None:
for line in fh:
_LOGGER.info(f'[PID={popen.pid}][{prefix}] {line.rstrip()}')

stdout_reader = Thread(target=reader, args=(popen.stdout, 'stdo'))
stdout_reader.daemon = True
stdout_reader.start()

stderr_reader = Thread(target=reader, args=(popen.stderr, 'stde'))
stderr_reader.daemon = True
stderr_reader.start()

return popen, stdout_reader, stderr_reader

def close(self) -> None:
_LOGGER.info(f'Stopping KoreServer: {self.host}:{self.port}, pid={self.pid}')
if '--solver-transcript' in self._command:
self._proc.send_signal(SIGINT)
else:
self._proc.terminate()
self._proc.wait()
self._stdout_reader.join()
self._stderr_reader.join()
_LOGGER.info(f'KoreServer stopped: {self.host}:{self.port}, pid={self.pid}')

def _validate(self) -> None:
Expand Down
Loading