tidwall / buntdb

BuntDB is an embeddable, in-memory key/value database for Go with custom indexing and geospatial support
MIT License
4.57k stars 289 forks source link

ido/fix crash on windows #120

Closed idosavion closed 1 month ago

idosavion commented 2 months ago

During Shrink, we try to move the temporary file which was created to the database's original location, this may fail on windows because if you try to rename a file to a name that already exists, the operation will fail with an "Access is denied" error (while succeed and overwrite the file on mac and linux).

Additionally, fixed a small typo in test

idosavion commented 2 months ago

@tidwall, first of all, thanks for maintaining this project! Another approach would be to first try to perform the renaming and only delete the file and retry on failure. I think this one is more straight forward, but let me know if you prefer the one suggested here.

tidwall commented 2 months ago

Can we do an optimistic check first and test for windows?

Such as :

var err error
if err = os.Rename(src, dest); err != nil {
    if os == WINDOWS {
        if err = os.Remove(dest); err == nil {
            err = os.Rename(src, dest);
        }
    }
}
return err;

This way we will effectively keep the same syscall operations on posix systems, and it won't break the atomicity guarantees with moving and unlinking files that are opened by multiple processes.

idosavion commented 2 months ago

Thanks for the quick response @tidwall! I confirmed the (modified) fix on windows

tidwall commented 1 month ago

Thanks!