Skip to content

Commit ed44e2a

Browse files
berciuliviuacatangiu
authored andcommitted
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 <[email protected]> Signed-off-by: YUAN LYU <[email protected]>
1 parent 67fd749 commit ed44e2a

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/server.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use std::collections::HashMap;
4+
use logger::error;
55
use std::io::{Read, Write};
66
use std::os::unix::io::AsRawFd;
77
use std::os::unix::io::RawFd;
@@ -49,9 +49,9 @@ impl ServerRequest {
4949
/// The response is then wrapped in a `ServerResponse`.
5050
///
5151
/// Returns a `ServerResponse` ready for yielding to the server
52-
pub fn process<F>(&self, callable: F) -> ServerResponse
52+
pub fn process<F>(&self, mut callable: F) -> ServerResponse
5353
where
54-
F: Fn(&Request) -> Response,
54+
F: FnMut(&Request) -> Response,
5555
{
5656
let http_response = callable(self.inner());
5757
ServerResponse::new(http_response, self.id)
@@ -388,6 +388,27 @@ impl HttpServer {
388388
Ok(parsed_requests)
389389
}
390390

391+
/// This function is responsible with flushing any remaining outgoing
392+
/// requests on the server.
393+
///
394+
/// Note that this function can block the thread on write, since the
395+
/// operation is blocking.
396+
pub fn flush_outgoing_writes(&mut self) {
397+
for (_, connection) in self.connections.iter_mut() {
398+
while connection.state == ClientConnectionState::AwaitingOutgoing {
399+
if let Err(e) = connection.write() {
400+
if let ServerError::ConnectionError(ConnectionError::InvalidWrite) = e {
401+
// Nothing is logged since an InvalidWrite means we have successfully
402+
// flushed the connection
403+
} else {
404+
error!("Connection write error: {}", e);
405+
}
406+
break;
407+
}
408+
}
409+
}
410+
}
411+
391412
/// The file descriptor of the `epoll` structure can enable the server to become
392413
/// a non-blocking structure in an application.
393414
///

0 commit comments

Comments
 (0)