I'm trying to build a websocket server for a C library. This C library offers sync operations as well as callback operations (once an event happens).
I'm facing Segmentation Faults when the callback tries to access memory from outside the callback, and I've decided to set up the minimal example in order to reproduce it.
I've set up a repository with a minimal example in order to showcase it: https://github.com/cquintana-verbio/ffi-callback
How to trigger: Run
The sample C library I've created offers an Accumulator which will accumulate numbers. These numbers will be summed, and once a defined N is achieved, a callback will be triggered with the sum of the previous N numbers.
In the Rust code, the
trampoline and the whole callback interfacing with C has been extracted from the Unnofficial Rust FFI guide: https://s3.amazonaws.com/temp.michaelfbryan.com/callbacks/index.html#rust-closures
I've set up the following tests:
- Closure that checks the result. WORKS
- (Test that expects to fail) Closure that checks the result against an invalid value. This was done in order to prove that the callback is being triggered. WORKS.
- A closure that accesses an outer variable (mpsc channel). Segmentation fault.
- Same as 3, but with an async (tokio) channel, which is the actual case I want to build. Segmentation fault.
Whenever I try to access a variable that has been defined outside the callback, I have a segmentation fault.
Also, I assume that in the case 4 I will probably need to use a
Pin), as the memory may be moved between threads depending on how the async runtime schedules it. Am I right?
I'd like some help in order to make the tests pass, so I can start building the websocket server with async capabilities.
Thanks in advance!