Repository: TabbyML/tabby
File: crates/llama-cpp-server/src/supervisor.rs (Rust)

Code before:
                if embedding {
                    command
                        .arg("--embedding")
                        .arg("--ubatch-size")
                        .arg(var("LLAMA_CPP_EMBEDDING_N_UBATCH_SIZE").unwrap_or("4096".into()));
                }

                if let Some(chat_template) = chat_template.as_ref() {
                    command.arg("--chat-template").arg(chat_template);
                }

                if enable_fast_attention {
                    command.arg("-fa");
                };

                let command_args = format!("{:?}", command);

                let mut process = command.spawn().unwrap_or_else(|e| {
                    panic!(
                        "Failed to start llama-server <{}> with command {:?}: {}",
                        name, command, e
                    )
                });

                let mut stderr =
                    BufReader::new(process.stderr.take().expect("Failed to get stderr")).lines();
                let mut error_lines = VecDeque::with_capacity(100);

                let wait_handle = process.wait();

                while let Ok(Some(line)) = stderr.next_line().await {
                    if !line.contains("GET /health") {
                        if error_lines.len() >= 100 {
                            error_lines.pop_front();
                        }
                        error_lines.push_back(line);
                    }
                }

                let status_code = wait_handle.await.ok().and_then(|s| s.code()).unwrap_or(-1);

                if status_code != 0 {
                    warn!(
                        "llama-server <{}> exited with status code {}, args: `{}`",
                        name, status_code, command_args
                    );

                    let error_message = error_lines.into_iter().fold(
                        String::from("Recent llama-cpp errors:\n"),
                        |mut acc, line| {
                            acc.push_str(&format!("{}\n", line));

Review comment (bug):
`expect` would panic and exit, it would be better to let users know that it's failed to get the llama.cpp stderr

```suggestion
                    BufReader::new(process.stderr.take().expect("Failed to get llama.cpp stderr")).lines();
```

Code after:

                if embedding {
                    command
                        .arg("--embedding")
                        .arg("--ubatch-size")
                        .arg(var("LLAMA_CPP_EMBEDDING_N_UBATCH_SIZE").unwrap_or("4096".into()));
                }

                if let Some(chat_template) = chat_template.as_ref() {
                    command.arg("--chat-template").arg(chat_template);
                }

                if enable_fast_attention {
                    command.arg("-fa");
                };

                let command_args = format!("{:?}", command);

                let mut process = command.spawn().unwrap_or_else(|e| {
                    panic!(
                        "Failed to start llama-server <{}> with command {:?}: {}",
                        name, command, e
                    )
                });

                let mut stderr = BufReader::new(
                    process
                        .stderr
                        .take()
                        .expect("Failed to get llama.cpp stderr"),
                )
                .lines();
                let mut error_lines = VecDeque::with_capacity(100);

                let wait_handle = process.wait();

                while let Ok(Some(line)) = stderr.next_line().await {
                    if !line.contains("GET /health") {
                        if error_lines.len() >= 100 {
                            error_lines.pop_front();
                        }
                        error_lines.push_back(line);
                    }
                }

                let status_code = wait_handle.await.ok().and_then(|s| s.code()).unwrap_or(-1);

                if status_code != 0 {
                    warn!(
                        "llama-server <{}> exited with status code {}, args: `{}`",
                        name, status_code, command_args