Index: lib/repository.c
--- lib/repository.c.orig
+++ lib/repository.c
@@ -1838,21 +1838,6 @@ retry:
 		struct got_packidx *packidx;
 		struct got_object_qid *qid;
 
-		/*
-		 * If the modification time of the 'objects/pack' directory
-		 * has changed then 'pe' could now be an invalid pointer.
-		 */
-		if (tv.tv_sec != repo->pack_path_mtime.tv_sec ||
-		    tv.tv_nsec != repo->pack_path_mtime.tv_nsec) {
-			if (++retries > max_retries) {
-				err = got_error_msg(GOT_ERR_TIMEOUT,
-				    "too many concurrent pack file "
-				    "modifications");
-				goto done;
-			}
-			goto retry;
-		}
-
 		path_packidx = pe->path;
 
 		err = got_packidx_open(&packidx, got_repo_get_fd(repo),
@@ -1884,6 +1869,24 @@ retry:
 				    &qid->id);
 				if (err)
 					goto done;
+				/*
+				 * If the modification time of the
+				 * 'objects/pack' directory has changed then
+				 * 'pe' could now be an invalid pointer.
+				 */
+				if (tv.tv_sec !=
+				    repo->pack_path_mtime.tv_sec ||
+				    tv.tv_nsec !=
+				    repo->pack_path_mtime.tv_nsec) {
+					if (++retries > max_retries) {
+						err = got_error_msg(
+						    GOT_ERR_TIMEOUT,
+						    "too many concurrent "
+						    "pack file modifications");
+						goto done;
+					}
+					goto retry;
+				}
 				if (matched_type != obj_type)
 					continue;
 			}
