From ed44e2af6f913f947887770c4acea048744c1c9b Mon Sep 17 00:00:00 2001 From: berciuliviu Date: Wed, 18 Nov 2020 16:21:22 +0000 Subject: [PATCH] Add error handling for fatal errors This commit adds a mechanism that considers some errors as 'fatal'. It alerts the user and then terminates the Firecracker process. Signed-off-by: berciuliviu Signed-off-by: YUAN LYU --- src/server.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/server.rs b/src/server.rs index 669b1c6..39659cb 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,7 +1,7 @@ // Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -use std::collections::HashMap; +use logger::error; use std::io::{Read, Write}; use std::os::unix::io::AsRawFd; use std::os::unix::io::RawFd; @@ -49,9 +49,9 @@ impl ServerRequest { /// The response is then wrapped in a `ServerResponse`. /// /// Returns a `ServerResponse` ready for yielding to the server - pub fn process(&self, callable: F) -> ServerResponse + pub fn process(&self, mut callable: F) -> ServerResponse where - F: Fn(&Request) -> Response, + F: FnMut(&Request) -> Response, { let http_response = callable(self.inner()); ServerResponse::new(http_response, self.id) @@ -388,6 +388,27 @@ impl HttpServer { Ok(parsed_requests) } + /// This function is responsible with flushing any remaining outgoing + /// requests on the server. + /// + /// Note that this function can block the thread on write, since the + /// operation is blocking. + pub fn flush_outgoing_writes(&mut self) { + for (_, connection) in self.connections.iter_mut() { + while connection.state == ClientConnectionState::AwaitingOutgoing { + if let Err(e) = connection.write() { + if let ServerError::ConnectionError(ConnectionError::InvalidWrite) = e { + // Nothing is logged since an InvalidWrite means we have successfully + // flushed the connection + } else { + error!("Connection write error: {}", e); + } + break; + } + } + } + } + /// The file descriptor of the `epoll` structure can enable the server to become /// a non-blocking structure in an application. ///