Skip to content

Commit 08c61f4

Browse files
authored
fix: hanging server (#806)
Since the update to actix-rt@2.3 (f413996), 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.
1 parent 97e576b commit 08c61f4

File tree

2 files changed

+61
-65
lines changed

2 files changed

+61
-65
lines changed

rust-code-analysis-web/src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use clap::{crate_version, App, Arg};
99

1010
use web::server;
1111

12-
fn main() {
12+
#[actix_web::main]
13+
async fn main() {
1314
let matches = App::new("rust-code-analysis-web")
1415
.version(crate_version!())
1516
.author(&*env!("CARGO_PKG_AUTHORS").replace(':', "\n"))
@@ -51,7 +52,7 @@ fn main() {
5152
eprintln!("Invalid port number");
5253
return;
5354
};
54-
if let Err(e) = server::run(host.to_string(), port, num_jobs) {
55+
if let Err(e) = server::run(host.to_string(), port, num_jobs).await {
5556
eprintln!("Cannot run the server at {}:{}: {}", host, port, e);
5657
}
5758
}

rust-code-analysis-web/src/web/server.rs

Lines changed: 58 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use actix_rt::Runtime;
21
use actix_web::{
32
dev::Body,
43
guard, http,
@@ -220,71 +219,67 @@ fn ping() -> HttpResponse {
220219
HttpResponse::Ok().body(Body::Empty)
221220
}
222221

223-
pub fn run(host: String, port: u16, n_threads: usize) -> std::io::Result<()> {
224-
actix_rt::System::new().run()?;
225-
let rt = Runtime::new()?;
222+
pub async fn run(host: String, port: u16, n_threads: usize) -> std::io::Result<()> {
226223
let max_size = 1024 * 1024 * 4;
227224

228-
rt.block_on(async move {
229-
HttpServer::new(move || {
230-
App::new()
231-
.service(
232-
web::resource("/ast")
233-
.guard(guard::Header("content-type", "application/json"))
234-
.app_data(web::Json::<AstPayload>::configure(|cfg| {
235-
cfg.limit(max_size)
236-
}))
237-
.route(web::post().to(ast_parser)),
238-
)
239-
.service(
240-
web::resource("/comment")
241-
.guard(guard::Header("content-type", "application/json"))
242-
.app_data(web::Json::<WebCommentPayload>::configure(|cfg| {
243-
cfg.limit(max_size)
244-
}))
245-
.route(web::post().to(comment_removal_json)),
246-
)
247-
.service(
248-
web::resource("/comment")
249-
.guard(guard::Header("content-type", "application/octet-stream"))
250-
.data(web::PayloadConfig::default().limit(max_size))
251-
.route(web::post().to(comment_removal_plain)),
252-
)
253-
.service(
254-
web::resource("/metrics")
255-
.guard(guard::Header("content-type", "application/json"))
256-
.app_data(web::Json::<WebMetricsPayload>::configure(|cfg| {
257-
cfg.limit(max_size)
258-
}))
259-
.route(web::post().to(metrics_json)),
260-
)
261-
.service(
262-
web::resource("/metrics")
263-
.guard(guard::Header("content-type", "application/octet-stream"))
264-
.data(web::PayloadConfig::default().limit(max_size))
265-
.route(web::post().to(metrics_plain)),
266-
)
267-
.service(
268-
web::resource("/function")
269-
.guard(guard::Header("content-type", "application/json"))
270-
.app_data(web::Json::<WebFunctionPayload>::configure(|cfg| {
271-
cfg.limit(max_size)
272-
}))
273-
.route(web::post().to(function_json)),
274-
)
275-
.service(
276-
web::resource("/function")
277-
.guard(guard::Header("content-type", "application/octet-stream"))
278-
.data(web::PayloadConfig::default().limit(max_size))
279-
.route(web::post().to(function_plain)),
280-
)
281-
.service(web::resource("/ping").route(web::get().to(ping)))
282-
})
283-
.workers(n_threads)
284-
.bind((host.as_str(), port))?
285-
.run()
286-
.await
225+
HttpServer::new(move || {
226+
App::new()
227+
.service(
228+
web::resource("/ast")
229+
.guard(guard::Header("content-type", "application/json"))
230+
.app_data(web::Json::<AstPayload>::configure(|cfg| {
231+
cfg.limit(max_size)
232+
}))
233+
.route(web::post().to(ast_parser)),
234+
)
235+
.service(
236+
web::resource("/comment")
237+
.guard(guard::Header("content-type", "application/json"))
238+
.app_data(web::Json::<WebCommentPayload>::configure(|cfg| {
239+
cfg.limit(max_size)
240+
}))
241+
.route(web::post().to(comment_removal_json)),
242+
)
243+
.service(
244+
web::resource("/comment")
245+
.guard(guard::Header("content-type", "application/octet-stream"))
246+
.data(web::PayloadConfig::default().limit(max_size))
247+
.route(web::post().to(comment_removal_plain)),
248+
)
249+
.service(
250+
web::resource("/metrics")
251+
.guard(guard::Header("content-type", "application/json"))
252+
.app_data(web::Json::<WebMetricsPayload>::configure(|cfg| {
253+
cfg.limit(max_size)
254+
}))
255+
.route(web::post().to(metrics_json)),
256+
)
257+
.service(
258+
web::resource("/metrics")
259+
.guard(guard::Header("content-type", "application/octet-stream"))
260+
.data(web::PayloadConfig::default().limit(max_size))
261+
.route(web::post().to(metrics_plain)),
262+
)
263+
.service(
264+
web::resource("/function")
265+
.guard(guard::Header("content-type", "application/json"))
266+
.app_data(web::Json::<WebFunctionPayload>::configure(|cfg| {
267+
cfg.limit(max_size)
268+
}))
269+
.route(web::post().to(function_json)),
270+
)
271+
.service(
272+
web::resource("/function")
273+
.guard(guard::Header("content-type", "application/octet-stream"))
274+
.data(web::PayloadConfig::default().limit(max_size))
275+
.route(web::post().to(function_plain)),
276+
)
277+
.service(web::resource("/ping").route(web::get().to(ping)))
287278
})
279+
.workers(n_threads)
280+
.bind((host.as_str(), port))?
281+
.run()
282+
.await
288283
}
289284

290285
// 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

0 commit comments

Comments
 (0)