Blob Blame History Raw
diff --git a/refs.c b/refs.c
index 67d6745..ddb9a77 100644
--- a/refs.c
+++ b/refs.c
@@ -1422,6 +1422,7 @@ static struct ref_dir *get_loose_refs(struct ref_cache *refs)
 /* We allow "recursive" symbolic refs. Only within reason, though */
 #define MAXDEPTH 5
 #define MAXREFLEN (1024)
+#define MAXRETRIES 5
 
 /*
  * Called by resolve_gitlink_ref_recursive() after it failed to read
@@ -1576,6 +1577,7 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags, unsigned
 		struct stat st;
 		char *buf;
 		int fd;
+		int retries = 0;
 
 		if (--depth < 0) {
 			errno = ELOOP;
@@ -1612,7 +1614,8 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags, unsigned
 		if (S_ISLNK(st.st_mode)) {
 			len = readlink(path, buffer, sizeof(buffer)-1);
 			if (len < 0) {
-				if (errno == ENOENT || errno == EINVAL)
+				if ((errno == ENOENT || errno == EINVAL) &&
+					retries++ < MAXRETRIES)
 					/* inconsistent with lstat; retry */
 					goto stat_ref;
 				else
@@ -1645,7 +1648,7 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags, unsigned
 		 */
 		fd = open(path, O_RDONLY);
 		if (fd < 0) {
-			if (errno == ENOENT)
+			if (errno == ENOENT && retries++ < MAXRETRIES)
 				/* inconsistent with lstat; retry */
 				goto stat_ref;
 			else