akash-akya

akash-akya

Exile is an alternative to beam ports for running external programs. It provides back-pressure using non-blocking io, and tries to fix all issues associated with ports.

Github: GitHub - akash-akya/exile: Alternative to ports for running external programs. It provides back-pressure, non-blocking io, and solves port related issues · GitHub

Rationale and issues associated with exiting approaches are mentioned here

This is another stab at solving the issues associated with running an external command. My other approach ExCmd uses named pipes for solving these issues. But, since :file functions are blocking operations in the beam, this can cause issues. As of now, there are no non-blocking file operations available in :file module. So the only way to do non-blocking io is using NIF/port-driver.

Exile.stream! is a stream interface for interacting with the external program. It works with both Enumerable and Collectable. Apart from being compostable, stream handles closing stdin and terminating the external process.

Exile.stream!(~w(ffmpeg -i pipe:0 -f mp3 pipe:1), input: File.stream!("music_video.mkv", [], 65536))
|> Stream.into(File.stream!("music.mp3"))
|> Stream.run()

Key differences when compared to other middleware based libraries and port

  • fixes issues associated with ports
    • no more zombie process
    • can selectively close stdin
    • its is demand-driven (back-pressure)
  • it uses enif_select for asynchronous io, so it utilizes resources efficiently
  • uses non-blocking read/write system calls. so it can never block the scheduler
  • it does not use any middleware
    • no additional os process. no performance/resource cost
    • no need to install any external command
  • can run many external programs in parallel without adversely affecting schedulers (when compared to ExCmd)
  • stream abstraction for interacting with the external program
  • should be portable across POSIX compliant operating systems (not tested)

Non-blocking io can be used for other interesting things. Such as reading named pipe (FIFO) files. Exile.stream!(~w(cat data.pipe)) does not block schedulers so you can open hundreds of FIFO files unlike default :file module.

Please check the project page for more detail.

Note: Exile is experimental and it is still work-in-progress

feedback is welcome :slight_smile:

Showing Posts 1 to 10

dimitarvp

dimitarvp

Very, very nice. Love it.

Just one note, might be misinformed: I am worried that with a C dependency the crate might not compile at all on Windows – have you tried it? Or are you targeting UNIX-es only?

That’s the reason I am only doing NIFs with Rust – got impressed in the past how quickly and easily it compiles stuff on Windows without complaints.

akash-akya

akash-akya OP

Very, very nice. Love it.

Thank you

C dependency the crate might not compile at all on Windows – have you tried it? Or are you targeting UNIX-es only?

It uses the POSIX API. so currently, it is for UNIX like systems only.

We can have different implementation for windows, but I don’t have a windows machine to test it properly.

I like to use Rust, but I think this case different. Most of the code associated with system calls, so using C is straight-forward and it can be used with any POSIX complaint os without relying on rust compiler presence.

dimitarvp

dimitarvp

I see, thank you. :023:

jayjun

jayjun

Great stuff, need more explorations in this area! Two questions,

  1. Forking in the VM briefly causes a memory explosion, that’s why erl_child_setup exists to spawn ports. Have you done a comparison?

  2. How do you prevent misbehaving programs from becoming orphans? Misbehaving as in those that don’t exit after standard input is closed.

akash-akya

akash-akya OP

Thank you :slight_smile:

Forking in the VM briefly causes a memory explosion , that’s why erl_child_setup exists to spawn ports. Have you done a comparison?

I thought of this approach before. But in most modern os fork() is copy-on-write. so memory explosion is not an issue (unless I’m missing something). But keeping separate processes for exec has other advantages which are mentioned here. This can be done, but I think the current approach is much simpler. And this process will be like a “shared” resource among all schedulers and I prefer to avoid that. also, we might have to monitor this os process and deal with all the concern that comes with it. If we really need it, we can add it in the future I think.

How do you prevent misbehaving programs from becoming orphans? Misbehaving as in those that don’t exit after standard input is closed.

The executed external program is tied to a beam process, and this beam process will be monitored by another a “watcher” beam process. Watcher makes sure to terminate the spawned program when the beam process dies for whatever reason. It does in the order: polite close stdin → SIGTERM → SIGKILL.

I would like to know if there are better approaches than this

jayjun

jayjun

The forker spawn driver was added fairly recently in ERTS 8.0 (search for erl_child_setup) because of 3-5x better performance, even 10x. This is way after Linux implemented copy-on-write.

So I suspect the memory explosion comment still refers to modern kernels. I found a good read.

fork() is evil; vfork() is goodness; afork() would be better; clone() is stupid

But even COW is very expensive because it requires modifying memory mappings, taking expensive page faults, and so on. Modern kernels tend to seed the child with a copy of the parent’s resident set, but if the parent has a large memory footprint (e.g., is a JVM), then the RSS will be huge.

Regardless, now that you’ve brought fork() back into the VM, I feel it’s a great opportunity to do a proper comparison. Judging from the above, the difference isn’t trivial.

The executed external program is tied to a beam process …

The scenario I imagined is when the Erlang node crashed or is forcefully killed. An external shim can detect when that happens then clean up by killing all child processes before exit.

mbklein

mbklein

This looks very cool, and I’m trying to convert some code that’s currently using ports to use Exile instead, but I’ve having some trouble. The code is in JavaScript, and I’m having trouble trying to get it to handle the input stream. I’ve managed to reduce it to a super-simple test case that just copies stdin to stdout:

Here’s the JS:

#!/usr/bin/env node

process.stdin.on("end", () => console.error("done"));
console.error("piping");
process.stdin.pipe(process.stdout);

And here’s the Elixir:

defmodule ExileTest do
  def hello do
    [Path.expand("priv/js/command.js")]
    |> Exile.stream!(input_stream: File.stream!("priv/input.txt", [], 65536), stderr_to_console: true)
    |> Enum.to_list
  end
end

If I run it from the command line…

$ cat priv/input.txt| priv/js/command.js
piping
Exile is an alternative to beam ports
for running external programs. It provides
back-pressure using non-blocking io, and
tries to fix all issues associated with ports.
done

But if I try to call it from iex…

Erlang/OTP 22 [erts-10.6.2] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [hipe]

Interactive Elixir (1.9.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> ExileTest.hello()
piping

…and it sits there blocking forever until I ^C my way out.

I’ve tried a bunch of different ways to read input on the JS side: process.stdin.read(), process.stdin.on('data', ...).on('end', ...) handlers, fs.readFileSync(0), and whatever else I could think of. Exile never seems to send it any data.

Any thoughts? Would you prefer I open an issue in the GitHub repo instead?

akash-akya

akash-akya OP

The forker spawn driver was added fairly recently in ERTS 8.0 (search for erl_child_setup ) because of 3-5x better performance, even 10x . This is way after Linux implemented copy-on-write.

Ok, I was thinking about the memory overhead. But its CPU overhead due to memory pages update.

I did basic benchmark on mac. forking from beam process is indeed around 3 times slower than erl_child_setup (sometimes 4.5 times). Note that it is still ~1000 forks per second. practically speaking, I’m not sure if raw forking speed is important, because we usually hit other system limits before we start to operate at this rate. Nevertheless, this is nice to have, thanks for pointing it out, I’ll look into improving this. Here are the results

The scenario I imagined is when the Erlang node crashed or is forcefully killed. An external shim can detect when that happens then clean up by killing all child processes before exit.

Yes, exile does not kill processes if beam crashes or if the beam is killed with SIGKILL. This is similar to how all shells and other languages I checked behave. Because any kind of fix for this involves spawning another “watcher” os process (one or many). This is unnecessary overhead for well-behaved programs. Moreover, the user can fix this themselves by running a script if required.

On the same note, other kill signals can be handled (such as SIGTERM). WIP changes.

akash-akya

akash-akya OP

Exile.stream!(input_stream: File.stream!("priv/input.txt", [], 65536), stderr_to_console: true)

Hi, the field name is input not input_stream. Exile is ignoring input_stream param. It’s hanging because there is no input.

Anyway, exile should fail with proper error message for an invalid option. Thanks for pointing it out. I’ll fix this

jayjun

jayjun

Thanks for investigating! 1,000 forks per second is indeed a lot of headroom.

This is unnecessary overhead for well-behaved programs.

Trade off is perfectly understandable. I specifically have trouble with this claim,

Misbehaving programs are the ones that become zombies (see Port docs). Exile is no different from a plain port in this respect.

On the other hand, a port-based library can double as the kill-on-exit script, thus guarantee no zombies. Middleware solutions are superior here, instead of comparing with “all shells and other languages”.

Where Next? Top

Trending in Announcing Top

achempion
Hi, I would like to tell about my initiative to further maintain and develop Waffle project which is the fork of Arc library. The progre...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
danschultzer
I’ve moved the strategies out of PowAssent and created a multi-provider framework for Elixir. This way the strategies can be used in any ...
New
gtcode
Aloha gang, There’s a new class of AI model out from TypeSafe. Jev is their flagship model. You can learn about it here: ...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
AstonJ
With advancements in AI gathering pace and set to increasingly accelerate, perhaps it’s worth having an annual thread on the topic - feel...
New
bartblast
If you haven’t tried Hologram yet, what’s stopped you? Here’s why I’m asking. Building a framework turns out to be two jobs, the framewo...
New
mat-hek
Popcorn, the library that runs Elixir code in the browser, is now stable :popcorn: This is possible because it now builds on a very stab...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews