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

Code before:
                        }
                        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));
                            acc
                        },
                    );

                    match status_code {
                        0 => (),
                        1 | -1 => {
                            eprintln!("{}", error_message);
                            if let Some(solution) = analyze_error_message(&error_message) {
                                let solution_lines: Vec<_> = solution.split('\n').collect();
                                let msg = tabby_common::terminal::InfoMessage::new(
                                    "ERROR",
                                    tabby_common::terminal::HeaderFormat::BoldRed,
                                    &solution_lines,
                                );
                                msg.print();
                            }
                            eprintln!(
                                "llama-server <{}> encountered a fatal error. Exiting service. Please check the above logs and suggested solutions for details.",
                                name
                            );
                            std::process::exit(1);
                        }
                        _ => {
                            warn!(
                                "llama-server <{}> exited with status code {}, retrying...",
                                name, status_code
                            );
                        }
                    }
                    tokio::time::sleep(Duration::from_secs(1)).await;
                }
            }
        });

Review comment (suggestion):
Analyze the error message while printing it line by line. If a solution is detected, we can halt the analysis directly.

then we also don't have to split the solution into lines later

Code after:
                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
                    );

                    eprintln!(
                        "{}\n",
                        tabby_common::terminal::HeaderFormat::BoldRed
                            .format("Recent llama-cpp errors:")
                    );
                    match status_code {
                        0 => (),
                        1 | -1 => {
                            for line in error_lines {
                                eprintln!("{}", line);
                                if let Some(solution) = analyze_error_message(&line) {
                                    let solution_lines: Vec<_> = solution.split('\n').collect();
                                    let msg = tabby_common::terminal::InfoMessage::new(
                                        "ERROR",
                                        tabby_common::terminal::HeaderFormat::BoldRed,
                                        &solution_lines,
                                    );
                                    msg.print();
                                    break;
                                }
                            }
                            eprintln!(
                                "llama-server <{}> encountered a fatal error. Exiting service. Please check the above logs and suggested solutions for details.",
                                name
                            );
                            std::process::exit(1);
                        }
                        _ => {
                            if first_retry {
                                for line in &error_lines {
                                    eprintln!("{}", line);
                                }