Skip to content

Conversation

@emlowe
Copy link
Contributor

@emlowe emlowe commented Dec 8, 2025

Add context feature to BlockTools and make create_block_tools_async a context manager

This allows for the automatic cleanup of the TemporaryDirectory that BlockTools may create during initialization. Python 3.14 has more checks for implicit cleanup of temp dirs and this required an ignore being added to pytest.ini otherwise tests were being marked as failed due to ResourceWarnings

This code only affects tests, and although the specific problem can be worked around in pytest.ini, this is a "more correct" solution as it explicitly calls the directory cleanup function.


Note

Convert BlockTools and create_block_tools(_async) into (async) context managers and update tests/benchmarks/tools to use them, improving resource cleanup.

  • Simulator / BlockTools:
    • Add context manager support to BlockTools (__enter__/__exit__) with explicit tempdir cleanup.
    • Change create_block_tools_async to an @asynccontextmanager yielding BlockTools.
    • Change create_block_tools to a @contextmanager yielding BlockTools.
  • Tests and Fixtures:
    • Update fixtures (bt, get_b_tools*, custom tools) to return AsyncIterator[BlockTools] and use async with create_block_tools_async(...).
    • Replace direct awaits with async with across blockchain, full node, farmer/harvester, solver, simulation, and setup helpers.
    • Ensure proper shutdown/cleanup (e.g., RPC clients) within context scopes.
  • Benchmarks and CLI Tools:
    • Switch benchmarks (benchmarks/blockchains.py) and tools/generate_chain.py to use context-managed BlockTools/keyrings.
    • Minor structural adjustments to preserve timing/logging within managed scopes.

Written by Cursor Bugbot for commit 5565471. This will update automatically on new commits. Configure here.

@emlowe emlowe added CI CI changes Changed Required label for PR that categorizes merge commit message as "Changed" for changelog labels Dec 8, 2025
@emlowe emlowe marked this pull request as ready for review December 8, 2025 22:16
@emlowe emlowe requested a review from a team as a code owner December 8, 2025 22:16
@emlowe emlowe requested review from Quexington and arvidn December 8, 2025 22:16
create_block_tools_async(
constants=consensus_constants, keychain=keychain2, config_overrides=config_overrides
)
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not obvious that it's safe to start these block tools after we try to find available local listen ports. They may try to open listen sockets themselves. I think it would be safer to move these up to create them in the original order

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, bugbot found these issues as well


@pytest.fixture(scope="session", name="bt")
async def block_tools_fixture(get_keychain, blockchain_constants, anyio_backend, testrun_uid: str) -> BlockTools:
async def block_tools_fixture(get_keychain, blockchain_constants, anyio_backend, testrun_uid: str):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally we would keep the type annotation for the return value

)

def __enter__(self) -> Self:
return self
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait, aren't you making this an async context manager? Then you need __aenter__() and __aexit__(), don't you?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The BlockTools class is always used with as the class itself doesn't need any async functionality to get an instance. create_block_tools_async is an asynccontextmanager and I think it's ok for it to use BlockTools with and not async with

@emlowe emlowe removed the request for review from Quexington December 8, 2025 23:57
Copy link
Contributor

@Quexington Quexington left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pending other reviewers' comments of course but I didn't find anything else to comment on.

@github-actions
Copy link
Contributor

github-actions bot commented Dec 9, 2025

File Coverage Missing Lines
chia/_tests/conftest.py 80.0% lines 957, 960
chia/_tests/util/setup_nodes.py 83.3% lines 389
chia/cmds/sim_funcs.py 0.0% lines 256, 263-264, 267
chia/simulator/block_tools.py 80.0% lines 2217-2220
Total Missing Coverage
75 lines 11 lines 85%

@emlowe emlowe mentioned this pull request Dec 9, 2025
21 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changed Required label for PR that categorizes merge commit message as "Changed" for changelog CI CI changes coverage-diff

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants