Skip to content

Commit 68c4ebd

Browse files
committed
Don't create runtime_context.kill_switch by default
So that the runtime_context object can still be pickled. Other cleanups
1 parent 27e224e commit 68c4ebd

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

cwltool/context.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def __init__(self, kwargs: Optional[Dict[str, Any]] = None) -> None:
183183
self.select_resources: Optional[select_resources_callable] = None
184184
self.eval_timeout: float = 60
185185
self.postScatterEval: Optional[Callable[[CWLObjectType], Optional[CWLObjectType]]] = None
186-
self.on_error: Union[Literal["stop"], Literal["continue"]] = "stop"
186+
self.on_error: Union[Literal["stop"], Literal["continue"], Literal["kill"]] = "stop"
187187
self.strict_memory_limit: bool = False
188188
self.strict_cpu_limit: bool = False
189189
self.cidfile_dir: Optional[str] = None
@@ -200,7 +200,7 @@ def __init__(self, kwargs: Optional[Dict[str, Any]] = None) -> None:
200200
self.default_stderr: Optional[Union[IO[bytes], TextIO]] = None
201201
self.validate_only: bool = False
202202
self.validate_stdout: Optional[Union[IO[bytes], TextIO, IO[str]]] = None
203-
self.kill_switch = threading.Event()
203+
self.kill_switch: Optional[threading.Event] = None
204204
super().__init__(kwargs)
205205
if self.tmp_outdir_prefix == "":
206206
self.tmp_outdir_prefix = self.tmpdir_prefix

cwltool/errors.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ class GraphTargetMissingException(WorkflowException):
1717
class WorkflowKillSwitch(Exception):
1818
"""When processStatus != "success" and on-error=kill, raise this exception."""
1919

20-
def __init__(self, job_id, rcode):
20+
def __init__(self, job_id: str, rcode: int) -> None:
21+
"""Record the job identifier and the error code."""
2122
self.job_id = job_id
2223
self.rcode = rcode
2324

24-
def __str__(self):
25-
return f'[job {self.job_id}] activated kill switch with return code {self.rcode}'
25+
def __str__(self) -> str:
26+
"""Represent this exception as a string."""
27+
return f"[job {self.job_id}] activated kill switch with return code {self.rcode}"

cwltool/executors.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,8 @@ def run_jobs(
477477
self.wait_for_next_completion(runtime_context)
478478
self.run_job(None, runtime_context)
479479
finally:
480-
runtime_context.workflow_eval_lock.release()
480+
if (lock := runtime_context.workflow_eval_lock) is not None:
481+
lock.release()
481482
self.taskqueue.drain()
482483
self.taskqueue.join()
483484

cwltool/job.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@ def _execute(
231231
runtime: List[str],
232232
env: MutableMapping[str, str],
233233
runtimeContext: RuntimeContext,
234-
monitor_function: Optional[Callable[["subprocess.Popen[str]"], None]] = None,
234+
monitor_function: Optional[
235+
Callable[["subprocess.Popen[str]", threading.Event], None]
236+
] = None,
235237
) -> None:
236238
"""Execute the tool, either directly or via script.
237239
@@ -333,6 +335,10 @@ def stderr_stdout_log_path(
333335
builder: Optional[Builder] = getattr(self, "builder", None)
334336
if builder is not None:
335337
job_script_contents = builder.build_job_script(commands)
338+
if runtimeContext.kill_switch is None:
339+
runtimeContext.kill_switch = kill_switch = threading.Event()
340+
else:
341+
kill_switch = runtimeContext.kill_switch
336342
rcode = _job_popen(
337343
commands,
338344
stdin_path=stdin_path,
@@ -341,7 +347,7 @@ def stderr_stdout_log_path(
341347
env=env,
342348
cwd=self.outdir,
343349
make_job_dir=lambda: runtimeContext.create_outdir(),
344-
kill_switch=runtimeContext.kill_switch,
350+
kill_switch=kill_switch,
345351
job_script_contents=job_script_contents,
346352
timelimit=self.timelimit,
347353
name=self.name,
@@ -547,7 +553,8 @@ def monitor_kill_switch() -> None:
547553
nonlocal ks_tm
548554
if kill_switch.is_set():
549555
_logger.error("[job %s] terminating by kill switch", self.name)
550-
if sproc.stdin: sproc.stdin.close()
556+
if sproc.stdin:
557+
sproc.stdin.close()
551558
sproc.terminate()
552559
else:
553560
ks_tm = Timer(interval=1, function=monitor_kill_switch)

cwltool/task_queue.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import threading
88
from typing import Callable, Optional
99

10-
from .loghandler import _logger
1110
from .errors import WorkflowKillSwitch
11+
from .loghandler import _logger
1212

1313

1414
class TaskQueue:

0 commit comments

Comments
 (0)