Skip to content

Commit 7f2af48

Browse files
committed
fix: add timeout to MySQL apt-get installation
Add context timeout to prevent apt-get install from blocking indefinitely when network is unavailable or slow. Uses 60 second timeout for install and 10 second timeout for debconf setup. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent a354b38 commit 7f2af48

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

internal/sqltest/native/mysql.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,25 @@ func startMySQLServer(ctx context.Context) (string, error) {
9696
if _, err := exec.LookPath("mysql"); err != nil {
9797
slog.Info("native/mysql", "status", "installing")
9898

99-
// Pre-configure MySQL root password
100-
setSelectionsCmd := exec.Command("sudo", "bash", "-c",
99+
// Pre-configure MySQL root password (with timeout)
100+
debconfCtx, debconfCancel := context.WithTimeout(ctx, 10*time.Second)
101+
defer debconfCancel()
102+
setSelectionsCmd := exec.CommandContext(debconfCtx, "sudo", "bash", "-c",
101103
`echo "mysql-server mysql-server/root_password password mysecretpassword" | sudo debconf-set-selections && `+
102104
`echo "mysql-server mysql-server/root_password_again password mysecretpassword" | sudo debconf-set-selections`)
103105
setSelectionsCmd.Env = append(os.Environ(), "DEBIAN_FRONTEND=noninteractive")
104106
if output, err := setSelectionsCmd.CombinedOutput(); err != nil {
105107
slog.Debug("native/mysql", "debconf", string(output))
106108
}
107109

108-
// Try to install MySQL server
109-
cmd := exec.Command("sudo", "apt-get", "install", "-y", "-qq", "mysql-server")
110+
// Try to install MySQL server (with timeout)
111+
installCtx, installCancel := context.WithTimeout(ctx, 60*time.Second)
112+
defer installCancel()
113+
cmd := exec.CommandContext(installCtx, "sudo", "apt-get", "install", "-y", "-qq", "mysql-server")
110114
cmd.Env = append(os.Environ(), "DEBIAN_FRONTEND=noninteractive")
111115
if output, err := cmd.CombinedOutput(); err != nil {
112-
// If apt-get fails (no network), return error
113-
return "", fmt.Errorf("apt-get install mysql-server failed (network may be unavailable): %w\n%s", err, output)
116+
// If apt-get fails (no network or timeout), return error
117+
return "", fmt.Errorf("apt-get install mysql-server failed (network may be unavailable or timed out): %w\n%s", err, output)
114118
}
115119
}
116120

0 commit comments

Comments
 (0)