# Question about file locking in badger

**URL:** <https://discuss.dgraph.io/t/question-about-file-locking-in-badger/7128>\
**Category:** Badger\
**Created:** [June 11, 2020, 12:12am UTC](https://discuss.dgraph.io/t/question-about-file-locking-in-badger/7128 "2020-06-11T00:12:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mschoch](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/mschoch/32/1881_2.png) [@mschoch](https://discuss.dgraph.io/u/mschoch)\
**Post date:** [June 11, 2020, 12:12am UTC](https://discuss.dgraph.io/t/question-about-file-locking-in-badger/7128/1 "2020-06-11T00:12:51Z")

</div>

I’ve been researching file locking options in Go and was reviewing the choices badger has made. In dir\_windows.go the following comment appears, “this works but it’s a bit klunky. i’d prefer to use LockFileEx but it needs unsafe pkg”. (link removed because your forum software is bullshit)

I found this a bit surprising because badger does use the unsafe package in other places, and in particular the dir\_unix.go relies on the “[golang.org/x/sys/unix](http://golang.org/x/sys/unix)” package, which also uses unsafe.

Is the comment in dir\_windows.go accurate, or is there some other reason to avoid using “[golang.org/x/sys/windows](http://golang.org/x/sys/windows)” and LockFileEx?

Thanks,  
marty

---

<div class="post-metadata">

**Author:** ![mrjn](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/mrjn/32/775_2.png) [@mrjn](https://discuss.dgraph.io/u/mrjn)\
**Post date:** [June 11, 2020, 1:45am UTC](https://discuss.dgraph.io/t/question-about-file-locking-in-badger/7128/2 "2020-06-11T01:45:39Z")

</div>

Hey @mschoch,

Nice to hear from you. I’ve increased your trust level, so you should be able to post links now.

I think Windows has been a bit of a blind spot for us, considering most of our contributors are either on Mac or Linux. I don’t recall any specific reason we shouldn’t use sys/windows.

@ibrahim What do you reckon?

---

<div class="post-metadata">

**Author:** ![ibrahim](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/ibrahim/32/6387_2.png) [@ibrahim](https://discuss.dgraph.io/u/ibrahim)\
**Post date:** [June 11, 2020, 1:24pm UTC](https://discuss.dgraph.io/t/question-about-file-locking-in-badger/7128/3 "2020-06-11T13:24:58Z")

</div>

Hey @mschoch,  
The code in `windows_dir.go` file hasn’t been touched in a while. I think there’s room for improvement. I’ve created an issue for using `LockFileEx` instead of `CreateFile` with `no sharing mode`. [Use LockFile for directory locking on windows · Issue #1364 · dgraph-io/badger · GitHub](https://github.com/dgraph-io/badger/issues/1364)

---

<div class="post-metadata">

**Author:** ![mschoch](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/mschoch/32/1881_2.png) [@mschoch](https://discuss.dgraph.io/u/mschoch)\
**Post date:** [June 13, 2020, 2:18am UTC](https://discuss.dgraph.io/t/question-about-file-locking-in-badger/7128/4 "2020-06-13T02:18:37Z")

</div>

Thanks for the replies @mrjn and @ibrahim.

For the code I was working on I needed both exclusive and shared locks. I seem to be able to get the behavior I want with unix.Flock() and windows.LockFileEx() but I’ve found 2 other libraries which both seem to choose to invoke LockFileEx manually via syscall+unsafe instead of using the [golang.org/x/sys/windows](http://golang.org/x/sys/windows) directly.

> <https://github.com/juju/fslock/blob/master/fslock_windows.go#L134>

> <https://github.com/gofrs/flock/blob/master/flock_winapi.go#L35>

So while I think I have what I need working now, I’m left wondering if there is something more that I’m still missing. I’ve subscribed to the issue you created, so I’ll track which direction you go, and please share anything you learn along the way.

marty

---

<div class="post-metadata">

**Author:** ![ibrahim](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/ibrahim/32/6387_2.png) [@ibrahim](https://discuss.dgraph.io/u/ibrahim)\
**Post date:** [June 13, 2020, 8:44am UTC](https://discuss.dgraph.io/t/question-about-file-locking-in-badger/7128/5 "2020-06-13T08:44:27Z")

</div>

@mschoch In both the repositories, the file locking code was written before golang added the `LockFileEx` API. The `LockFileEx` as added 9 months ago [windows: add LockFileEx, UnlockFileEx system calls · golang/sys@bb3f8db · GitHub](https://github.com/golang/sys/commit/bb3f8db39f24d596342144141f45701e604f5aed) while both the projects have locking code which was written at least 2 years ago. That explains why they’re using syscall+unsafe.

Also, it looks like the first repository is no longer in use [Can we remove this repo? · Issue #4 · juju/fslock · GitHub](https://github.com/juju/fslock/issues/4)
