Skip to content

Commit

Permalink
Fix bluebox shutdown (#18725)
Browse files Browse the repository at this point in the history
### Purpose:

Use a temporary file as a flag for when bluebox threads should abort
out. This allows for the timelord to shutdown cleanly.

### Current Behavior:

On exit, timelord hangs waiting for bluebox threads and daemon must kill
it. On windows this kills the owning console instead.

### New Behavior:

Bluebox threads abort out and timelord shuts down cleanly
  • Loading branch information
wjblanke authored Oct 17, 2024
2 parents 9c8557a + 658d356 commit 23b1cb5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 32 deletions.
20 changes: 18 additions & 2 deletions chia/timelord/timelord.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import logging
import os
import random
import tempfile
import time
import traceback
from collections.abc import AsyncIterator
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from typing import TYPE_CHECKING, Any, ClassVar, Optional, cast
from typing import IO, TYPE_CHECKING, Any, ClassVar, Optional, cast

from chiavdf import create_discriminant, prove

Expand Down Expand Up @@ -53,7 +54,7 @@ class BlueboxProcessData(Streamable):
iters: uint64


def prove_bluebox_slow(payload: bytes) -> bytes:
def prove_bluebox_slow(payload: bytes, executor_shutdown_tempfile_name: str) -> bytes:
bluebox_process_data = BlueboxProcessData.from_bytes(payload)
initial_el = b"\x08" + (b"\x00" * 99)
return cast(
Expand All @@ -63,10 +64,15 @@ def prove_bluebox_slow(payload: bytes) -> bytes:
initial_el,
bluebox_process_data.size_bits,
bluebox_process_data.iters,
executor_shutdown_tempfile_name,
),
)


def _create_shutdown_file() -> IO[bytes]:
return tempfile.NamedTemporaryFile(prefix="chia_timelord_executor_shutdown_trigger")


class Timelord:
if TYPE_CHECKING:
from chia.rpc.rpc_server import RpcServiceProtocol
Expand Down Expand Up @@ -138,6 +144,7 @@ def __init__(self, root_path: Path, config: dict[str, Any], constants: Consensus
self.pending_bluebox_info: list[tuple[float, timelord_protocol.RequestCompactProofOfTime]] = []
self.last_active_time = time.time()
self.max_allowed_inactivity_time = 60
self._executor_shutdown_tempfile: Optional[IO[bytes]] = None
self.bluebox_pool: Optional[ThreadPoolExecutor] = None

@contextlib.asynccontextmanager
Expand All @@ -156,6 +163,7 @@ async def manage(self) -> AsyncIterator[None]:
if os.name == "nt" or slow_bluebox:
# `vdf_client` doesn't build on windows, use `prove()` from chiavdf.
workers = self.config.get("slow_bluebox_process_count", 1)
self._executor_shutdown_tempfile = _create_shutdown_file()
self.bluebox_pool = ThreadPoolExecutor(
max_workers=workers,
)
Expand All @@ -169,6 +177,8 @@ async def manage(self) -> AsyncIterator[None]:
yield
finally:
self._shut_down = True
if self._executor_shutdown_tempfile is not None:
self._executor_shutdown_tempfile.close()
for task in self.process_communication_tasks:
task.cancel()
if self.main_loop is not None:
Expand Down Expand Up @@ -1168,13 +1178,19 @@ async def _manage_discriminant_queue_sanitizer_slow(self, pool: ThreadPoolExecut
pool,
prove_bluebox_slow,
bytes(bluebox_process_data),
"" if self._executor_shutdown_tempfile is None else self._executor_shutdown_tempfile.name,
)
t2 = time.time()
delta = t2 - t1
if delta > 0:
ips = picked_info.new_proof_of_time.number_of_iterations / delta
else:
ips = 0

if len(proof) == 0:
log.info(f"Empty VDF proof returned: {picked_info.height}. Time: {delta}s. IPS: {ips}.")
return

log.info(f"Finished compact proof: {picked_info.height}. Time: {delta}s. IPS: {ips}.")
output = proof[:100]
proof_part = proof[100:200]
Expand Down
1 change: 1 addition & 0 deletions chia/util/vdf_prover.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def get_vdf_info_and_proof(
vdf_input.data,
constants.DISCRIMINANT_SIZE_BITS,
number_iters,
"",
)

output = ClassgroupElement.create(result[:form_size])
Expand Down
58 changes: 29 additions & 29 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ boto3 = "1.34.143" # AWS S3 for Data Layer S3 plugin
chiabip158 = "1.5.1" # bip158-style wallet filters
chiapos = "2.0.4" # proof of space
chia_rs = "0.15.0"
chiavdf = "1.1.6" # timelord and vdf verification
chiavdf = "1.1.8" # timelord and vdf verification
click = "8.1.7" # For the CLI
clvm = "0.9.10"
clvm_tools = "0.4.9" # Currying Program.to other conveniences
Expand Down

0 comments on commit 23b1cb5

Please sign in to comment.