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

Use an isolated clone of the Session in workunit handlers to shield against cancellation #11721

Merged
merged 2 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/python/pants/engine/internals/native_engine.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def session_get_observation_histograms(scheduler: PyScheduler, session: PySessio
def session_record_test_observation(
scheduler: PyScheduler, session: PySession, value: int
) -> None: ...
def session_isolated_shallow_clone(session: PySession) -> PySession: ...
def all_counter_names() -> list[str]: ...
def graph_len(scheduler: PyScheduler) -> int: ...
def graph_visualize(scheduler: PyScheduler, session: PySession, path: str) -> None: ...
Expand Down
12 changes: 9 additions & 3 deletions src/python/pants/engine/internals/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def __init__(
"""
self.include_trace_on_error = include_trace_on_error
self._visualize_to_dir = visualize_to_dir
self._visualize_run_count = 0
# Validate and register all provided and intrinsic tasks.
rule_index = RuleIndex.create(rules)
tasks = register_rules(rule_index, union_membership)
Expand Down Expand Up @@ -311,7 +312,6 @@ class SchedulerSession:
def __init__(self, scheduler: Scheduler, session: PySession) -> None:
self._scheduler = scheduler
self._py_session = session
self._run_count = 0

@property
def scheduler(self) -> Scheduler:
Expand All @@ -325,6 +325,11 @@ def py_scheduler(self) -> PyScheduler:
def py_session(self) -> PySession:
return self._py_session

def isolated_shallow_clone(self) -> SchedulerSession:
return SchedulerSession(
self._scheduler, native_engine.session_isolated_shallow_clone(self._py_session)
)

def poll_workunits(self, max_log_verbosity: LogLevel) -> PolledWorkunits:
result = native_engine.session_poll_workunits(
self.py_scheduler, self.py_session, max_log_verbosity.level
Expand Down Expand Up @@ -401,8 +406,9 @@ def metrics(self) -> dict[str, int]:

def _maybe_visualize(self) -> None:
if self._scheduler.visualize_to_dir is not None:
name = f"graph.{self._run_count:03d}.dot"
self._run_count += 1
# TODO: This increment-and-get is racey.
name = f"graph.{self._scheduler._visualize_run_count:03d}.dot"
self._scheduler._visualize_run_count += 1
self.visualize_graph_to_file(os.path.join(self._scheduler.visualize_to_dir, name))

def teardown_dynamic_ui(self) -> None:
Expand Down
1 change: 1 addition & 0 deletions src/python/pants/engine/streaming_workunit_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def __init__(
pantsd: bool,
max_workunit_verbosity: LogLevel = LogLevel.TRACE,
) -> None:
scheduler = scheduler.isolated_shallow_clone()
self.callbacks = callbacks
self.context = StreamingWorkunitContext(
_scheduler=scheduler,
Expand Down
12 changes: 12 additions & 0 deletions src/rust/engine/src/externs/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,12 @@ py_module_initializer!(native_engine, |py, m| {
session_record_test_observation(a: PyScheduler, b: PySession, c: u64)
),
)?;
m.add(
py,
"session_isolated_shallow_clone",
py_fn!(py, session_isolated_shallow_clone(a: PySession)),
)?;

m.add(py, "all_counter_names", py_fn!(py, all_counter_names()))?;

m.add(
Expand Down Expand Up @@ -1389,6 +1395,12 @@ fn session_record_test_observation(
})
}

fn session_isolated_shallow_clone(py: Python, session_ptr: PySession) -> CPyResult<PySession> {
with_session(py, session_ptr, |session| {
PySession::create_instance(py, session.isolated_shallow_clone())
})
}
Comment on lines +1398 to +1402
Copy link
Contributor

Choose a reason for hiding this comment

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

Generally, I am curious if we'll be able to move free functions like this onto methods defined on PySession? Totally okay to not do in this PR - it's a followup project I want to explore to see if it further simplifies our FFI.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we probably can. The with_$x context managers add a bunch of noise, and are much less central than calling allow_threads, which we should try to do in nearly all cases.

Copy link
Member Author

Choose a reason for hiding this comment

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

Opened #11722


fn validate_reachability(py: Python, scheduler_ptr: PyScheduler) -> PyUnitResult {
with_scheduler(py, scheduler_ptr, |scheduler| {
scheduler
Expand Down
Loading