r/rust • • 1d ago

Runtime reloadable and removable plugins in rust like cordis

Hi rust community, I think I learnt rust to build webscraper that was efficient for a college project 2,3 years ago given my knowledge is almost none, let's dig into my problem and I would love to have some perspectives or opinions or anything at this point lol,

So I heard rust doesn't have a stable ABI and more and more .

I was inspired by cordis (the javascript meta framework (ik js mentioned)) I wanted to be able to do something of my own harness ik ik lot of questions on why you want to make you own blah blah but I have a potato pc comes with a fair share of problems .

Now the main question is there are implementation of cordis in rust but they truly don't do like do 0 memory waste or 0 cpuc cycle waste not truly 0 but I hope you get the point there is GC so .

I know that this is going to be a tradeoff right now my only options seems to build a binary and replace it inplace with a new one while keeping track of the old one and replacing when it's all good. But thats not really a good way given I have keep track of current binary and previous binary and my original binary just incase so how do I do this ?

TLDR: how do I achieve runtime plugin reloading the rusty way , i do not want to do wasm and all that integrations would leave the point of being rusty and not bloating it.

9 Upvotes

21 comments sorted by

11

u/Emetros 1d ago

I don’t get the issue with wasm? I’ve seen quite a few videos from dev conf saying that it’s far from being as terrible as people say it is. Pretty much every blog post I read said it was one of the easiest and safest plugin implementation. Are these posts wrong?

2

u/XyraNelle 1d ago

wasm seems way less of a headache than juggling binaries lol i'm with you on that

1

u/kissfrosted 1d ago

sounds like you've looked into this way more than me, got any better ideas for a rusty approach?

1

u/Emetros 1d ago

There’s another comment in the thread mentioning the stable_abi/stabby way and IPC calls. They are basically the only other options I’ve looked into but from what I remember IPC calls is less performant. Stable_abi/FFI using extern C is probably the fastest option I’ve seen but it is a bit more annoying when it comes to reload the library if I remember correctly. I think there’s a crate made for that called hot_reload. Here’s one of the blog posts I’ve been reading. There is a list of posts about plugin systems

1

u/FlirtyChunz_ 1d ago

wasm always felt like overkill to me too, binaries swapping sounds way less complicated

1

u/sansmorixz 1d ago

Thing is wasm is not yet qs complete as native rust is. Hell wasm64 is just now getting started and don’t even get me started on wasi p3 standardisations and the component model.

I was trying to build a side project based on wasm, and I spent way more time debugging issues in wasm official libs and their runtimes instead of focusing on what i wanted to build.

0

u/FlirtyChunz_ 1d ago

wasm sounds tempting for the safety but i get wanting that pure rust vibe instead, curious if there's a middle ground

1

u/ImplementCreative106 1d ago

looks like binary swapping is the only way for what i would want man this sucks

-1

u/ImplementCreative106 1d ago

Oh yeah and also I don't want to loose bunch of performance for no reason if it's just one time compile I might as well pay the price of running it 24*7 once

-2

u/ImplementCreative106 1d ago

I used a little bit of AI for this pardon me for that,

yeah wasm is totally fine if you actually want a sandbox for untrusted code. my issue is my plugins need to do OS-level stuff (spawning processes/PTYs, sqlite, sockets) and hook straight into internal traits. with wasm i'd be pulling in wasmtime and writing a ton of WIT/WASI glue just to punch holes through a sandbox i don't even need, plus taking a hit on binary size and cold start. it's not that wasm is terrible, it's just paying a bunch of complexity and runtime overhead to actively fight the sandbox when i already trust the plugins

1

u/Emetros 1d ago

No worries. I was working on the same kind of stuff as you do recently and I picked wasmtime because I wanted interoperability, hot reload and sandboxed environment. In the end, I have to give up on sandboxed environment as sometimes a plugin needs to install a binary

9

u/min6char 1d ago

Runtime reloadable plugins and stable ABI are essentially the same thing, if you want reloadable plugins in a language that doesn't have stable ABI, you need to figure out how to emulate stable ABI behavior. So the communications across plugins need to use some kind of marshalling type in the middle, which means you need to accept some amount of duplicated memory. Some common strategies for this in Rust:

- plugin interfaces is FFI bindings (i.e, C bindings). This lets you lean on the fact htat C does have stable ABI

- plugin interfaces are based on some kind of IPC protocol like protobuf, cap'n' proto, flatbuffers, etc. Pick your favorite to get as little waste as you want.

2

u/EdgelordMcMeme_Lord 1d ago

i keep a little bottle of hot sauce on my desk for situations exactly like this

the C FFI route is probably the path of least resistance if you're set on keeping it all in-process. you're still marshalling across the boundary but at least you're not spinning up a whole IPC stack for it. the duplication is kind of the price of admission here, no real way around that

if you go the IPC route cap'n proto is absurdly fast for this kind of thing, basically zero-copy on the wire. still gotta serialize somewhere though

1

u/min6char 20h ago

You don't need to do full IPC. You can just marshall the messages into their wire formats and then pass a pointer to the buffer across the plugin boundary (you need FFI for that too of course). For cap'n proto and flatbuffers this is extremely cheap and/or free.

(EDIT: but unless you can tolerate the ergonomics of plumbing cap'n proto types through the whole plugin inplementation, at SOME point you've got to copy back out of there...)

1

u/gschukin 1d ago

There is a project CordisClaw that do the same as Cordis in rust.

2

u/ImplementCreative106 1d ago

The real tradeoffs CordisClaw makes Hard Rust dylib ABI lock-in: Because it uses Rust dylib (not cdylib/C-ABI) so host and plugins share cordis-plugin-sdk symbols, every .so must be built with the exact same rustc version, target, flags, and SDK build. They guard this with ABI fingerprints + SHA-256 checks and fail-fast loading (zero fallback). Upgrade rustc or tweak a shared SDK struct, and every plugin breaks until rebuilt together anyway.

Well looks like that's also kinda not so useful compared to just compiling it and then using the new binary .....

I understand I have to make tradeoffs but is there anything better ?

1

u/addmoreice 1d ago

Not really. I've basically landed on the exact same structure and for the same exact reasons. It works, it gives you the nice guards and protections you would want with rust, and lets you use your harness to update your harness, and that is pretty awesome.

0

u/CradleZero 1d ago

If you do end up testing WASM for the hot-reload boundary, curious where the pain actually shows up first: marshalling overhead across the ABI, or cold-start latency when swapping instances live?

1

u/ImplementCreative106 1d ago

I would have rather better way of using debug cache and then rebuilding and then using release build , I wouldn't be going wasm way atleast for my usecase until and unless there is something I am missing , hey I am newbie remember

0

u/CradleZero 1d ago

Fair call, staying with normal debug/release rebuilds is way simpler until you actually need third-party sandboxing. Good luck with the plugin setup.

0

u/addmoreice 1d ago

I've been building my own harness I call 'suspenders' which hasn't yet been regurgitated up onto my github account, but I have had to deal with this same issue.

I've got a monitoring process, I've got a tight small core that does plugin loading and it works. It's a bit clunky and I've seen a few runs early on where things failed to load when I thought it should have, but in general it *does* work. Now.

It took a *lot* of tests to get everything stable. I've basically had to wrap tests around both sides of everything and write fake plugins that fail in more and more weird ways just to nail down that everything works. *half* the code base is tests built around confirming everything can go down and come back up. It's kind of nuts.

That being said, it *does* work and now that the core is working well, the actual rest of the functionality is coming along swimmingly. Just, be warned, if this is the route you plan to go down, you will need to do a lot of testing to be confident it's working correctly. It's just the nature of the beast.