Fix deadlock

This commit is contained in:
Adam Štrauch 2025-07-20 23:42:16 +02:00
parent 517f0c848a
commit 196c2f59ef
Signed by: cx
GPG key ID: 7262DAFE292BCE20

View file

@ -47,30 +47,47 @@ func (d *IncusDriver) pipeToRestic(incusCmd *exec.Cmd, filename string, tags []s
resticCmd.Stderr = os.Stderr resticCmd.Stderr = os.Stderr
resticCmd.Env = os.Environ() resticCmd.Env = os.Environ()
// Start the incus export command // Start both commands
if err := incusCmd.Start(); err != nil { if err := incusCmd.Start(); err != nil {
return fmt.Errorf("failed to start incus export: %w", err) return fmt.Errorf("failed to start incus export: %w", err)
} }
// Start the restic backup command
if err := resticCmd.Start(); err != nil { if err := resticCmd.Start(); err != nil {
log.Println("DEBUG", resticCmd.String()) log.Println("DEBUG", resticCmd.String())
return fmt.Errorf("failed to start restic backup: %w", err) return fmt.Errorf("failed to start restic backup: %w", err)
} }
// Wait for the incus export command to finish // Wait for both commands concurrently
if err := incusCmd.Wait(); err != nil { var incusErr, resticErr error
log.Printf("incus export command failed: %v", err) done := make(chan struct{}, 2)
}
if err := w.Close(); err != nil { go func() {
log.Printf("failed to close pipe: %v", err) incusErr = incusCmd.Wait()
} if incusErr != nil {
log.Printf("incus export command failed: %v", incusErr)
}
w.Close() // Close the write end when incus is done
done <- struct{}{}
}()
// Wait for the restic backup command to finish go func() {
if err := resticCmd.Wait(); err != nil { resticErr = resticCmd.Wait()
log.Println("DEBUG", resticCmd.String()) if resticErr != nil {
return fmt.Errorf("restic backup command failed: %w", err) log.Println("DEBUG", resticCmd.String())
}
done <- struct{}{}
}()
// Wait for both commands to finish
<-done
<-done
// Return the first error encountered
if incusErr != nil {
return fmt.Errorf("incus export command failed: %w", incusErr)
}
if resticErr != nil {
return fmt.Errorf("restic backup command failed: %w", resticErr)
} }
return nil return nil