Commit 4ebf745f064ac5a4ef2d2370ca386b5cd6b09fd5

Carlos Martín Nieto 2016-03-09T11:16:16

mwindow: free unused windows if we fail to mmap The first time may be due to memory fragmentation or just bad luck on a 32-bit system. When we hit the mmap error for the first time, free up the unused windows and try again.

diff --git a/src/mwindow.c b/src/mwindow.c
index 55c8d89..d3e9be7 100644
--- a/src/mwindow.c
+++ b/src/mwindow.c
@@ -296,8 +296,18 @@ static git_mwindow *new_window(
 	 */
 
 	if (git_futils_mmap_ro(&w->window_map, fd, w->offset, (size_t)len) < 0) {
-		git__free(w);
-		return NULL;
+		/*
+		 * The first error might be down to memory fragmentation even if
+		 * we're below our soft limits, so free up what we can and try again.
+		 */
+
+		while (git_mwindow_close_lru(mwf) == 0)
+			/* nop */;
+
+		if (git_futils_mmap_ro(&w->window_map, fd, w->offset, (size_t)len) < 0) {
+			git__free(w);
+			return NULL;
+		}
 	}
 
 	ctl->mmap_calls++;