# moonrockz/moon-lander-plugins/process

A service plugin in its own worker process. The plugin can be of any kind: MoonBit code, a
wasm module or a linked native library. A crash of the plugin ends only the worker, and the
host can end a call that does not return. The contract and the failures are the same as for
each other plugin kind: see [the module guide](../README.mbt.md). This package builds for
native hosts only.

```text
host process                              worker process
session -> channel -> stdin/stdout frames -> serve loop -> plugin
                      stderr             -> host logger
```

## Before you use a worker

- A worker is not a sandbox. It has the file and network access of the user. To limit
  what plugin code can do, use a [wasm plugin](../wasm/README.mbt.md).
- The host starts one process for each plugin service, and each start of the service
  starts a new process.
- The application gives the worker program and its arguments. The library does not guess
  the path of the running program.

## The worker program

A worker program serves one plugin with `@plugins_worker.serve` from
[`moonrockz/moon-lander-plugins/worker`](../worker/README.mbt.md). It can be a separate
program, or a worker mode of the application's own program. This program has a worker mode:
with the argument `worker` it serves its linked Zig heartbeat plugin.

```moonbit notest
  let args = @env.args()
  // The worker mode: serve the Zig heartbeat on standard input and output.
  if args.get(1) is Some("worker") {
    @plugins_worker.serve(@plugins_abi.NativePlugin::load(zig_heartbeat()))
    return
  }
```

The host side gives the path of the same program and the argument:

```moonbit notest
///|
let worker = @plugins_process.WorkerPlugin::new(
  @lander_core.AbsolutePath::parse(path),
  args=["worker"],
)
```

`WorkerPlugin` is a `@plugins.Plugin`, so `plugin_service` of
[`moonrockz/moon-lander-plugins/hosting`](../hosting/README.mbt.md) takes it as it takes
each other plugin. The program above is the probe that `mise run check:plugins` runs: it
runs the conformance suite with the Zig heartbeat in a worker of itself.

## Standard output and standard error

Standard output of a worker belongs to the frames. Each frame has a fixed mark, a version
and a length. Plain text on standard output breaks the contract: the plugin fails with an
error that names the broken message, and the host does not read the text as a reply.

Standard error is free for the plugin and its libraries. The host writes each line to the
log of the service at the warning level. A line of more than 8 KiB is cut to its first
8 KiB and gets the mark ` [cut]`.

## Limits

| Limit | Default | What happens at the limit |
| --- | --- | --- |
| The call limit of `plugin_service` | 5 seconds | The host ends the worker by force, and the plugin fails with `LimitReached`. `RunningPlugin::enforcement` is `Enforced` |
| `frame_limit` of `WorkerPlugin::new` | 16 MiB (`FrameLimit::default()`) | The plugin fails. The host checks the length before it reads the body, so it does not allocate a body over the limit |

A worker that ends before its stop fails the plugin. The error names the operation in
progress and how the worker ended: an exit code, or on Linux and macOS a signal. The stop
phase of the host continues.

## The environment

A worker gets only the variables of the `WorkerEnvironment` that the application gives,
and on Windows `SystemRoot`, which a Windows process needs to start. It does not get the
other variables of the host. A variable name must not be empty or have `=` or a NUL
character, and a value must not have a NUL character: `WorkerEnvironment::set` raises
`WorkerError` otherwise.

## The end of a worker

After the `stop` call, the release closes standard input of the worker and waits for its
end within the call limit. After that, or when the shutdown timeout of the host ends, the
host ends the worker by force. The connection does not end before the worker process ends,
so no worker stays after the host run.

A worker ignores the console interrupt and the termination signal, so Ctrl-C in a terminal
or a SIGTERM to the whole service does not end the plugin before its `stop` call. When the
host process ends without a stop, for example after SIGKILL, the standard input of the
worker closes, and the worker ends after its current call. A worker in a call that never
returns cannot see that, and it stays until something ends it.

## Processes that a worker starts

The host owns the worker process, but not the processes that the worker starts. When the
host ends a worker by force, such a process can stay. Core async gives no process groups or
job objects yet (`moonbitlang/async#648`), and a later change can use them.
