Skip to content

Commit

Permalink
Fixed the file descriptor leak issue caused by incorrect runtime usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Kim committed Sep 30, 2024
1 parent ef2b74b commit af692fd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 37 deletions.
19 changes: 13 additions & 6 deletions envsetup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,21 @@ function remote_test() {
function run_test() {
cargo run --bin rsb_hub & sleep 1
cargo run --bin test_service & sleep 1
RUST_BACKTRACE=1 cargo test
for i in $(seq 1 100); do RUST_BACKTRACE=1 cargo test || break; done
}

# function run_test_async() {
# cargo run --bin rsb_hub & sleep 1
# cargo run --bin test_service_async & sleep 1
# cargo test
# }
function remote_test_async() {
read_remote_linux
remote_sync
command ssh "$remote_user_host" -t "bash -c \"source ~/.profile && cd $remote_directory && \
source ./envsetup.sh && run_test_async \""
}

function run_test_async() {
cargo run --bin rsb_hub & sleep 1
cargo run --bin test_service_async & sleep 1
for i in $(seq 1 100); do RUST_BACKTRACE=1 cargo test || break; done
}

declare -a publish_dirs=("rsbinder-aidl" "rsbinder" "rsbinder-tools")

Expand Down
68 changes: 37 additions & 31 deletions tests/src/bin/test_service_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,9 +692,9 @@ impl IRepeatFixedSizeArray::IRepeatFixedSizeArrayAsyncService for FixedSizeArray
}
}

fn rt() -> TokioRuntime<tokio::runtime::Runtime> {
let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
TokioRuntime(rt)
fn rt() -> TokioRuntime<tokio::runtime::Handle> {
// let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
TokioRuntime(tokio::runtime::Handle::current())
}

fn main() {
Expand All @@ -703,34 +703,40 @@ fn main() {
ProcessState::init_default();
ProcessState::start_thread_pool();

let service_name = <BpTestService as ITestService::ITestService>::descriptor();
let service =
BnTestService::new_async_binder(TestService::default(), rt());
hub::add_service(service_name, service.as_binder()).expect("Could not register service");

let versioned_service_name = <BpFooInterface as IFooInterface::IFooInterface>::descriptor();
let versioned_service =
BnFooInterface::new_async_binder(FooInterface, rt());
hub::add_service(versioned_service_name, versioned_service.as_binder())
.expect("Could not register service");

let nested_service_name =
<INestedService::BpNestedService as INestedService::INestedService>::descriptor();
let nested_service = INestedService::BnNestedService::new_async_binder(
NestedService,
rt(),
);
hub::add_service(nested_service_name, nested_service.as_binder())
let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
runtime.block_on(async {
let service_name = <BpTestService as ITestService::ITestService>::descriptor();
let service =
BnTestService::new_async_binder(TestService::default(), rt());
hub::add_service(service_name, service.as_binder()).expect("Could not register service");

let versioned_service_name = <BpFooInterface as IFooInterface::IFooInterface>::descriptor();
let versioned_service =
BnFooInterface::new_async_binder(FooInterface, rt());
hub::add_service(versioned_service_name, versioned_service.as_binder())
.expect("Could not register service");

let nested_service_name =
<INestedService::BpNestedService as INestedService::INestedService>::descriptor();
let nested_service = INestedService::BnNestedService::new_async_binder(
NestedService,
rt(),
);
hub::add_service(nested_service_name, nested_service.as_binder())
.expect("Could not register service");

let fixed_size_array_service_name =
<IRepeatFixedSizeArray::BpRepeatFixedSizeArray as IRepeatFixedSizeArray::IRepeatFixedSizeArray>::descriptor();
let fixed_size_array_service = IRepeatFixedSizeArray::BnRepeatFixedSizeArray::new_async_binder(
FixedSizeArrayService,
rt(),
);
hub::add_service(fixed_size_array_service_name, fixed_size_array_service.as_binder())
.expect("Could not register service");

let fixed_size_array_service_name =
<IRepeatFixedSizeArray::BpRepeatFixedSizeArray as IRepeatFixedSizeArray::IRepeatFixedSizeArray>::descriptor();
let fixed_size_array_service = IRepeatFixedSizeArray::BnRepeatFixedSizeArray::new_async_binder(
FixedSizeArrayService,
rt(),
);
hub::add_service(fixed_size_array_service_name, fixed_size_array_service.as_binder())
.expect("Could not register service");

ProcessState::join_thread_pool().expect("Failed to join thread pool");
// By awaiting `pending`, we yield to the runtime. This results in the current-thread
// runtime being driven by the current thread (the main thread in this case). E.g., this
// means that anything spawned with `tokio::spawn` will run on this thread.
std::future::pending().await
})
}

0 comments on commit af692fd

Please sign in to comment.