Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

there is no reactor running, must be called from the context of Tokio runtime #266

Closed
gangliao opened this issue Oct 28, 2020 · 7 comments
Closed
Labels
bug Something isn't working

Comments

@gangliao
Copy link

gangliao commented Oct 28, 2020

use lambda::{handler_fn, Context};
use serde_json::Value;

type Error = Box<dyn std::error::Error + Sync + Send + 'static>;

#[tokio::main]
async fn main() -> Result<(), Error> {
    lambda::run(handler_fn(handler)).await?;
    Ok(())
}

async fn handler(event: Value, _: Context) -> Result<Value, Error> {
    Ok(event)
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[tokio::test]
    async fn handler_handles() {
        let event = json!({
            "answer": 42
        });
        assert_eq!(
            handler(event.clone(), Context::default())
                .await
                .expect("expected Ok(_) value"),
            event
        )
    }
}

Hi @softprops @leepa @bdonlan @endor @marcbowes ,

When I run the code you provided above in the Lambda function, I got the following error. How can I fix it?

START RequestId: 067ed0e7-d441-4ed2-a77c-38f9b90bc0b5 Version: $LATEST
thread 'main' panicked at 'there is no reactor running, must be called from the context of Tokio runtime', /home/gangliao/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.22/src/io/driver/mod.rs:202:14
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
END RequestId: 067ed0e7-d441-4ed2-a77c-38f9b90bc0b5
REPORT RequestId: 067ed0e7-d441-4ed2-a77c-38f9b90bc0b5	Duration: 193.80 ms	Billed Duration: 200 ms	Memory Size: 128 MB	Max Memory Used: 6 MB	
RequestId: 067ed0e7-d441-4ed2-a77c-38f9b90bc0b5 Error: Runtime exited with error: exit status 101
Runtime.ExitError
@gangliao
Copy link
Author

gangliao commented Oct 28, 2020

If I change the code as follows:

use lambda::{handler_fn, Context};
use serde_json::Value;
use tokio::runtime;

type Error = Box<dyn std::error::Error + Sync + Send + 'static>;

async fn handler(event: Value, _: Context) -> Result<Value, Error> {
    Ok(event)
}

fn main() -> Result<(), Error> {
    let rt = runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .unwrap();
    rt.block_on(async {
        lambda::run(handler_fn(handler)).await?;
        Ok(())
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[tokio::test]
    async fn handler_handles() {
        let event = json!({
            "answer": 42
        });

        assert_eq!(
            handler(event.clone(), Context::default())
                .await
                .expect("expected Ok(_) value"),
            event
        )
    }
}
db9ec823/async-stream-0.2.1/src/async_stream.rs:53:17
  63: <core::pin::Pin<P> as futures_core::stream::Stream>::poll_next
             at /home/gangliao/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-core-0.3.7/src/stream.rs:129:9
  64: futures_util::stream::stream::StreamExt::poll_next_unpin
             at /home/gangliao/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.7/src/stream/stream/mod.rs:1330:9
  65: <futures_util::stream::stream::next::Next<St> as core::future::future::Future>::poll
             at /home/gangliao/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.7/src/stream/stream/next.rs:35:9
  66: lambda::run_inner::{{closure}}
             at /home/gangliao/.cargo/git/checkouts/aws-lambda-rust-runtime-7c865cce90132439/13aa8f0/lambda/src/lib.rs:203:29
  67: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /home/gangliao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/future/mod.rs:80:19
  68: lambda::run::{{closure}}
             at /home/gangliao/.cargo/git/checkouts/aws-lambda-rust-runtime-7c865cce90132439/13aa8f0/lambda/src/lib.rs:158:5
  69: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /home/gangliao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/future/mod.rs:80:19
  70: bootstrap::main::{{closure}}
             at /home/gangliao/ServerlessCQ/src/lambda.rs:17:9
  71: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /home/gangliao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/future/mod.rs:80:19
  72: tokio::park::thread::CachedParkThread::block_on::{{closure}}
             at /home/gangliao/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.3.2/src/park/thread.rs:263:54
  73: tokio::coop::with_budget::{{closure}}
             at /home/gangliao/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.3.2/src/coop.rs:121:9
  74: std::thread::local::LocalKey<T>::try_with
             at /home/gangliao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/local.rs:272:16
  75: std::thread::local::LocalKey<T>::with
             at /home/gangliao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/local.rs:248:9
  76: tokio::coop::with_budget
             at /home/gangliao/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.3.2/src/coop.rs:114:5
  77: tokio::coop::budget
             at /home/gangliao/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.3.2/src/coop.rs:98:5
  78: tokio::park::thread::CachedParkThread::block_on
             at /home/gangliao/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.3.2/src/park/thread.rs:263:31
  79: tokio::runtime::enter::Enter::block_on
             at /home/gangliao/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.3.2/src/runtime/enter.rs:151:13
  80: tokio::runtime::thread_pool::ThreadPool::block_on
             at /home/gangliao/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.3.2/src/runtime/thread_pool/mod.rs:80:9
  81: tokio::runtime::Runtime::block_on
             at /home/gangliao/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.3.2/src/runtime/mod.rs:442:43
  82: bootstrap::main
             at /home/gangliao/ServerlessCQ/src/lambda.rs:16:5
  83: core::ops::function::FnOnce::call_once
             at /home/gangliao/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
END RequestId: 7f74f64d-bc39-418c-9e77-c77dd9a47805
REPORT RequestId: 7f74f64d-bc39-418c-9e77-c77dd9a47805	Duration: 610.64 ms	Billed Duration: 700 ms	Memory Size: 128 MB	Max Memory Used: 18 MB	
RequestId: 7f74f64d-bc39-418c-9e77-c77dd9a47805 Error: Runtime exited with error: exit status 101
Runtime.ExitError

@rimutaka
Copy link
Contributor

I noticed you use tokio-0.3.2. The code above should work with tokio-0.2.*.

I've just run into a similar problem in a unrelated project after upgrading tokio to the latest. No idea what's causing it.

@davidbarsky
Copy link
Contributor

This is blocked on Hyper getting support for Tokio 0.3: hyperium/hyper#2302. You can work around using Tokio 0.2 or use Tokio 0.3 with tokio-compat-02.

@gangliao
Copy link
Author

@davidbarsky @rimutaka Thanks. It works now.

@rimutaka
Copy link
Contributor

Thanks @davidbarsky ! I was just about to start looking into the cause.

Can you re-open this issue so it's visible until hyperium/hyper#2302 is resolved? There will be a few more people running into this problem.

@danjenson
Copy link

danjenson commented Nov 28, 2020

I'm getting a segmentation fault making a simple outgoing http reqwest using tokio-compat-02, and when I just use tokio = { version = "0.2", features = ["full"] } alone; I get the above error when 0.2 is not used. Also, @rimutaka, hyperium/hyper#2302 has been closed.

Cargo.toml:

[package]
name = "req"
version = "0.1.0"
authors = ["X"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
lambda_http = { version = "0.2.0-beta.1", git = "https://github.com/awslabs/aws-lambda-rust-runtime" }
tokio = { version = "0.3.4", features = ["full"] }
tokio-compat-02 = "0.1.2"
reqwest = { version = "0.10.9", features = ["json"] }

main.rs

use lambda_http::{
    handler,
    lambda::{self, Context},
    IntoResponse, Request,
};
// NOTE:
// when using tokio-0.2 alone, you get a segmentation fault
// when tokio-0.2 compatibility is enabled with tokio-0.3, you get a segmentation fault
// when tokio-0.2 compatibility is not enabled with tokio-0.3, 'main' panics because 'there is
// no reactor running, must be called from the context of Tokio runtime'
// use tokio_compat_02::FutureExt;

pub type Error = Box<dyn std::error::Error + Send + Sync + 'static>;

#[tokio::main]
async fn main() -> Result<(), Error> {
    lambda::run(handler(func)).await?;
    // NOTE: [tokio-0.2 compat] - this doesn't help
    // lambda::run(handler(func)).compat().await?;
    Ok(())
}

async fn func(_: Request, _: Context) -> Result<impl IntoResponse, Error> {
    let _res = reqwest::get("https://www.google.com").await?;
    // NOTE: [tokio-0.2 compat] - this doesn't help
    // let _res = reqwest::get("https://www.google.com").compat().await?;
    Ok("success")
}

deploy.sh:

PKG_CONFIG_ALLOW_CROSS=1 cargo build --release --target x86_64-unknown-linux-musl
cp target/x86_64-unknown-linux-musl/release/req target/x86_64-unknown-linux-musl/release/bootstrap
zip -j target/x86_64-unknown-linux-musl/release/bootstrap.zip target/x86_64-unknown-linux-musl/release/bootstrap
# aws lambda create-function \
#   --function-name reqwest-test \
#   --handler doesnt.matter \
#   --zip-file fileb://target/x86_64-unknown-linux-musl/release/bootstrap.zip \
#   --runtime provided \
#   --role [REMOVED] \
#   --environment Variables={RUST_BACKTRACE=1} \
#   --tracing-config Mode=Active
aws lambda update-function-code \
  --function-name reqwest-test \
  --zip-file fileb://target/x86_64-unknown-linux-musl/release/bootstrap.zip

event.json

{
  "headers": {
    "accept": "*/*",
    "content-length": "0",
    "host": "xxx.execute-api.us-east-1.amazonaws.com",
    "user-agent": "curl/7.64.1",
    "x-amzn-trace-id": "Root=1-5eb33c07-de25b420912dee103a5db434",
    "x-forwarded-for": "65.78.31.245",
    "x-forwarded-port": "443",
    "x-forwarded-proto": "https"
  },
  "isBase64Encoded": false,
  "rawPath": "/",
  "rawQueryString": "",
  "requestContext": {
    "accountId": "123456789012",
    "apiId": "xxx",
    "domainName": "xxx.execute-api.us-east-1.amazonaws.com",
    "domainPrefix": "xxx",
    "http": {
      "method": "GET",
      "path": "/",
      "protocol": "HTTP/1.1",
      "sourceIp": "65.78.31.245",
      "userAgent": "curl/7.64.1"
    },
    "requestId": "MIZRNhJtIAMEMDw=",
    "routeKey": "$default",
    "stage": "$default",
    "time": "06/May/2020:22:36:55 +0000",
    "timeEpoch": 1588804615616
  },
  "routeKey": "$default",
  "version": "2.0"
}

UPDATE: I made this a separate issue (#270) since it's unclear whether this is actually related to tokio -- running outside of the lambda runtime, it works fine.

@bahildebrand
Copy link
Contributor

With #276 being closed I think this issue can be closed. All Tokio ecosystem crates have been pulled up so these compatibility issues should no longer be present.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants