Skip to content

Commit

Permalink
Avoid unwrap when generating authkey (#2804)
Browse files Browse the repository at this point in the history
Fixes: #2690

### What
In some situations the authkey generation fails. Change the
implementation to avoid unwrap and fallback on a rand if all else fails.

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested [demo.rerun.io](https://demo.rerun.io/pr/2804) (if
applicable)

- [PR Build Summary](https://build.rerun.io/pr/2804)
- [Docs
preview](https://rerun.io/preview/pr%3Ajleibs%2Fdont_unwrap_authkey/docs)
- [Examples
preview](https://rerun.io/preview/pr%3Ajleibs%2Fdont_unwrap_authkey/examples)
  • Loading branch information
jleibs authored Jul 25, 2023
1 parent be043df commit 4e62fa7
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions rerun_py/src/python_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,15 @@ fn default_store_id(py: Python<'_>, variant: StoreKind, application_id: &str) ->
// TODO(emilk): are there any security concerns with leaking authkey?
//
// https://docs.python.org/3/library/multiprocessing.html#multiprocessing.Process.authkey
let seed = authkey(py);
let seed = match authkey(py) {
Ok(seed) => seed,
Err(err) => {
re_log::error_once!("Failed to retrieve python authkey: {err}\nMultiprocessing will result in split recordings.");
// If authkey failed, just generate a random 8-byte authkey
let bytes = rand::Rng::gen::<[u8; 8]>(&mut rand::thread_rng());
bytes.to_vec()
}
};
let salt: u64 = 0xab12_cd34_ef56_0178;

let mut hasher = std::collections::hash_map::DefaultHasher::default();
Expand All @@ -1349,8 +1357,9 @@ fn default_store_id(py: Python<'_>, variant: StoreKind, application_id: &str) ->
StoreId::from_uuid(variant, uuid)
}

fn authkey(py: Python<'_>) -> Vec<u8> {
fn authkey(py: Python<'_>) -> PyResult<Vec<u8>> {
let locals = PyDict::new(py);

py.run(
r#"
import multiprocessing
Expand All @@ -1360,10 +1369,17 @@ authkey = multiprocessing.current_process().authkey
None,
Some(locals),
)
.unwrap();
let authkey = locals.get_item("authkey").unwrap();
let authkey: &PyBytes = authkey.downcast().unwrap();
authkey.as_bytes().to_vec()
.and_then(|()| {
locals
.get_item("authkey")
.ok_or_else(|| PyRuntimeError::new_err("authkey missing from expected locals"))
})
.and_then(|authkey| {
authkey
.downcast()
.map_err(|err| PyRuntimeError::new_err(err.to_string()))
})
.map(|authkey: &PyBytes| authkey.as_bytes().to_vec())
}

fn convert_color(color: Vec<u8>) -> PyResult<[u8; 4]> {
Expand Down

0 comments on commit 4e62fa7

Please sign in to comment.