Skip to content

Commit

Permalink
fix(cli): random segfault with lagon dev (#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuiiBz authored Dec 24, 2022
1 parent 4b59eff commit 7e0738a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-tomatoes-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lagon/cli': patch
---

Fix random segfault with `lagon dev`
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions packages/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ colored = "2.0.0"
dirs = "4.0.0"
webbrowser = "0.8.2"
tokio = { version = "1", features = ["full"] }
tokio-util = { version = "0.7.4", features = ["rt"] }
hyper = { version = "0.14", features = ["client", "server", "http1", "http2", "runtime", "stream"] }
mime = "0.3.16"
serde = { version = "1.0", features = ["derive"] }
Expand Down
24 changes: 18 additions & 6 deletions packages/cli/src/commands/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Mutex;
use tokio_util::task::LocalPoolHandle;

use crate::utils::{
bundle_function, info, input, success, validate_code_file, validate_public_dir, warn, Assets,
Expand Down Expand Up @@ -77,6 +78,7 @@ async fn handle_request(
ip: String,
content: Arc<Mutex<(Vec<u8>, Assets)>>,
environment_variables: HashMap<String, String>,
pool: LocalPoolHandle,
) -> Result<HyperResponse<Body>> {
let mut url = req.uri().to_string();

Expand Down Expand Up @@ -129,13 +131,20 @@ async fn handle_request(
Ok(mut request) => {
request.add_header("X-Forwarded-For".into(), ip);

let mut isolate = Isolate::new(
IsolateOptions::new(String::from_utf8(index)?)
.with_metadata(Some((String::from(""), String::from(""))))
.with_environment_variables(environment_variables),
pool.spawn_pinned_by_idx(
move || async move {
let mut isolate = Isolate::new(
IsolateOptions::new(
String::from_utf8(index).expect("Code is not UTF-8"),
)
.with_metadata(Some((String::from(""), String::from(""))))
.with_environment_variables(environment_variables),
);

isolate.run(request, tx).await;
},
0,
);

isolate.run(request, tx).await;
}
Err(error) => {
println!("Error while parsing request: {}", error);
Expand Down Expand Up @@ -236,10 +245,12 @@ pub async fn dev(

let server_content = content.clone();
let environment_variables = parse_environment_variables(env)?;
let pool = LocalPoolHandle::new(1);

let server = Server::bind(&addr).serve(make_service_fn(move |conn: &AddrStream| {
let content = server_content.clone();
let environment_variables = environment_variables.clone();
let pool = pool.clone();

let addr = conn.remote_addr();
let ip = addr.ip().to_string();
Expand All @@ -251,6 +262,7 @@ pub async fn dev(
ip.clone(),
content.clone(),
environment_variables.clone(),
pool.clone(),
)
}))
}
Expand Down

0 comments on commit 7e0738a

Please sign in to comment.