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

Panic if called within a tokio runtime on Linux #132

Closed
connor4312 opened this issue Jun 13, 2023 · 6 comments · Fixed by #135 or #199
Closed

Panic if called within a tokio runtime on Linux #132

connor4312 opened this issue Jun 13, 2023 · 6 comments · Fixed by #135 or #199
Assignees

Comments

@connor4312
Copy link

connor4312 commented Jun 13, 2023

Calling .get_password() from within a Tokio runtime panics on Linux, using the linux-secret-service-rt-tokio-crypto-openssl feature.

thread 'main' panicked at 'Cannot start a runtime from within a runtime. This happens because a function (like `block_on`) attempted to block the current thread while the thread is being used to drive asynchronous tasks.', /home/connor/.cargo/registry/src/github.jparrowsec.cn-1ecc6299db9ec823/zbus-3.13.1/src/utils.rs:47:14
stack backtrace:
   0: rust_begin_unwind
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:579:5
   1: core::panicking::panic_fmt
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/panicking.rs:64:14
   2: tokio::runtime::context::enter_runtime
             at /home/connor/.cargo/registry/src/github.jparrowsec.cn-1ecc6299db9ec823/tokio-1.28.2/src/runtime/context.rs:183:9
   3: tokio::runtime::scheduler::current_thread::CurrentThread::block_on
             at /home/connor/.cargo/registry/src/github.jparrowsec.cn-1ecc6299db9ec823/tokio-1.28.2/src/runtime/scheduler/current_thread.rs:146:25
   4: tokio::runtime::runtime::Runtime::block_on
             at /home/connor/.cargo/registry/src/github.jparrowsec.cn-1ecc6299db9ec823/tokio-1.28.2/src/runtime/runtime.rs:302:47
   5: zbus::utils::block_on
             at /home/connor/.cargo/registry/src/github.jparrowsec.cn-1ecc6299db9ec823/zbus-3.13.1/src/utils.rs:47:5
   6: zbus::blocking::connection::Connection::session
             at /home/connor/.cargo/registry/src/github.jparrowsec.cn-1ecc6299db9ec823/zbus-3.13.1/src/blocking/connection.rs:33:9
   7: secret_service::blocking::SecretService::connect
             at /home/connor/.cargo/registry/src/github.jparrowsec.cn-1ecc6299db9ec823/secret-service-3.0.1/src/blocking/mod.rs:49:20
   8: keyring::secret_service::SsCredential::map_matching_items
             at /home/connor/.cargo/registry/src/github.jparrowsec.cn-1ecc6299db9ec823/keyring-2.0.3/src/secret_service.rs:239:18
   9: <keyring::secret_service::SsCredential as keyring::credential::CredentialApi>::get_password
             at /home/connor/.cargo/registry/src/github.jparrowsec.cn-1ecc6299db9ec823/keyring-2.0.3/src/secret_service.rs:133:38
  10: keyring::Entry::get_password
             at /home/connor/.cargo/registry/src/github.jparrowsec.cn-1ecc6299db9ec823/keyring-2.0.3/src/lib.rs:244:9

One workaround, which I'll probably take on my end, is to spawn the operation on another thread. A better but viral change would be exposing async methods to avoid zbus' blocking API.

microsoft/vscode#184792

@brotskydotcom
Copy link
Collaborator

I hate to say this, but with the way the blocking interface in secret-service works, this is pretty much "as designed." I will take a note to mention this issue (and the workaround of using a separate thread for all keyring calls) in the docs for the secret-service keystore. Feel free to submit a PR for a suggested doc change.

@landhb and I have talked about maybe building a "native" dbus keystore (based on the dbus crate) to avoid this problem.

I've never really considered exposing an async interface to keyring because so many of the platform native keystores are synchronous. It feels like it would be disingenuous to claim the API was async when every call to every keystore other than secret-service would either block the runtime or spin up a separate thread to do the work. We might as well force the async client to spin up that thread (like we do now with secret service), so the client has control.

connor4312 added a commit to microsoft/vscode that referenced this issue Jun 14, 2023
tunnel: fix keyring panic on Linxu

Fixes #184792

Reported on hwchen/keyring-rs#132
@connor4312
Copy link
Author

Yea, that's understandable. It's just unfortunate that this is a hidden problem that only pops up at runtime on Linux. Maybe something that could be done in secret-service, if using tokio as a runtime, is calling Handle::try_current() and then automatically running on another thread if that is Ok.

@brotskydotcom
Copy link
Collaborator

Feel free to file a secret-service issue and PR if you think you have an idea about how this could be done safely.

@russellbanks
Copy link
Contributor

russellbanks commented Jul 30, 2024

@brotskydotcom Could you possibly add this to the ReadMe? I was caught out with this on Linux now as since v3 I've specified keyring to use the same runtime as my application uses. Before v3, I just used the default feature set which used async-io.

@brotskydotcom
Copy link
Collaborator

Yes, I will add this info to the ReadMe, together with advice to consider using dbus-secret-service to avoid the entire problem.

@brotskydotcom brotskydotcom reopened this Jul 30, 2024
@russellbanks
Copy link
Contributor

Yes, I will add this info to the ReadMe, together with advice to consider using dbus-secret-service to avoid the entire problem.

Using the sync version was actually my solution in the end anyway.

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