firestore-db-and-auth-rs icon indicating copy to clipboard operation
firestore-db-and-auth-rs copied to clipboard

already mutably borrowed: BorrowError

Open yoannfleurydev opened this issue 2 years ago • 4 comments

Describe the bug

My program did stop with the following error:

thread 'tokio-runtime-worker' panicked at 'already mutably borrowed: BorrowError', /Users/yfleury/.cargo/registry/src/github.com-1ecc6299db9ec823/firestore-db-and-auth-0.6.1/src/sessions.rs:308:45

Looking at the sessions.rs line (308 in the log, 488 in this repo source code) it seems that the call to .borrow() is panicking.

image

I'm kind of new to Rust so I'm not sure everything is right on my side.

To Reproduce

Here is my program https://github.com/BearStudio/twitch-listener

Steps to reproduce the behavior:

  1. Run with cargo run
  2. Use documents like
struct Question {
    id: String,
    username: String,
    message: String,
    timestamp: String,
}

Expected behavior

I expect my program not to panic.

yoannfleurydev avatar Nov 07 '21 14:11 yoannfleurydev

Hi @yoannfleurydev, I am getting this same issue as well, did you ever find a solution?

blessedbythestorm avatar Mar 10 '22 10:03 blessedbythestorm

Hi @yoannfleurydev, I am getting this same issue as well, did you ever find a solution?

Hi, no, not at this time sorry.

yoannfleurydev avatar Mar 10 '22 11:03 yoannfleurydev

So I fixed the issue by reducing the scope of the borrows but for some reason it is still failing to renew the authorization, there doesn't seem to be any other errors in the process. This is all done through the standard ServiceSession that comes with the crate and works perfectly fine for the first hour.

pub mod service_account {
...

    impl super::FirebaseAuthBearer for Session {
    ...

        fn access_token(&self) -> String {

            if jwt_update_expiry_if(&mut self.jwt.borrow_mut(), self.refresh_minutes) {
                if let Some(secret) = self.credentials.keys.secret.as_ref() {
                    match self.jwt.borrow().encode(&secret.deref()) {
                        Ok(v) => {
                            match v.encoded() {
                                Ok(v2) => {
                                    self.access_token_.swap(&RefCell::new(v2.encode()));
                                }
                                Err(e) => {
                                    panic!("{:?}", e)
                                }
                            }
                        }
                        Err(e) => {
                            panic!("{}", e);
                        }
                    }
                }
            }

            self.access_token_.borrow().clone()
        }
    }
}

After the hour has passed requests start returning APIError(401, "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential.. The panics never trigger either, there is no indication that the auth refresh failed. Could it be possible I am doing something wrong?

This is how my initialization looks:

impl Firestore {
    pub fn new() -> Firestore {
        let cred = Credentials::from_file("firebase-service-account.json")
            .expect("Failed to load service file!");

        let session = ServiceSession::new(cred, 59)
            .expect("Failed to create session!");

        Firestore {
            session
        }
    }
}

@davidgraeff?

blessedbythestorm avatar Mar 13 '22 12:03 blessedbythestorm

+1 having the same issue

redvg avatar Mar 31 '22 08:03 redvg

Should be fixed. This is an RwLock now.

davidgraeff avatar Jan 22 '24 16:01 davidgraeff