当调用一个web服务线程'actix-rt'时 rust 恐慌错误|系统:0|仲裁者:0

qncylg1j  于 2023-03-08  发布在  其他
关注(0)|答案(1)|浏览(168)

这是一个简单的rest端点,它接受post输入并调用另一个rest端点

use actix_web::{web, App, HttpResponse, HttpServer};
use solana_client::rpc_client::RpcClient;
use std::sync::Arc;
use serde::{Deserialize, Serialize};

fn create_rpc_client(endpoint: String) -> RpcClient {
    RpcClient::new(endpoint)
}

#[derive(Debug, Serialize, Deserialize)]
struct EpochInfoRequest {
    network: Option<String>,
}

fn get_current_epoch(rpc_client: Arc<RpcClient>) -> u64 {
    // Get the current epoch
    let epoch_info = rpc_client.get_epoch_info().unwrap();
    epoch_info.epoch
}

async fn get_epoch_info(info: web::Json<EpochInfoRequest>) -> HttpResponse {
    // Get the network name from the request
    let network = match &info.network {
        Some(network) => network,
        None => "mainnet", // Default network if not provided
    };

    // Choose a Solana network to connect to based on the request path
    let endpoint = match network {
        "devnet" => "https://api.devnet.solana.com".to_string(),
        "testnet" => "https://api.testnet.solana.com".to_string(),
        "mainnet" => "https://api.mainnet-beta.solana.com".to_string(),
        _ => network.to_string(), // Custom network
    };

    println!("{}",endpoint);
    let rpc_client = Arc::new(create_rpc_client(endpoint));

    // Get the current epoch
    let current_epoch = get_current_epoch(rpc_client);

    println!("{}",current_epoch);

    HttpResponse::Ok().json(current_epoch)
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .service(
                web::resource("/epoch")
                    .route(web::post().to(get_epoch_info))
            )
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

运行程序时

❯ curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"network":"testnet"}' \
  http://localhost:8080/epoch

Error.log

❯ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.44s
     Running `target/debug/solana_rpc`
https://api.testnet.solana.com
thread 'actix-rt|system:0|arbiter:0' panicked at 'can call blocking only when running on the multi-threaded runtime', /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/solana-client-1.14.16/src/rpc_client.rs:4185:9
stack backtrace:
   0: rust_begin_unwind
             at /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/std/src/panicking.rs:575:5
   1: core::panicking::panic_fmt
             at /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/panicking.rs:64:14
   2: core::panicking::panic_display
             at /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/panicking.rs:135:5
   3: tokio::runtime::scheduler::multi_thread::worker::block_in_place
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/scheduler/multi_thread/worker.rs:348:9
   4: tokio::task::blocking::block_in_place
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/task/blocking.rs:78:9
   5: solana_client::rpc_client::RpcClient::invoke
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/solana-client-1.14.16/src/rpc_client.rs:4185:9
   6: solana_client::rpc_client::RpcClient::get_epoch_info
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/solana-client-1.14.16/src/rpc_client.rs:2824:9
   7: solana_rpc::get_current_epoch
             at ./src/main.rs:17:22
   8: solana_rpc::get_epoch_info::{{closure}}
             at ./src/main.rs:59:25
   9: actix_web::handler::handler_service::{{closure}}::{{closure}}
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-web-4.3.1/src/handler.rs:106:21
  10: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/future/future.rs:124:9
  11: <actix_web::resource::Resource<T> as actix_web::service::HttpServiceFactory>::register::{{closure}}::{{closure}}
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-web-4.3.1/src/resource.rs:414:27
  12: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/future/future.rs:124:9
  13: <actix_service::map_err::MapErrFuture<A,Req,F,E> as core::future::future::Future>::poll
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-service-2.0.2/src/map_err.rs:99:9
  14: actix_http::h1::dispatcher::InnerDispatcher<T,S,B,X,U>::poll_response
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-http-3.3.1/src/h1/dispatcher.rs:472:27
  15: <actix_http::h1::dispatcher::Dispatcher<T,S,B,X,U> as core::future::future::Future>::poll
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-http-3.3.1/src/h1/dispatcher.rs:1145:43
  16: <actix_http::service::HttpServiceHandlerResponse<T,S,B,X,U> as core::future::future::Future>::poll
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-http-3.3.1/src/service.rs:780:45
  17: <actix_service::and_then::AndThenServiceResponse<A,B,Req> as core::future::future::Future>::poll
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-service-2.0.2/src/and_then.rs:114:37
  18: <actix_server::service::StreamService<S,I> as actix_service::Service<(actix_server::worker::WorkerCounterGuard,actix_server::socket::MioStream)>>::call::{{closure}}
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-server-2.2.0/src/service.rs:75:30
  19: tokio::runtime::task::core::Core<T,S>::poll::{{closure}}
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/task/core.rs:223:17
  20: tokio::loom::std::unsafe_cell::UnsafeCell<T>::with_mut
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/loom/std/unsafe_cell.rs:14:9
  21: tokio::runtime::task::core::Core<T,S>::poll
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/task/core.rs:212:13
  22: tokio::runtime::task::harness::poll_future::{{closure}}
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/task/harness.rs:476:19
  23: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/panic/unwind_safe.rs:271:9
  24: std::panicking::try::do_call
             at /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/std/src/panicking.rs:483:40
  25: ___rust_try
  26: std::panicking::try
             at /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/std/src/panicking.rs:447:19
  27: std::panic::catch_unwind
             at /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/std/src/panic.rs:137:14
  28: tokio::runtime::task::harness::poll_future
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/task/harness.rs:464:18
  29: tokio::runtime::task::harness::Harness<T,S>::poll_inner
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/task/harness.rs:198:27
  30: tokio::runtime::task::harness::Harness<T,S>::poll
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/task/harness.rs:152:15
  31: tokio::runtime::task::raw::poll
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/task/raw.rs:255:5
  32: tokio::runtime::task::raw::RawTask::poll
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/task/raw.rs:200:18
  33: tokio::runtime::task::LocalNotified<S>::run
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/task/mod.rs:394:9
  34: tokio::task::local::LocalSet::tick::{{closure}}
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/task/local.rs:615:63
  35: tokio::runtime::coop::with_budget
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/coop.rs:107:5
  36: tokio::runtime::coop::budget
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/coop.rs:73:5
  37: tokio::task::local::LocalSet::tick
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/task/local.rs:615:31
  38: <tokio::task::local::RunUntil<T> as core::future::future::Future>::poll::{{closure}}
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/task/local.rs:927:16
  39: tokio::task::local::LocalSet::with::{{closure}}
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/task/local.rs:684:13
  40: std::thread::local::LocalKey<T>::try_with
             at /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/std/src/thread/local.rs:446:16
  41: std::thread::local::LocalKey<T>::with
             at /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/std/src/thread/local.rs:422:9
  42: tokio::task::local::LocalSet::with
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/task/local.rs:667:9
  43: <tokio::task::local::RunUntil<T> as core::future::future::Future>::poll
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/task/local.rs:913:9
  44: tokio::task::local::LocalSet::run_until::{{closure}}
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/task/local.rs:573:18
  45: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/future/future.rs:124:9
  46: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}::{{closure}}::{{closure}}
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/scheduler/current_thread.rs:541:57
  47: tokio::runtime::coop::with_budget
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/coop.rs:107:5
  48: tokio::runtime::coop::budget
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/coop.rs:73:5
  49: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}::{{closure}}
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/scheduler/current_thread.rs:541:25
  50: tokio::runtime::scheduler::current_thread::Context::enter
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/scheduler/current_thread.rs:350:19
  51: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/scheduler/current_thread.rs:540:36
  52: tokio::runtime::scheduler::current_thread::CoreGuard::enter::{{closure}}
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/scheduler/current_thread.rs:615:57
  53: tokio::macros::scoped_tls::ScopedKey<T>::set
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/macros/scoped_tls.rs:61:9
  54: tokio::runtime::scheduler::current_thread::CoreGuard::enter
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/scheduler/current_thread.rs:615:27
  55: tokio::runtime::scheduler::current_thread::CoreGuard::block_on
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/scheduler/current_thread.rs:530:19
  56: tokio::runtime::scheduler::current_thread::CurrentThread::block_on
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/scheduler/current_thread.rs:154:24
  57: tokio::runtime::runtime::Runtime::block_on
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/runtime/runtime.rs:302:47
  58: tokio::task::local::LocalSet::block_on
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.26.0/src/task/local.rs:534:9
  59: actix_rt::runtime::Runtime::block_on
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-rt-2.8.0/src/runtime.rs:82:9
  60: actix_rt::arbiter::Arbiter::with_tokio_rt::{{closure}}
             at /Users/anish/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-rt-2.8.0/src/arbiter.rs:144:21
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
m1m5dgzv

m1m5dgzv1#

solana_client::rpc_client::RpcClient使用tokio::task::blocking::block_in_place来提供同步的阻塞API。但是actix-rt使用的默认运行时是单线程的,不允许阻塞任务。由于您已经处于异步运行时中,我建议您改用solana_client::nonblocking::rpc_client::RpcClient

- use solana_client::rpc_client::RpcClient;
+ use solana_client::nonblocking::rpc_client::RpcClient;
- fn get_current_epoch(rpc_client: Arc<RpcClient>) -> u64 {
+ async fn get_current_epoch(rpc_client: Arc<RpcClient>) -> u64 {
      // Get the current epoch
-     let epoch_info = rpc_client.get_epoch_info().unwrap();
+     let epoch_info = rpc_client.get_epoch_info().await.unwrap();
      epoch_info.epoch
-     let current_epoch = get_current_epoch(rpc_client);
+     let current_epoch = get_current_epoch(rpc_client).await;

相关问题