From 657ca8c1d49208870bce4dfc53e68e2111ea4d57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhun=20Parmaks=C4=B1z?= Date: Mon, 16 May 2022 12:34:57 +0300 Subject: [PATCH] fix(server): do not hold the RwLock guard before async calls --- src/paste.rs | 10 ++++++++-- src/server.rs | 10 +++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/paste.rs b/src/paste.rs index 3a897f5..206ab84 100644 --- a/src/paste.rs +++ b/src/paste.rs @@ -9,6 +9,7 @@ use std::fs::{self, File}; use std::io::{Error as IoError, ErrorKind as IoErrorKind, Result as IoResult, Write}; use std::path::{Path, PathBuf}; use std::str; +use std::sync::RwLock; use url::Url; /// Type of the data to store. @@ -157,7 +158,7 @@ impl Paste { &mut self, expiry_date: Option, client: &Client, - config: Config, + config: &RwLock, ) -> Result { let data = str::from_utf8(&self.data).map_err(error::ErrorBadRequest)?; let url = Url::parse(data).map_err(error::ErrorBadRequest)?; @@ -172,6 +173,8 @@ impl Paste { .await .map_err(error::ErrorInternalServerError)?; let payload_limit = config + .read() + .map_err(|_| error::ErrorInternalServerError("cannot acquire config"))? .server .max_content_length .get_bytes() @@ -183,6 +186,9 @@ impl Paste { .await .map_err(error::ErrorInternalServerError)? .to_vec(); + let config = config + .read() + .map_err(|_| error::ErrorInternalServerError("cannot acquire config"))?; let bytes_checksum = util::sha256_digest(&*bytes)?; self.data = bytes; if !config.paste.duplicate_files.unwrap_or(true) && expiry_date.is_none() { @@ -330,7 +336,7 @@ mod tests { }; let client_data = Data::new(Client::default()); let file_name = paste - .store_remote_file(None, &client_data, config.clone()) + .store_remote_file(None, &client_data, &RwLock::new(config.clone())) .await?; let file_path = PasteType::RemoteFile .get_path(&config.server.upload_path) diff --git a/src/server.rs b/src/server.rs index 138deb1..71119f5 100644 --- a/src/server.rs +++ b/src/server.rs @@ -165,13 +165,9 @@ async fn upload( paste.store_file(content.get_file_name()?, expiry_date, &config)? } PasteType::RemoteFile => { - { - let config = config.read().map_err(|_| { - error::ErrorInternalServerError("cannot acquire config") - })?; - paste.store_remote_file(expiry_date, &client, config.clone()) - } - .await? + paste + .store_remote_file(expiry_date, &client, &config) + .await? } PasteType::Url => { let config = config