From 3ac2a2e22d863753a6b568596fbe9d76d03023b5 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 8 Aug 2025 12:38:55 -0700 Subject: [PATCH] fix tikv listing due to expired entries (#7115) * fix tikv listing due to expired entries When there are many entries with empty fileName values (which can happen after TTL cleanup), the continue statements prevent the loop counter from incrementing, creating an infinite loop. * address comments * Update weed/filer/tikv/tikv_store.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * address comments Update weed/filer/tikv/tikv_store.go Co-Authored-By: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- weed/filer/tikv/tikv_store.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/weed/filer/tikv/tikv_store.go b/weed/filer/tikv/tikv_store.go index abc7ea55f..c02f2c22e 100644 --- a/weed/filer/tikv/tikv_store.go +++ b/weed/filer/tikv/tikv_store.go @@ -228,19 +228,31 @@ func (store *TikvStore) ListDirectoryPrefixedEntries(ctx context.Context, dirPat return err } defer iter.Close() - for i := int64(0); i < limit && iter.Valid(); i++ { + i := int64(0) + for iter.Valid() { key := iter.Key() if !bytes.HasPrefix(key, directoryPrefix) { break } fileName := getNameFromKey(key) - if fileName == "" || fileName == startFileName && !includeStartFile { + if fileName == "" { if err := iter.Next(); err != nil { break - } else { - continue } + continue } + if fileName == startFileName && !includeStartFile { + if err := iter.Next(); err != nil { + break + } + continue + } + + // Check limit only before processing valid entries + if limit > 0 && i >= limit { + break + } + lastFileName = fileName entry := &filer.Entry{ FullPath: util.NewFullPath(string(dirPath), fileName), @@ -252,11 +264,15 @@ func (store *TikvStore) ListDirectoryPrefixedEntries(ctx context.Context, dirPat glog.V(0).InfofCtx(ctx, "list %s : %v", entry.FullPath, err) break } + + // Only increment counter after successful processing + i++ + if err := iter.Next(); !eachEntryFunc(entry) || err != nil { break } } - return nil + return err }) if err != nil { return lastFileName, fmt.Errorf("prefix list %s : %v", dirPath, err)