diff --git a/go/vt/mysqlctl/mysqld.go b/go/vt/mysqlctl/mysqld.go index a2d2c5b7d71..52f806fe29b 100644 --- a/go/vt/mysqlctl/mysqld.go +++ b/go/vt/mysqlctl/mysqld.go @@ -438,14 +438,16 @@ func cleanupLockfile(socket string, ts string) error { lockPath := fmt.Sprintf("%s.lock", socket) pid, err := os.ReadFile(lockPath) if errors.Is(err, os.ErrNotExist) { + log.Infof("%v: no stale lock file at %s", ts, lockPath) // If there's no lock file, we can early return here, nothing // to clean up then. return nil } else if err != nil { + log.Errorf("%v: error checking if lock file exists: %v", ts, err) // Any other errors here are unexpected. return err } - p, err := strconv.Atoi(string(pid)) + p, err := strconv.Atoi(string(bytes.TrimSpace(pid))) if err != nil { log.Errorf("%v: error parsing pid from lock file: %v", ts, err) return err diff --git a/go/vt/mysqlctl/mysqld_test.go b/go/vt/mysqlctl/mysqld_test.go index 5ee21196ddd..b45ab5bd6d8 100644 --- a/go/vt/mysqlctl/mysqld_test.go +++ b/go/vt/mysqlctl/mysqld_test.go @@ -188,6 +188,11 @@ func TestCleanupLockfile(t *testing.T) { assert.NoError(t, cleanupLockfile("mysql.sock", ts)) assert.NoFileExists(t, "mysql.sock.lock") + // If lockfile exists, but the process is not found, we clean it up. + os.WriteFile("mysql.sock.lock", []byte("123456789\n"), 0o600) + assert.NoError(t, cleanupLockfile("mysql.sock", ts)) + assert.NoFileExists(t, "mysql.sock.lock") + // If the lockfile exists, and the process is found, we don't clean it up. os.WriteFile("mysql.sock.lock", []byte(strconv.Itoa(os.Getpid())), 0o600) assert.NoError(t, cleanupLockfile("mysql.sock", ts))