Skip to content

Commit 3e4021a

Browse files
committed
Enable Tokio eager handoff for node runtimes
Enable Tokio's eager driver handoff when building with tokio_unstable so node-owned runtimes can use the dedicated driver handoff path where available. Build binding artifacts and selected CI coverage with tokio_unstable so the cfg-gated runtime path remains exercised. Co-Authored-By: HAL 9000
1 parent eba2379 commit 3e4021a

7 files changed

Lines changed: 40 additions & 7 deletions

.github/workflows/benchmarks.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ jobs:
4343
echo "ELECTRS_EXE=$( pwd )/bin/electrs-${{ runner.os }}-${{ runner.arch }}" >> "$GITHUB_ENV"
4444
- name: Run benchmarks
4545
run: |
46-
cargo bench
46+
RUSTFLAGS="--cfg tokio_unstable" cargo bench

.github/workflows/rust.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ jobs:
8484
- name: Test on Rust ${{ matrix.toolchain }}
8585
if: "matrix.platform != 'windows-latest'"
8686
run: |
87-
RUSTFLAGS="--cfg no_download --cfg cycle_tests" cargo test
87+
RUSTFLAGS="--cfg no_download --cfg cycle_tests --cfg tokio_unstable" cargo test
8888
- name: Test with UniFFI support on Rust ${{ matrix.toolchain }}
8989
if: "matrix.platform != 'windows-latest' && matrix.build-uniffi"
9090
run: |
@@ -114,4 +114,4 @@ jobs:
114114
- uses: actions/checkout@v6
115115
- uses: dtolnay/rust-toolchain@nightly
116116
- uses: dtolnay/install@cargo-docs-rs
117-
- run: cargo docs-rs
117+
- run: cargo docs-rs

scripts/uniffi_bindgen_generate_kotlin.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ PROJECT_DIR="ldk-node-jvm"
55
PACKAGE_DIR="org/lightningdevkit/ldknode"
66
UNIFFI_BINDGEN_BIN="cargo run --manifest-path bindings/uniffi-bindgen/Cargo.toml"
77

8+
case " ${RUSTFLAGS:-} " in
9+
*" --cfg tokio_unstable "*|*" --cfg=tokio_unstable "*) ;;
10+
*) export RUSTFLAGS="${RUSTFLAGS:+$RUSTFLAGS }--cfg tokio_unstable" ;;
11+
esac
12+
813
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
914
rustup target add x86_64-unknown-linux-gnu || exit 1
1015
cargo build --release --target x86_64-unknown-linux-gnu --features uniffi || exit 1

scripts/uniffi_bindgen_generate_kotlin_android.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ TARGET_DIR="target"
55
PROJECT_DIR="ldk-node-android"
66
UNIFFI_BINDGEN_BIN="cargo run --manifest-path bindings/uniffi-bindgen/Cargo.toml"
77

8+
case " ${RUSTFLAGS:-} " in
9+
*" --cfg tokio_unstable "*|*" --cfg=tokio_unstable "*) RUSTFLAGS_WITH_TOKIO_UNSTABLE="${RUSTFLAGS:-}" ;;
10+
*) RUSTFLAGS_WITH_TOKIO_UNSTABLE="${RUSTFLAGS:+$RUSTFLAGS }--cfg tokio_unstable" ;;
11+
esac
12+
813
export_variable_if_not_present() {
914
local name="$1"
1015
local value="$2"
@@ -35,9 +40,9 @@ case "$OSTYPE" in
3540
PATH="$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/$LLVM_ARCH_PATH/bin:$PATH"
3641

3742
rustup target add x86_64-linux-android aarch64-linux-android armv7-linux-androideabi
38-
RUSTFLAGS="-C link-args=-Wl,-z,max-page-size=16384,-z,common-page-size=16384" CFLAGS="-D__ANDROID_MIN_SDK_VERSION__=21" AR=llvm-ar CARGO_TARGET_X86_64_LINUX_ANDROID_LINKER="x86_64-linux-android21-clang" CC="x86_64-linux-android21-clang" cargo build --profile release-smaller --features uniffi --target x86_64-linux-android || exit 1
39-
RUSTFLAGS="-C link-args=-Wl,-z,max-page-size=16384,-z,common-page-size=16384" CFLAGS="-D__ANDROID_MIN_SDK_VERSION__=21" AR=llvm-ar CARGO_TARGET_ARMV7_LINUX_ANDROIDEABI_LINKER="armv7a-linux-androideabi21-clang" CC="armv7a-linux-androideabi21-clang" cargo build --profile release-smaller --features uniffi --target armv7-linux-androideabi || exit 1
40-
RUSTFLAGS="-C link-args=-Wl,-z,max-page-size=16384,-z,common-page-size=16384" CFLAGS="-D__ANDROID_MIN_SDK_VERSION__=21" AR=llvm-ar CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER="aarch64-linux-android21-clang" CC="aarch64-linux-android21-clang" cargo build --profile release-smaller --features uniffi --target aarch64-linux-android || exit 1
43+
RUSTFLAGS="$RUSTFLAGS_WITH_TOKIO_UNSTABLE -C link-args=-Wl,-z,max-page-size=16384,-z,common-page-size=16384" CFLAGS="-D__ANDROID_MIN_SDK_VERSION__=21" AR=llvm-ar CARGO_TARGET_X86_64_LINUX_ANDROID_LINKER="x86_64-linux-android21-clang" CC="x86_64-linux-android21-clang" cargo build --profile release-smaller --features uniffi --target x86_64-linux-android || exit 1
44+
RUSTFLAGS="$RUSTFLAGS_WITH_TOKIO_UNSTABLE -C link-args=-Wl,-z,max-page-size=16384,-z,common-page-size=16384" CFLAGS="-D__ANDROID_MIN_SDK_VERSION__=21" AR=llvm-ar CARGO_TARGET_ARMV7_LINUX_ANDROIDEABI_LINKER="armv7a-linux-androideabi21-clang" CC="armv7a-linux-androideabi21-clang" cargo build --profile release-smaller --features uniffi --target armv7-linux-androideabi || exit 1
45+
RUSTFLAGS="$RUSTFLAGS_WITH_TOKIO_UNSTABLE -C link-args=-Wl,-z,max-page-size=16384,-z,common-page-size=16384" CFLAGS="-D__ANDROID_MIN_SDK_VERSION__=21" AR=llvm-ar CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER="aarch64-linux-android21-clang" CC="aarch64-linux-android21-clang" cargo build --profile release-smaller --features uniffi --target aarch64-linux-android || exit 1
4146
$UNIFFI_BINDGEN_BIN generate bindings/ldk_node.udl --lib-file "$TARGET_DIR"/x86_64-linux-android/release-smaller/libldk_node.so --language kotlin --config uniffi-android.toml -o "$BINDINGS_DIR"/"$PROJECT_DIR"/lib/src/main/kotlin || exit 1
4247

4348
JNI_LIB_DIR="$BINDINGS_DIR"/"$PROJECT_DIR"/lib/src/main/jniLibs/

scripts/uniffi_bindgen_generate_python.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
BINDINGS_DIR="./bindings/python/src/ldk_node"
33
UNIFFI_BINDGEN_BIN="cargo run --manifest-path bindings/uniffi-bindgen/Cargo.toml"
44

5+
case " ${RUSTFLAGS:-} " in
6+
*" --cfg tokio_unstable "*|*" --cfg=tokio_unstable "*) ;;
7+
*) export RUSTFLAGS="${RUSTFLAGS:+$RUSTFLAGS }--cfg tokio_unstable" ;;
8+
esac
9+
510
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
611
DYNAMIC_LIB_PATH="./target/release-smaller/libldk_node.so"
712
else

scripts/uniffi_bindgen_generate_swift.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ set -eox pipefail
44
BINDINGS_DIR="./bindings/swift"
55
UNIFFI_BINDGEN_BIN="cargo run --manifest-path bindings/uniffi-bindgen/Cargo.toml"
66

7+
case " ${RUSTFLAGS:-} " in
8+
*" --cfg tokio_unstable "*|*" --cfg=tokio_unstable "*) ;;
9+
*) export RUSTFLAGS="${RUSTFLAGS:+$RUSTFLAGS }--cfg tokio_unstable" ;;
10+
esac
11+
712
mkdir -p $BINDINGS_DIR
813

914
# Install rust target toolchains

src/runtime.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,20 @@ impl Runtime {
3232
let mode = match tokio::runtime::Handle::try_current() {
3333
Ok(handle) => RuntimeMode::Handle(handle),
3434
Err(_) => {
35-
let rt = tokio::runtime::Builder::new_multi_thread().enable_all().build()?;
35+
let mut runtime_builder = tokio::runtime::Builder::new_multi_thread();
36+
runtime_builder.enable_all();
37+
// Eager driver handoff lets Tokio move the I/O driver to another worker sooner
38+
// when this runtime's current worker enters `block_in_place` via `block_on`.
39+
// That marginally reduces the chance that a synchronous caller blocks the same
40+
// worker that would otherwise drive the I/O resource it is waiting on. It does
41+
// not solve the issue completely: it only applies to node runtimes we build
42+
// ourselves under `tokio_unstable`, does not affect externally supplied runtime
43+
// handles, and cannot guarantee that every persistence driver task needed by the
44+
// blocked future is already polling elsewhere. See the `StoreRuntime` docs below
45+
// for the full deadlock scenario and the temporary store-runtime isolation.
46+
#[cfg(tokio_unstable)]
47+
runtime_builder.enable_eager_driver_handoff();
48+
let rt = runtime_builder.build()?;
3649
RuntimeMode::Owned(rt)
3750
},
3851
};

0 commit comments

Comments
 (0)