alibaba/nacos · error · IOException

lock {} conflict

Error message

lock {} conflict

What it means

Thrown by ConcurrentDiskUtil.tryLock when channel.tryLock fails more than RETRY_COUNT (10) times with exponential backoff (IOException). The file lock on the target file is held by another process/thread and could not be acquired within the retry budget. The message includes the absolute path of the contended file.

Source

Thrown at client/src/main/java/com/alibaba/nacos/client/utils/ConcurrentDiskUtil.java:165

        } catch (InterruptedException e) {
            LOGGER.warn("sleep wrong", e);
            // set the interrupted flag
            Thread.currentThread().interrupt();
        }
    }
    
    private static FileLock tryLock(File file, FileChannel channel, boolean shared)
        throws IOException {
        FileLock result = null;
        int i = 0;
        do {
            try {
                result = channel.tryLock(0L, Long.MAX_VALUE, shared);
            } catch (Exception e) {
                ++i;
                if (i > RETRY_COUNT) {
                    LOGGER.error("[NA] lock " + file.getName() + " fail;retryed time: " + i, e);
                    throw new IOException("lock " + file.getAbsolutePath() + " conflict");
                }
                sleep(SLEEP_BASETIME * i);
                LOGGER.warn("lock " + file.getName() + " conflict;retry time: " + i);
            }
        } while (null == result);
        return result;
    }
    
}

View on GitHub (pinned to 9b989acdf1)

Solutions

  1. Reduce contention: ensure only one process/thread writes to the file at a time.
  2. Check for orphaned Nacos client processes that may hold locks and restart them.
  3. If on NFS/network filesystem, verify locking support and consider a local filesystem for cache files.
  4. Increase RETRY_COUNT or SLEEP_BASETIME via a custom build if contention is expected and transient.
Defensive patterns

Strategy: retry

Try / catch

int maxLockRetries = 3;
for (int attempt = 0; attempt <= maxLockRetries; attempt++) {
    try {
        ConcurrentDiskUtil.writeFileContent(file, content, charsetName);
        break;
    } catch (IOException e) {
        if (e.getMessage().contains("conflict") && attempt < maxLockRetries) {
            Thread.sleep(500L * (attempt + 1));
            continue;
        }
        throw e;
    }
}

Prevention

When it happens

Trigger: Another JVM or thread holds an exclusive FileLock on the same file for longer than 10 retries with increasing sleep intervals (10ms, 20ms, ..., 100ms). On each failed attempt the method sleeps SLEEP_BASETIME * i milliseconds before retrying.

Common situations: Multiple Nacos client instances (or multiple threads in one process) writing to the same config cache file; a previous JVM crashed leaving a stale lock (rare on most OSes as locks are released on process exit); NFS or network filesystem locking issues; long-running write operations holding the lock.

Related errors


AI-assisted analysis of alibaba/nacos@9b989acdf1 (2026-08-14). Data as JSON: /api/errors/b7fcace5d587e442. Report an issue: GitHub.