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

Usage with multithread tokio runtime (axum). (dyn Permission + 'static) cannot be shared between threads safely. #186

Open
pongsakornsemsuwan opened this issue May 20, 2024 · 2 comments

Comments

@pongsakornsemsuwan
Copy link

Hi,
Newbie here so apologize in advance if this is a stupid question 😅
I'm trying to use the SDK, specifically the grant_token, with axum on multithread runtime environment.
I run into the problem calling following example.

pubnub
    .grant_token(usize)
    .resources(Option<[Box<permissions::Permission>]>)
    .patterns(Option<[Box<permissions::Permission>]>)
    .authorized_user_id(Option<String>)
    .meta(Option<HashMap<String, MetaValue>>)
    .execute()

The error I get is

error[E0277]: (dyn Permission + 'static) cannot be shared between threads safely
--> src/routes/player_route.rs:88:1
|
88 | #[debug_handler]
| ^^^^^^^^^^^^^^^^ (dyn Permission + 'static) cannot be shared between threads safely
|
= help: the trait Sync is not implemented for (dyn Permission + 'static)
= note: required for std::ptr::Unique<(dyn Permission + 'static)> to implement Sync
note: required because it appears within the type Box<(dyn Permission + 'static)>

Per my understanding, the cause is that .resources takes trait object Option<[Box<dyn Permission + 'static>] as a parameter whereas axum require Send for trait paremeter (tokio-rs/axum#2522) to ensure thread safety.

Anyone has successfully run the SDK on axum please enlighten me.

Thank you! 🙏

@Xavrax
Copy link
Contributor

Xavrax commented Jun 5, 2024

Hello @pongsakornsemsuwan !
First of all - I want to apologize for the late reply. We were a little bit busy and we've missed the notification about the new issue.

I've just tested our Pubnub SDK with Axium and it seems that some of our features work as expected (publish, subscribe) but grant_token is also failing the build for me.
That's very weird because Permission should be handled and consumed within the execute function and the lack of the Send trait shouldn't be a problem in this case.

Maybe we consume it too late and that's why compiler is not satisfied.

Anyway, I will take a closer look into that problem and fix it as soon as possible.

@Xavrax
Copy link
Contributor

Xavrax commented Jun 5, 2024

Okay. I was right. We consume that too late and that's why compiler is not happy with that.
I found very simple solution that can be found in #188 .

I will let you know whenever we will this PR.
Until that you can use the branch fix/permissions in your project to let it compile.

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

No branches or pull requests

2 participants