Report if file is locked for import command instead of hanging#1023
Merged
Conversation
Contributor
There was a problem hiding this comment.
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 importcommand to fail fast with a user-friendly error.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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 on lines
3
to
13
| import ( | ||
| "errors" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "fmt" | ||
| "os" | ||
| "syscall" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) |
Comment on lines
+56
to
+59
| locked, err := isFileLocked(filename) | ||
| if err != nil { | ||
| return fmt.Errorf("could not check file lock: %w", err) | ||
| } |
avinassh
requested changes
Mar 19, 2026
| return false, fmt.Errorf("failed to check lock: %w", err) | ||
| } | ||
|
|
||
| syscall.Flock(int(f.Fd()), syscall.LOCK_UN) |
avinassh
requested changes
Mar 19, 2026
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 | ||
| } |
Member
There was a problem hiding this comment.
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 | |
| } |
…o added a check for Flock function
Contributor
Author
|
Ok, now I think I have done all the nessecary changes. |
avinassh
approved these changes
Mar 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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