From 747d009bb4b88d54d53f6d2b417e02a85f41d9d1 Mon Sep 17 00:00:00 2001 From: Alexey Kondratov Date: Tue, 5 Jul 2022 12:27:53 +0200 Subject: [PATCH] Fix panic while waiting for Postgres readiness in the compute_ctl (#2021) We were reading Postgres pid file and looking for the 'ready' status, but it could be empty or we could not read it. So add all the checks. --- compute_tools/src/pg_helpers.rs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/compute_tools/src/pg_helpers.rs b/compute_tools/src/pg_helpers.rs index ea3909a029..207d09d76b 100644 --- a/compute_tools/src/pg_helpers.rs +++ b/compute_tools/src/pg_helpers.rs @@ -248,18 +248,20 @@ pub fn wait_for_postgres(pg: &mut Child, port: &str, pgdata: &Path) -> Result<() bail!("Postgres exited unexpectedly with code {}", code); } - if pid_path.exists() { - let file = BufReader::new(File::open(&pid_path)?); - let status = file - .lines() - .last() - .unwrap() - .unwrap_or_else(|_| "unknown".to_string()); - let can_connect = TcpStream::connect_timeout(&addr, timeout).is_ok(); + // Check that we can open pid file first. + if let Ok(file) = File::open(&pid_path) { + let file = BufReader::new(file); + let last_line = file.lines().last(); - // Now Postgres is ready to accept connections - if status.trim() == "ready" && can_connect { - break; + // Pid file could be there and we could read it, but it could be empty, for example. + if let Some(Ok(line)) = last_line { + let status = line.trim(); + let can_connect = TcpStream::connect_timeout(&addr, timeout).is_ok(); + + // Now Postgres is ready to accept connections + if status == "ready" && can_connect { + break; + } } }