Skip to content

Unable to use third party async libraries due to Sync requirements #287

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

Closed
vultix opened this issue Feb 12, 2021 · 3 comments · Fixed by #289
Closed

Unable to use third party async libraries due to Sync requirements #287

vultix opened this issue Feb 12, 2021 · 3 comments · Fixed by #289
Assignees

Comments

@vultix
Copy link

vultix commented Feb 12, 2021

Pull Request #284 Introduced the Sync requirement for all http lambdas.

This broke any http lambdas using any of these libraries:

  • Rusoto
  • Mongodb
  • Sqlx

Reproduction

use lambda_http::{
    handler,
    lambda::{self, Context},
    IntoResponse, Request, RequestExt, Response,
};

use serde_json::{json, Value};
use rusoto_core::Region;
use rusoto_dynamodb::{DynamoDb, DynamoDbClient, ListTablesInput};

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

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

async fn func(_: Request, _: Context) -> Result<Value, Error> {
    let client = DynamoDbClient::new(Region::UsEast1);

    let output = client.list_tables(Default::default()).await?;

    dbg!{output};

    Ok(json!({"success": true}))
}
[dependencies]
serde_json = "1.0"
serde = "1.0"
tokio = {version = "1.0", features = ["macros"]}

lambda_http = { git = "https://github.com/awslabs/aws-lambda-rust-runtime/", branch = "master"}
lambda = { git = "https://github.com/awslabs/aws-lambda-rust-runtime/", branch = "master"}
rusoto_dynamodb = "0.46.0"
rusoto_core = "0.46.0"

This is the error I get:

error: future cannot be shared between threads safely
   --> src/main.rs:16:17
    |
16  |     lambda::run(handler(func)).await?;
    |                 ^^^^^^^ future returned by `func` is not `Sync`
    | 
   ::: ~/.cargo/git/checkouts/aws-lambda-rust-runtime-7c865cce90132439/9bd45c1/lambda-http/src/lib.rs:109:19
    |
109 | pub fn handler<H: Handler>(handler: H) -> Adapter<H> {
    |                   ------- required by this bound in `handler`
    |
    = help: the trait `Sync` is not implemented for `dyn Future<Output = std::result::Result<ListTablesOutput, RusotoError<ListTablesError>>> + Send`
note: future is not `Sync` as it awaits another future which is not `Sync`
   --> src/main.rs:23:18
    |
23  |     let output = client.list_tables(Default::default()).await?;
    |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ await occurs here on type `Pin<Box<dyn Future<Output = std::result::Result<ListTablesOutput, RusotoError<ListTablesError>>> + Send>>`, which is not `Sync`
@bahildebrand
Copy link
Contributor

I'm looking into this. I'll get back to you when I have a solution.

@bahildebrand
Copy link
Contributor

@vultix after looking at this more I think we were a bit aggressive adding the sync trait. Considering the amount of libraries that could not be used if we kept the sync trait, I've removed it. I ran this with the provided example. Let me know if this helps with your other lambdas.

@vultix
Copy link
Author

vultix commented Feb 14, 2021

Thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants