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.Env = os.Environ()
// Start the incus export command
// Start both commands
if err := incusCmd.Start(); err != nil {
return fmt.Errorf("failed to start incus export: %w", err)
}
// Start the restic backup command
if err := resticCmd.Start(); err != nil {
log.Println("DEBUG", resticCmd.String())
return fmt.Errorf("failed to start restic backup: %w", err)
}
// Wait for the incus export command to finish
if err := incusCmd.Wait(); err != nil {
log.Printf("incus export command failed: %v", err)
}
// Wait for both commands concurrently
var incusErr, resticErr error
done := make(chan struct{}, 2)
if err := w.Close(); err != nil {
log.Printf("failed to close pipe: %v", err)
}
go func() {
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
if err := resticCmd.Wait(); err != nil {
log.Println("DEBUG", resticCmd.String())
return fmt.Errorf("restic backup command failed: %w", err)
go func() {
resticErr = resticCmd.Wait()
if resticErr != nil {
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