Skip to content

Commit

Permalink
fix: Use parking_lot to avoid PoisenError when acquiring a RwLock<_>
Browse files Browse the repository at this point in the history
  • Loading branch information
threema-donat committed Apr 24, 2024
1 parent ce701eb commit e680ba6
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- fix: Avoid panics when acquiring a RwLock<_>

## v0.6.2

- Add support for Safari web push
Expand Down
120 changes: 113 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ base64 = "0.20"
tracing = { version = "0.1", optional = true }
pem = { version = "1.0", optional = true }
ring = { version = "0.16", features = ["std"], optional = true }
parking_lot = "0.12"

[dev-dependencies]
argparse = "0.2"
Expand Down
12 changes: 5 additions & 7 deletions src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use crate::error::Error;
use base64::encode;
use std::io::Read;
use std::sync::Arc;
use std::{
sync::RwLock,
time::{Duration, SystemTime, UNIX_EPOCH},
};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use parking_lot::RwLock;

#[cfg(feature = "openssl")]
use openssl::{
Expand Down Expand Up @@ -138,7 +136,7 @@ impl Signer {
self.renew()?;
}

let signature = self.signature.read().unwrap();
let signature = self.signature.read();

#[cfg(feature = "tracing")]
{
Expand Down Expand Up @@ -187,7 +185,7 @@ impl Signer {
);
}

let mut signature = self.signature.write().unwrap();
let mut signature = self.signature.write();

*signature = Signature {
key: Self::create_signature(&self.secret, &self.key_id, &self.team_id, issued_at)?,
Expand All @@ -198,7 +196,7 @@ impl Signer {
}

fn is_expired(&self) -> bool {
let sig = self.signature.read().unwrap();
let sig = self.signature.read();
let expiry = get_time() - sig.issued_at;
expiry >= self.expire_after_s.as_secs() as i64
}
Expand Down

0 comments on commit e680ba6

Please sign in to comment.