]> source.dussan.org Git - jgit.git/commitdiff
Retry loading config when locked by another process 30/126730/3
authorMichael Keppler <Michael.Keppler@gmx.de>
Sun, 29 Jul 2018 05:53:11 +0000 (07:53 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Mon, 10 Jun 2019 17:59:17 +0000 (19:59 +0200)
When loading the config, a FileNotFoundException may occur if the file
exists but cannot be read (see [1]). This is the case on Windows with a
virus scanner checking the file. Therefore if the file exists and that
exception is thrown, retry multiple times, similar to how this was
already implemented for IOException.

[1] https://docs.oracle.com/javase/8/docs/api/java/io/FileNotFoundException.html

Bug: 529522
Change-Id: Ic5dc3b7b24bb0005d6256ed00513bc7c0b91e613
Signed-off-by: Michael Keppler <Michael.Keppler@gmx.de>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties
org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java
org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileBasedConfig.java

index afa87617406b928be3568e53ad6a8e91f9b8e76f..df42dc757b045fcfca67f393a407904edc401202 100644 (file)
@@ -139,6 +139,7 @@ configSubsectionContainsNewline=config subsection name contains newline
 configSubsectionContainsNullByte=config subsection name contains byte 0x00
 configValueContainsNullByte=config value contains byte 0x00
 configHandleIsStale=config file handle is stale, {0}. retry
+configHandleMayBeLocked=config file handle may be locked by other process, {0}. retry
 connectionFailed=connection failed
 connectionTimeOut=Connection time out: {0}
 contextMustBeNonNegative=context must be >= 0
index 4d0d051562f1c2b05eb9a3318c870ee67c52a169..bdaef5a36699aafbdc6acf4028e398b86383333d 100644 (file)
@@ -200,6 +200,7 @@ public class JGitText extends TranslationBundle {
        /***/ public String configSubsectionContainsNullByte;
        /***/ public String configValueContainsNullByte;
        /***/ public String configHandleIsStale;
+       /***/ public String configHandleMayBeLocked;
        /***/ public String connectionFailed;
        /***/ public String connectionTimeOut;
        /***/ public String contextMustBeNonNegative;
index 2b31ebd8e3cf6d299b6d8fab996daf46b1ca648a..fc6f4a39cd592a805b756d7132b178983ad20393 100644 (file)
@@ -148,7 +148,8 @@ public class FileBasedConfig extends StoredConfig {
         */
        @Override
        public void load() throws IOException, ConfigInvalidException {
-               final int maxStaleRetries = 5;
+               final int maxRetries = 5;
+               int retryDelayMillis = 20;
                int retries = 0;
                while (true) {
                        final FileSnapshot oldSnapshot = snapshot;
@@ -177,6 +178,22 @@ public class FileBasedConfig extends StoredConfig {
                                }
                                return;
                        } catch (FileNotFoundException noFile) {
+                               // might be locked by another process (see exception Javadoc)
+                               if (retries < maxRetries && configFile.exists()) {
+                                       if (LOG.isDebugEnabled()) {
+                                               LOG.debug(MessageFormat.format(
+                                                               JGitText.get().configHandleMayBeLocked,
+                                                               Integer.valueOf(retries)), noFile);
+                                       }
+                                       try {
+                                               Thread.sleep(retryDelayMillis);
+                                       } catch (InterruptedException e) {
+                                               Thread.currentThread().interrupt();
+                                       }
+                                       retries++;
+                                       retryDelayMillis *= 2; // max wait 1260 ms
+                                       continue;
+                               }
                                if (configFile.exists()) {
                                        throw noFile;
                                }
@@ -185,7 +202,7 @@ public class FileBasedConfig extends StoredConfig {
                                return;
                        } catch (IOException e) {
                                if (FileUtils.isStaleFileHandle(e)
-                                               && retries < maxStaleRetries) {
+                                               && retries < maxRetries) {
                                        if (LOG.isDebugEnabled()) {
                                                LOG.debug(MessageFormat.format(
                                                                JGitText.get().configHandleIsStale,