Skip to content

Commit

Permalink
api_server: add kill switch capability
Browse files Browse the repository at this point in the history
Added shutdown event-fd acting as a kill switch.
Used to signal the api server thread that it needs to shut down.

Signed-off-by: Egor Lazarchuk <[email protected]>
Co-authored-by: acatangiu <[email protected]>
  • Loading branch information
2 people authored and ShadowCurse committed Sep 18, 2023
1 parent 31a1687 commit 272ae95
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion src/api_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ serde_json = "1.0.78"
thiserror = "1.0.32"

logger = { path = "../logger" }
micro_http = { git = "https://github.com/firecracker-microvm/micro-http", rev = "4b18a04" }
micro_http = { git = "https://github.com/firecracker-microvm/micro-http" }
mmds = { path = "../mmds" }
seccompiler = { path = "../seccompiler" }
utils = { path = "../utils" }
Expand Down
5 changes: 5 additions & 0 deletions src/api_server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ impl ApiServer {
loop {
let request_vec = match server.requests() {
Ok(vec) => vec,
Err(ServerError::ShutdownEvent) => {
server.flush_outgoing_writes();
debug!("shutdown request received, API server thread ending.");
return;
}
Err(err) => {
// print request error, but keep server running
error!("API Server error on retrieving incoming request: {}", err);
Expand Down
2 changes: 1 addition & 1 deletion src/dumbo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ bitflags = "1.3.2"
derive_more = { version = "0.99.17", default-features = false, features = ["from"] }

logger = { path = "../logger" }
micro_http = { git = "https://github.com/firecracker-microvm/micro-http", rev = "4b18a04" }
micro_http = { git = "https://github.com/firecracker-microvm/micro-http" }
utils = { path = "../utils" }

[dev-dependencies]
Expand Down
12 changes: 11 additions & 1 deletion src/firecracker/src/api_server_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ pub(crate) fn run_with_api(
// It is used in the config/pre-boot loop which is a simple blocking loop
// which only consumes API events.
let api_event_fd = EventFd::new(libc::EFD_SEMAPHORE).expect("Cannot create API Eventfd.");
// FD used to signal API thread to stop/shutdown.
let api_kill_switch = EventFd::new(libc::EFD_NONBLOCK).expect("Cannot create API kill switch.");

// Channels for both directions between Vmm and Api threads.
let (to_vmm, from_api) = channel();
Expand All @@ -159,7 +161,7 @@ pub(crate) fn run_with_api(
.remove("api")
.expect("Missing seccomp filter for API thread.");

let server = match HttpServer::new(&bind_path) {
let mut server = match HttpServer::new(&bind_path) {
Ok(s) => s,
Err(ServerError::IOError(inner)) if inner.kind() == std::io::ErrorKind::AddrInUse => {
let sock_path = bind_path.display().to_string();
Expand All @@ -170,6 +172,14 @@ pub(crate) fn run_with_api(
}
};

let api_kill_switch_clone = api_kill_switch
.try_clone()
.expect("Failed to clone API kill switch");

server
.add_kill_switch(api_kill_switch_clone)
.expect("Cannot add HTTP server kill switch");

// Start the separate API thread.
let api_thread = thread::Builder::new()
.name("fc_api".to_owned())
Expand Down
4 changes: 2 additions & 2 deletions src/mmds/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ versionize_derive = "0.1.5"

dumbo = { path = "../dumbo" }
logger = { path = "../logger" }
micro_http = { git = "https://github.com/firecracker-microvm/micro-http", rev = "4b18a04" }
micro_http = { git = "https://github.com/firecracker-microvm/micro-http" }
snapshot = { path = "../snapshot" }
utils = { path = "../utils" }
utils = { path = "../utils" }

0 comments on commit 272ae95

Please sign in to comment.