Skip to content

Report if file is locked for import command instead of hanging#1023

Merged
avinassh merged 5 commits into
tursodatabase:mainfrom
RohanVashisht1234:main
Mar 27, 2026
Merged

Report if file is locked for import command instead of hanging#1023
avinassh merged 5 commits into
tursodatabase:mainfrom
RohanVashisht1234:main

Conversation

@RohanVashisht1234

@RohanVashisht1234 RohanVashisht1234 commented Feb 27, 2026

Copy link
Copy Markdown
Contributor

The database import process gets stuck infinitely, if the database file is locked, it shows uploading without actually uploading, and also doesn't give any warning regarding this.

I have implemented that it will show an error if the database is locked.

test2.mov

@penberg penberg changed the title added file lock checking inside import command Report if file is locked for import command instead of hanging Mar 19, 2026
@avinassh avinassh requested a review from Copilot March 19, 2026 10:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to prevent turso db import <filename> from appearing to hang indefinitely when the source SQLite database file is locked, by detecting the locked state early and returning a clear error.

Changes:

  • Added a helper to check whether the target file is locked.
  • Added a pre-flight lock check in the db import command to fail fast with a user-friendly error.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/cmd/db_import.go
Comment thread internal/cmd/db_import.go Outdated
Comment on lines +10 to +30
"syscall"

"github.com/spf13/cobra"
)

func isFileLocked(filename string) (bool, error) {
f, err := os.Open(filename)
if err != nil {
return false, err
}
defer f.Close()

err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
if err != nil {
if errors.Is(err, syscall.EWOULDBLOCK) {
return true, nil // File is locked
}
return false, fmt.Errorf("failed to check lock: %w", err)
}

syscall.Flock(int(f.Fd()), syscall.LOCK_UN)
Comment thread internal/cmd/db_import.go
Comment on lines 3 to 13
import (
"errors"
"path/filepath"
"strings"

"fmt"
"os"
"syscall"

"github.com/spf13/cobra"
)
Comment thread internal/cmd/db_import.go
Comment on lines +56 to +59
locked, err := isFileLocked(filename)
if err != nil {
return fmt.Errorf("could not check file lock: %w", err)
}
Comment thread internal/cmd/db_import.go Outdated
return false, fmt.Errorf("failed to check lock: %w", err)
}

syscall.Flock(int(f.Fd()), syscall.LOCK_UN)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if this fails?

Comment thread internal/cmd/db_import.go Outdated
Comment on lines +15 to +32
func isFileLocked(filename string) (bool, error) {
f, err := os.Open(filename)
if err != nil {
return false, err
}
defer f.Close()

err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
if err != nil {
if errors.Is(err, syscall.EWOULDBLOCK) {
return true, nil // File is locked
}
return false, fmt.Errorf("failed to check lock: %w", err)
}

syscall.Flock(int(f.Fd()), syscall.LOCK_UN)
return false, nil
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func isFileLocked(filename string) (bool, error) {
f, err := os.Open(filename)
if err != nil {
return false, err
}
defer f.Close()
err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
if err != nil {
if errors.Is(err, syscall.EWOULDBLOCK) {
return true, nil // File is locked
}
return false, fmt.Errorf("failed to check lock: %w", err)
}
syscall.Flock(int(f.Fd()), syscall.LOCK_UN)
return false, nil
}
func isFileLocked(filename string) (bool, error) {
f, err := os.Open(filename)
if err != nil {
return false, fmt.Errorf("failed to open file: %w", err)
}
defer f.Close()
fd := int(f.Fd())
err = syscall.Flock(fd, syscall.LOCK_EX|syscall.LOCK_NB)
if err != nil {
if errors.Is(err, syscall.EWOULDBLOCK) {
return true, nil
}
return false, fmt.Errorf("failed to acquire lock: %w", err)
}
if err := syscall.Flock(fd, syscall.LOCK_UN); err != nil {
return false, fmt.Errorf("failed to release lock: %w", err)
}
return false, nil
}

@RohanVashisht1234

Copy link
Copy Markdown
Contributor Author

Ok, now I think I have done all the nessecary changes.

@avinassh avinassh merged commit 23e5daf into tursodatabase:main Mar 27, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants