Gracefully Winding Down a C++ Library’s Resources From Rust (with an Async Twist)
Pete LeVasseur
Rust @ Woven by Toyota ???? | Chair @ Rust Safety-Critical Consortium Coding Guidelines Subcommittee | Maintainer @ Eclipse uProtocol
When implementing the uP-L1 Transport library over top of Connected Vehicle Systems Alliance (COVESA) 's vsomeip, I made the choice as pointed out in another article to protect access to mutations of vsomeip state by protecting access behind a dedicated set of threads, one per vsomeip application and used Rust's tokio async framework and channels to communicate to those dedicated threads those mutation commands.
Oftentimes in Rust, if all resources are being managed by the borrow checker, they will be dropped appropriately and no resources will leak. However, in this case resources are owned by a C++ library we need to wind down appropriately.
The twist is that since the Drop impl we must write to clean them up is not async, but we're using an async framework to dispatch work to vsomeip, we end up having to do a bit of careful juggling to ensure we can also shut down the tokio::runtime::Runtime.
Read on for the details! ??