Skip to content

Commit f3ce3e1

Browse files
Jonathan Woollett-LightJonathanWoollett-Light
authored andcommitted
refactor: Improving code debugability
Some code has been refactored to make debugging easier. Signed-off-by: Jonathan Woollett-Light <[email protected]>
1 parent 5aa8941 commit f3ce3e1

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

src/firecracker/src/api_server_adapter.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,33 +149,39 @@ pub(crate) fn run_with_api(
149149
let api_thread = thread::Builder::new()
150150
.name("fc_api".to_owned())
151151
.spawn(move || {
152-
match ApiServer::new(to_vmm, from_vmm, to_vmm_event_fd).bind_and_run(
152+
let res = ApiServer::new(to_vmm, from_vmm, to_vmm_event_fd).bind_and_run(
153153
&api_bind_path,
154154
process_time_reporter,
155155
&api_seccomp_filter,
156156
api_payload_limit,
157157
socket_ready_sender,
158-
) {
159-
Ok(_) => (),
160-
Err(api_server::Error::ServerCreation(ServerError::IOError(inner)))
158+
);
159+
160+
let Err(err) = res else {
161+
return;
162+
};
163+
164+
match err {
165+
api_server::Error::ServerCreation(ServerError::IOError(inner))
161166
if inner.kind() == std::io::ErrorKind::AddrInUse =>
162167
{
163168
let sock_path = api_bind_path.display().to_string();
164169
error!(
165170
"Failed to open the API socket at: {sock_path}. Check that it is not \
166171
already used."
167172
);
168-
std::process::exit(vmm::FcExitCode::GenericError as i32);
169173
}
170-
Err(api_server::Error::ServerCreation(err)) => {
174+
api_server::Error::ServerCreation(err) => {
171175
error!("Failed to bind and run the HTTP server: {err}");
172-
std::process::exit(vmm::FcExitCode::GenericError as i32);
173176
}
174177
}
178+
179+
std::process::exit(vmm::FcExitCode::GenericError as i32);
175180
})
176181
.expect("API thread spawn failed.");
177182

178183
let mut event_manager = EventManager::new().expect("Unable to create EventManager");
184+
179185
// Create the firecracker metrics object responsible for periodically printing metrics.
180186
let firecracker_metrics = Arc::new(Mutex::new(super::metrics::PeriodicMetrics::new()));
181187
event_manager.add_subscriber(firecracker_metrics.clone());
@@ -199,11 +205,13 @@ pub(crate) fn run_with_api(
199205
let req = from_api
200206
.recv()
201207
.expect("The channel's sending half was disconnected. Cannot receive data.");
208+
202209
// Also consume the API event along with the message. It is safe to unwrap()
203210
// because this event_fd is blocking.
204211
api_event_fd
205212
.read()
206213
.expect("VMM: Failed to read the API event_fd");
214+
207215
*req
208216
},
209217
|response| {

src/firecracker/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ fn main_exitable() -> FcExitCode {
383383

384384
let process_time_reporter =
385385
ProcessTimeReporter::new(start_time_us, start_time_cpu_us, parent_cpu_time_us);
386+
386387
api_server_adapter::run_with_api(
387388
&mut seccomp_filters,
388389
vmm_config_json,

src/vmm/src/rpc_interface.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,12 +380,18 @@ impl<'a> PrebootApiController<'a> {
380380
&mut vm_resources,
381381
event_manager,
382382
);
383+
383384
// Configure and start microVM through successive API calls.
384385
// Iterate through API calls to configure microVm.
385386
// The loop breaks when a microVM is successfully started, and a running Vmm is built.
386387
while preboot_controller.built_vmm.is_none() {
387-
// Get request, process it, send back the response.
388-
respond(preboot_controller.handle_preboot_request(recv_req()));
388+
// Get request
389+
let req = recv_req();
390+
// Process the request.
391+
let res = preboot_controller.handle_preboot_request(req);
392+
// Send back the response.
393+
respond(res);
394+
389395
// If any fatal errors were encountered, break the loop.
390396
if let Some(exit_code) = preboot_controller.fatal_error {
391397
return Err(exit_code);

0 commit comments

Comments
 (0)