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

Refactor StreamingWorkunitHandler to be a class-based context manager #11685

Merged
merged 3 commits into from Mar 12, 2021

Conversation

Eric-Arellano
Copy link
Contributor

@Eric-Arellano Eric-Arellano commented Mar 12, 2021

Before, it was possible to call StreamingWorkunitHandler.session() multiple times, meaning we'd be starting and stopping the same threads during the same Pants run. While this wasn't done in practice beyond a test, it complicates future changes and is an inaccurate representation of what we expect.

Even though it is still possible to do this with a class-based context manager, the class better expresses the intention than a reusable method. Further, this change will result in a RuntimeException if the user does use the context manager twice.

[ci skip-rust]
[ci skip-build-wheels]

# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
Comment on lines 232 to 233
if not goals:
return PANTS_SUCCEEDED_EXIT_CODE
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No need to have this in the SWH block, as there should be no workunits in this case.

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

True, but some SWHs consume the RunTracker to report on the overall status of a run as well: should probably leave it in there.

Comment on lines -336 to +335
assert len(flattened) == 10
assert len(flattened) == 11
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is now 11 because we don't have the memoization from the original scheduler. We created a new one.

# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
Comment on lines 232 to 233
if not goals:
return PANTS_SUCCEEDED_EXIT_CODE
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

True, but some SWHs consume the RunTracker to report on the overall status of a run as well: should probably leave it in there.

@@ -180,54 +182,42 @@ def __init__(
options_bootstrapper: OptionsBootstrapper,
specs: Specs,
report_interval_seconds: float,
pantsd: bool,
max_workunit_verbosity: LogLevel = LogLevel.TRACE,
) -> None:
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

So, it would still be possible to enter/exit this contextmanager multiple times, right? Using it in a with statement doesn't consume it.

One way to make that a non-issue though, maybe, would be to move the construction of the thread_runner into __enter__? That way you'd create a new one each time you called __enter__, and could fail if one already existed, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, that's a good point that this doesn't actually prevent the problem from a language perspective.

However, my change does result in this error if you use the same handler twice:

15:00:25.87 [ERROR] threads can only be started once
Traceback (most recent call last):
  File "/Users/eric/code/pants/src/python/pants/bin/daemon_pants_runner.py", line 134, in single_daemonized_run
    return runner.run(start_time)
  File "/Users/eric/code/pants/src/python/pants/bin/local_pants_runner.py", line 255, in run
    with streaming_reporter:
  File "/Users/eric/code/pants/src/python/pants/engine/streaming_workunit_handler.py", line 213, in __enter__
    self.thread_runner.start()
  File "/Users/eric/.pyenv/versions/3.9.1/lib/python3.9/threading.py", line 869, in start
    raise RuntimeError("threads can only be started once")
RuntimeError: threads can only be started once

…ager

# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
@Eric-Arellano Eric-Arellano merged commit 0f27e41 into pantsbuild:master Mar 12, 2021
@Eric-Arellano Eric-Arellano deleted the shw-context-manager branch March 12, 2021 22:54
@stuhood stuhood mentioned this pull request Mar 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants