Skip to content

Use all your internet connections at once on Windows, macOS, Linux and Android: per-connection load balancing over Wi-Fi, Ethernet, mobile data and phone tethers

Windows: retry saving limits.json while it is being read (#77)

Andrew Barnesfb4894fcf93d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 33123b9..9fa9c5b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,10 @@ entry here before pushing a version tag.
## [Unreleased]
+### Fixed
+- **`net-merge limit` on Windows no longer fails now and then with "Access is denied".** The running
+ instance could not save the new cap while the command was reading the saved caps to see it applied.
+
## [0.3.6] - 2026-10-28
### Added
diff --git a/internal/state/limits.go b/internal/state/limits.go
index 5bc3857..2b93b31 100644
--- a/internal/state/limits.go
+++ b/internal/state/limits.go
@@ -69,5 +69,5 @@ func SaveLimits(dir string, limits map[string]float64) error {
if err := os.WriteFile(tmp, append(b, '\n'), 0o644); err != nil {
return err
}
- return os.Rename(tmp, LimitsPath(dir))
+ return replaceFile(tmp, LimitsPath(dir))
}
diff --git a/internal/state/limits_test.go b/internal/state/limits_test.go
index e550823..a209f71 100644
--- a/internal/state/limits_test.go
+++ b/internal/state/limits_test.go
@@ -5,6 +5,7 @@ import (
"path/filepath"
"reflect"
"testing"
+ "time"
)
func TestLimitsRoundTrip(t *testing.T) {
@@ -77,3 +78,28 @@ func TestLimitsSurviveARebootOnMacOS(t *testing.T) {
t.Errorf("LimitsPath(%q) = %q, want %q", macStateDir, got, want)
}
}
+
+// `net-merge limit` reads limits.json every 50 ms while it waits for the running instance to save
+// the new cap. On Windows a rename over a file someone has open fails ("Access is denied"), so a
+// save that lands during one of those reads has to wait for it instead of failing.
+func TestSaveLimitsWhileTheFileIsOpen(t *testing.T) {
+ dir := t.TempDir()
+ if err := SaveLimits(dir, map[string]float64{"Wi-Fi": 5}); err != nil {
+ t.Fatal(err)
+ }
+ f, err := os.Open(LimitsPath(dir))
+ if err != nil {
+ t.Fatal(err)
+ }
+ time.AfterFunc(100*time.Millisecond, func() { f.Close() })
+ if err := SaveLimits(dir, map[string]float64{"Wi-Fi": 10}); err != nil {
+ t.Fatalf("SaveLimits while a reader has the file open: %v", err)
+ }
+ got, err := LoadLimits(dir)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got["Wi-Fi"] != 10 {
+ t.Fatalf("got %v, want the new cap saved", got)
+ }
+}
diff --git a/internal/state/replace_other.go b/internal/state/replace_other.go
new file mode 100644
index 0000000..ad0bbb6
--- /dev/null
+++ b/internal/state/replace_other.go
@@ -0,0 +1,8 @@
+//go:build !windows
+
+package state
+
+import "os"
+
+// replaceFile renames tmp over dst.
+func replaceFile(tmp, dst string) error { return os.Rename(tmp, dst) }
diff --git a/internal/state/replace_windows.go b/internal/state/replace_windows.go
new file mode 100644
index 0000000..08b07f8
--- /dev/null
+++ b/internal/state/replace_windows.go
@@ -0,0 +1,24 @@
+package state
+
+import (
+ "errors"
+ "os"
+ "time"
+
+ "golang.org/x/sys/windows"
+)
+
+// replaceFile renames tmp over dst. Windows refuses that while another process has dst open, and
+// `net-merge limit` opens limits.json every 50 ms while it waits for the running instance to save
+// it, so wait a moment for the reader to finish instead of failing.
+func replaceFile(tmp, dst string) error {
+ deadline := time.Now().Add(time.Second)
+ for {
+ err := os.Rename(tmp, dst)
+ if err == nil || time.Now().After(deadline) ||
+ !errors.Is(err, windows.ERROR_ACCESS_DENIED) && !errors.Is(err, windows.ERROR_SHARING_VIOLATION) {
+ return err
+ }
+ time.Sleep(10 * time.Millisecond)
+ }
+}