ssh_process_lifetime_manager_from_forked_process
drunc.processes.ssh_process_lifetime_manager_from_forked_process
Provides a subprocess-isolated wrapper around SSHProcessLifetimeManagerShell.
All method calls are forwarded to an SSHProcessLifetimeManagerShell instance running in a dedicated child process via multiprocessing queues, keeping SSH activity and threading state fully isolated from the parent process.
Classes
SSHProcessLifetimeManagerShellOnForkedProcess(disable_host_key_check=False, disable_localhost_host_key_check=False, logger=None, on_process_exit=None)
Bases: ProcessLifetimeManager
A ProcessLifetimeManager that delegates process management to an SSHProcessLifetimeManagerShell instance running in a dedicated child process.
All public methods forward their call to the child process via multiprocessing queues and block until a response is received. This isolates SSH connections, threads, and file-descriptor state from the parent process, which is useful when the parent uses fork-unsafe libraries or needs clean process boundaries.
_call() is safe to invoke from multiple threads simultaneously. Pending requests are matched to their responses by unique request IDs, and a dedicated response-dispatcher thread routes each response to the correct waiting caller without any cross-caller interference.
Initialise the forked-process manager and start the child process immediately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
disable_host_key_check
|
bool
|
Disable SSH host key verification for all hosts. |
False
|
disable_localhost_host_key_check
|
bool
|
Disable SSH host key verification for localhost connections only. |
False
|
logger
|
Optional[Logger]
|
Logger instance used by the parent process. The child creates its own independent logger. |
None
|
on_process_exit
|
Optional[Callable[[str, Optional[int], Optional[Exception]], None]]
|
Optional callback invoked in the parent process when a managed process exits. Signature: (uuid, exit_code, exception). The exception is reconstructed as a RuntimeError from the serialised message forwarded by the child process. |
None
|
Source code in drunc/processes/ssh_process_lifetime_manager_from_forked_process.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | |
Functions
crash_process(uuid)
Simulate a process crash by sending SIGKILL without performing any cleanup.
Delegates to the underlying SSHProcessLifetimeManagerShell running in the forked worker process. Sends SIGKILL to the remote process without cleaning up any associated resources, simulating an unexpected crash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuid
|
str
|
Process UUID to crash |
required |
Source code in drunc/processes/ssh_process_lifetime_manager_from_forked_process.py
get_active_process_keys()
Get list of active process UUIDs from the child process.
Returns:
| Type | Description |
|---|---|
List[str]
|
List of active process UUID strings. |
Source code in drunc/processes/ssh_process_lifetime_manager_from_forked_process.py
get_process_stderr(uuid)
Get accumulated stderr from a managed process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuid
|
str
|
Process UUID. |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Stderr content as a string, or None if not available. |
Source code in drunc/processes/ssh_process_lifetime_manager_from_forked_process.py
get_process_stdout(uuid)
Get accumulated stdout from a managed process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuid
|
str
|
Process UUID. |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Stdout content as a string, or None if not available. |
Source code in drunc/processes/ssh_process_lifetime_manager_from_forked_process.py
get_remote_pid(uuid)
Retrieve the remote PID for a managed process via the child process.
Delegates to the underlying SSHProcessLifetimeManagerShell running in the forked worker process. The returned RemotePidResult dataclass is picklable and transmitted across the process boundary without special handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuid
|
str
|
Process UUID to query. |
required |
Returns:
| Type | Description |
|---|---|
RemotePidResult
|
RemotePidResult with |
RemotePidResult
|
why the PID is unavailable (e.g. metadata not yet written). |
Source code in drunc/processes/ssh_process_lifetime_manager_from_forked_process.py
is_process_alive(uuid)
Check if a managed process is alive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuid
|
str
|
Process UUID to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the process is alive, False otherwise. |
Source code in drunc/processes/ssh_process_lifetime_manager_from_forked_process.py
kill_all_processes(process_timeouts=None)
Kill all managed processes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
process_timeouts
|
Optional[Dict[str, float]]
|
Optional per-UUID timeout overrides in seconds. |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Optional[int]]
|
Dictionary mapping all process UUIDs to their exit codes. |
Source code in drunc/processes/ssh_process_lifetime_manager_from_forked_process.py
kill_process(uuid, timeout=ProcessLifetimeManager.DEFAULT_TIMEOUT_FOR_KILLING_PROCESS)
Kill a remote process and clean up its resources.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuid
|
str
|
Process UUID to terminate. |
required |
timeout
|
float
|
Graceful termination timeout in seconds. |
DEFAULT_TIMEOUT_FOR_KILLING_PROCESS
|
Returns:
| Type | Description |
|---|---|
Optional[int]
|
Exit code of the terminated process, or None if undetermined. |
Source code in drunc/processes/ssh_process_lifetime_manager_from_forked_process.py
kill_processes(uuids, process_timeouts=None)
Kill multiple processes in role-based shutdown order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuids
|
List[str]
|
List of process UUIDs to terminate. |
required |
process_timeouts
|
Optional[Dict[str, float]]
|
Optional per-UUID timeout overrides in seconds. |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Optional[int]]
|
Dictionary mapping process UUIDs to their exit codes. |
Source code in drunc/processes/ssh_process_lifetime_manager_from_forked_process.py
kill_processes_by_role(role, candidate_uuids, process_timeouts=None)
Kill all processes with the specified role from the candidate UUID list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
role
|
str
|
Process role to match (e.g. "controller", "application"). |
required |
candidate_uuids
|
List[str]
|
List of process UUIDs to filter by role. |
required |
process_timeouts
|
Optional[Dict[str, float]]
|
Optional per-UUID timeout overrides in seconds. |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Optional[int]]
|
Dictionary mapping terminated process UUIDs to their exit codes. |
Source code in drunc/processes/ssh_process_lifetime_manager_from_forked_process.py
pop_early_exit_code(uuid)
Retrieve and remove the exit code of a process that exited unexpectedly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuid
|
str
|
Process UUID. |
required |
Returns:
| Type | Description |
|---|---|
Optional[int]
|
Exit code if the process terminated early without being explicitly killed, |
Optional[int]
|
None if still running or not found. |
Source code in drunc/processes/ssh_process_lifetime_manager_from_forked_process.py
read_log_file(hostname, user, log_file, num_lines=100)
Read the last N lines of a remote log file via the child process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hostname
|
str
|
Target hostname. |
required |
user
|
str
|
SSH username. |
required |
log_file
|
str
|
Remote log file path. |
required |
num_lines
|
int
|
Number of lines to retrieve from the end of the file. |
100
|
Returns:
| Type | Description |
|---|---|
List[str]
|
List of log lines. |
Source code in drunc/processes/ssh_process_lifetime_manager_from_forked_process.py
shutdown()
Gracefully shut down the child process and all background dispatcher threads.
Sends shutdown sentinels to each queue so the child's event loop and both dispatcher threads exit cleanly. Joins the child process with a short timeout and forcibly terminates it if it does not exit in time.
Source code in drunc/processes/ssh_process_lifetime_manager_from_forked_process.py
start_process(uuid, boot_request)
Start a remote process in the child process using the boot request configuration.
The BootRequest protobuf message is serialised to bytes before being sent across the process boundary and deserialised inside the child, avoiding any reliance on protobuf pickling support.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuid
|
str
|
Unique identifier for this process. |
required |
boot_request
|
BootRequest
|
BootRequest containing process configuration. |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the child process raises during process startup. |
Source code in drunc/processes/ssh_process_lifetime_manager_from_forked_process.py
validate_host_connection(host, auth_method, user)
Validate an SSH connection to the specified host via the child process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
Target hostname. |
required |
auth_method
|
str
|
Authentication method (passed through to inner manager). |
required |
user
|
str
|
SSH username. |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the SSH connection validation fails. |