handle server crashing while holding data_store lock.

This commit is contained in:
CodeSteak 2019-05-13 23:09:50 +02:00
parent 7ced269906
commit adfafe1fc6

View File

@ -25,24 +25,38 @@ impl ProtocolHandler for ServerState {
let q = Query::parse(query) let q = Query::parse(query)
.map_err(|e| ProtocolError::BadRequest(format!("Invalid Query String: {:?}", e)))?; .map_err(|e| ProtocolError::BadRequest(format!("Invalid Query String: {:?}", e)))?;
let data_store = self.data_store.read().unwrap(); let data_store = self
.data_store
.read()
.unwrap_or_else(|_| handle_broken_rwlock());
let mut textures = data_store.borrow_textures(); let mut textures = data_store.borrow_textures();
Ok(q.search(&mut textures)) Ok(q.search(&mut textures))
} }
fn get_texture_by_id(&mut self, id: &str) -> ProtocolResult<Option<Texture>> { fn get_texture_by_id(&mut self, id: &str) -> ProtocolResult<Option<Texture>> {
let data_store = self.data_store.read().unwrap(); let data_store = self
.data_store
.read()
.unwrap_or_else(|_| handle_broken_rwlock());
Ok(data_store.texture_by_id(id)) Ok(data_store.texture_by_id(id))
} }
fn get_texture_by_name(&mut self, name: &str) -> ProtocolResult<Option<Texture>> { fn get_texture_by_name(&mut self, name: &str) -> ProtocolResult<Option<Texture>> {
let data_store = self.data_store.read().unwrap(); let data_store = self
.data_store
.read()
.unwrap_or_else(|_| handle_broken_rwlock());
Ok(data_store.texture_by_name(name)) Ok(data_store.texture_by_name(name))
} }
fn get_texture_file(&mut self, hash: Sha256) -> ProtocolResult<Vec<u8>> { fn get_texture_file(&mut self, hash: Sha256) -> ProtocolResult<Vec<u8>> {
let data_store = self.data_store.read().unwrap(); let data_store = self
.data_store
.read()
.unwrap_or_else(|_| handle_broken_rwlock());
let data = data_store.read_texture_file_by_hash(&hash)?; let data = data_store.read_texture_file_by_hash(&hash)?;
@ -54,7 +68,10 @@ impl ProtocolHandler for ServerState {
hash: Sha256, hash: Sha256,
format: TextureFormat, format: TextureFormat,
) -> ProtocolResult<Vec<u8>> { ) -> ProtocolResult<Vec<u8>> {
let mut data_store = self.data_store.write().unwrap(); let mut data_store = self
.data_store
.write()
.unwrap_or_else(|_| handle_broken_rwlock());
let preview = data_store.get_texture_preview(&hash, format)?; let preview = data_store.get_texture_preview(&hash, format)?;
@ -67,7 +84,10 @@ impl ProtocolHandler for ServerState {
insert: Option<Texture>, insert: Option<Texture>,
insert_texture_data: Option<Vec<u8>>, insert_texture_data: Option<Vec<u8>>,
) -> ProtocolResult<ReplaceTextureStatus> { ) -> ProtocolResult<ReplaceTextureStatus> {
let mut data_store = self.data_store.write().unwrap(); let mut data_store = self
.data_store
.write()
.unwrap_or_else(|_| handle_broken_rwlock());
match insert_texture_data { match insert_texture_data {
Some(data) => { Some(data) => {
@ -133,3 +153,10 @@ impl ProtocolHandler for ServerState {
} }
} }
} }
fn handle_broken_rwlock() -> ! {
eprintln!("Panic while data_store lock was acquired.");
eprintln!("Representation in memory could be invalid.");
eprintln!("Can't recover. Please restart.");
std::process::exit(13);
}