From 08c61f48ad7ea3ad20bff9deb64fd211e7f93857 Mon Sep 17 00:00:00 2001 From: Thomas Praxl Date: Mon, 21 Mar 2022 10:39:57 +0100 Subject: [PATCH] fix: hanging server (#806) Since the update to actix-rt@2.3 (f413996a), the web server hasn't been started, when we executed `cargo run -p rust-code-analysis-web`. It froze once `actix_rt::System::new().run()?;` was called, effectively preventing the execution of the code below. By looking at the actix-rt examples and changing the code to be more like the examples, I was able to fix this. The webserver now behaves as expected. --- rust-code-analysis-web/src/main.rs | 5 +- rust-code-analysis-web/src/web/server.rs | 121 +++++++++++------------ 2 files changed, 61 insertions(+), 65 deletions(-) diff --git a/rust-code-analysis-web/src/main.rs b/rust-code-analysis-web/src/main.rs index 56c1ac4..f6d06a0 100644 --- a/rust-code-analysis-web/src/main.rs +++ b/rust-code-analysis-web/src/main.rs @@ -9,7 +9,8 @@ use clap::{crate_version, App, Arg}; use web::server; -fn main() { +#[actix_web::main] +async fn main() { let matches = App::new("rust-code-analysis-web") .version(crate_version!()) .author(&*env!("CARGO_PKG_AUTHORS").replace(':', "\n")) @@ -51,7 +52,7 @@ fn main() { eprintln!("Invalid port number"); return; }; - if let Err(e) = server::run(host.to_string(), port, num_jobs) { + if let Err(e) = server::run(host.to_string(), port, num_jobs).await { eprintln!("Cannot run the server at {}:{}: {}", host, port, e); } } diff --git a/rust-code-analysis-web/src/web/server.rs b/rust-code-analysis-web/src/web/server.rs index a761b6a..3abf6f7 100644 --- a/rust-code-analysis-web/src/web/server.rs +++ b/rust-code-analysis-web/src/web/server.rs @@ -1,4 +1,3 @@ -use actix_rt::Runtime; use actix_web::{ dev::Body, guard, http, @@ -220,71 +219,67 @@ fn ping() -> HttpResponse { HttpResponse::Ok().body(Body::Empty) } -pub fn run(host: String, port: u16, n_threads: usize) -> std::io::Result<()> { - actix_rt::System::new().run()?; - let rt = Runtime::new()?; +pub async fn run(host: String, port: u16, n_threads: usize) -> std::io::Result<()> { let max_size = 1024 * 1024 * 4; - rt.block_on(async move { - HttpServer::new(move || { - App::new() - .service( - web::resource("/ast") - .guard(guard::Header("content-type", "application/json")) - .app_data(web::Json::::configure(|cfg| { - cfg.limit(max_size) - })) - .route(web::post().to(ast_parser)), - ) - .service( - web::resource("/comment") - .guard(guard::Header("content-type", "application/json")) - .app_data(web::Json::::configure(|cfg| { - cfg.limit(max_size) - })) - .route(web::post().to(comment_removal_json)), - ) - .service( - web::resource("/comment") - .guard(guard::Header("content-type", "application/octet-stream")) - .data(web::PayloadConfig::default().limit(max_size)) - .route(web::post().to(comment_removal_plain)), - ) - .service( - web::resource("/metrics") - .guard(guard::Header("content-type", "application/json")) - .app_data(web::Json::::configure(|cfg| { - cfg.limit(max_size) - })) - .route(web::post().to(metrics_json)), - ) - .service( - web::resource("/metrics") - .guard(guard::Header("content-type", "application/octet-stream")) - .data(web::PayloadConfig::default().limit(max_size)) - .route(web::post().to(metrics_plain)), - ) - .service( - web::resource("/function") - .guard(guard::Header("content-type", "application/json")) - .app_data(web::Json::::configure(|cfg| { - cfg.limit(max_size) - })) - .route(web::post().to(function_json)), - ) - .service( - web::resource("/function") - .guard(guard::Header("content-type", "application/octet-stream")) - .data(web::PayloadConfig::default().limit(max_size)) - .route(web::post().to(function_plain)), - ) - .service(web::resource("/ping").route(web::get().to(ping))) - }) - .workers(n_threads) - .bind((host.as_str(), port))? - .run() - .await + HttpServer::new(move || { + App::new() + .service( + web::resource("/ast") + .guard(guard::Header("content-type", "application/json")) + .app_data(web::Json::::configure(|cfg| { + cfg.limit(max_size) + })) + .route(web::post().to(ast_parser)), + ) + .service( + web::resource("/comment") + .guard(guard::Header("content-type", "application/json")) + .app_data(web::Json::::configure(|cfg| { + cfg.limit(max_size) + })) + .route(web::post().to(comment_removal_json)), + ) + .service( + web::resource("/comment") + .guard(guard::Header("content-type", "application/octet-stream")) + .data(web::PayloadConfig::default().limit(max_size)) + .route(web::post().to(comment_removal_plain)), + ) + .service( + web::resource("/metrics") + .guard(guard::Header("content-type", "application/json")) + .app_data(web::Json::::configure(|cfg| { + cfg.limit(max_size) + })) + .route(web::post().to(metrics_json)), + ) + .service( + web::resource("/metrics") + .guard(guard::Header("content-type", "application/octet-stream")) + .data(web::PayloadConfig::default().limit(max_size)) + .route(web::post().to(metrics_plain)), + ) + .service( + web::resource("/function") + .guard(guard::Header("content-type", "application/json")) + .app_data(web::Json::::configure(|cfg| { + cfg.limit(max_size) + })) + .route(web::post().to(function_json)), + ) + .service( + web::resource("/function") + .guard(guard::Header("content-type", "application/octet-stream")) + .data(web::PayloadConfig::default().limit(max_size)) + .route(web::post().to(function_plain)), + ) + .service(web::resource("/ping").route(web::get().to(ping))) }) + .workers(n_threads) + .bind((host.as_str(), port))? + .run() + .await } // curl --header "Content-Type: application/json" --request POST --data '{"id": "1234", "file_name": "prova.cpp", "code": "int x = 1;", "comment": true, "span": true}' http://127.0.0.1:8081/ast