scylladb / cpp-rust-driver

API-compatible rewrite of https://github.com/scylladb/cpp-driver as a wrapper for Rust driver.
GNU Lesser General Public License v2.1
11 stars 11 forks source link

CurrentThread runtime support - fix #106

Closed wprzytula closed 1 year ago

wprzytula commented 1 year ago

So far, the implementation of CassFuture caused deadlock when the only thread called cass_future_wait(). This happened because the thread went asleep on a Condvar until the future is resolved, but there were no other executors that would resolve the future. Although a runtime with no worker threads is not recommended for production usage, it is helpful e.g. for debugging (as it makes execution serialised). Therefore, we fix the problem by changing the way the thread waits for the future's result: instead of sleeping while the future is not yet resolved, it calls runtime.block_on(fut), which makes it the executor of that future. This not only fixes the problem, but possibly boosts efficiency. More specifically, it saves a sleep and a wakeup in the case of having a non-empty worker thread pool (the main thread does not wake a worker, falls asleep until the worker thread resolves the future and gets woken up in the end, but instead simply does the work by itself).

To test that the changes did solve the problem, change RUNTIME in lib.rs into a "current thread" variant: from:

pub static ref RUNTIME: Runtime = Runtime::new().unwrap();

into:

pub static ref RUNTIME: Runtime =
        tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .unwrap();

Pre-review checklist

wprzytula commented 1 year ago

v2: I've fixed the race condition and hardened CassFuture a bit: