Skip to content

Commit

Permalink
mysqlctl: Fix cleaning up the stale lock file
Browse files Browse the repository at this point in the history
The lock file can have a newline, so we need to trim any space like
characters before we try to parse the PID.

Signed-off-by: Dirkjan Bussink <[email protected]>
  • Loading branch information
dbussink committed Nov 24, 2023
1 parent e08d151 commit 9d7a41a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 3 additions & 1 deletion go/vt/mysqlctl/mysqld.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions go/vt/mysqlctl/mysqld_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 9d7a41a

Please sign in to comment.