alibaba/nacos · error · IllegalStateException

WatchFileCenter already shutdown

Error message

WatchFileCenter already shutdown

What it means

Thrown by WatchFileCenter.checkState() when an operation (e.g. registering a new watcher) is attempted after the WatchFileCenter has been shut down (CLOSED flag is true). The center refuses post-shutdown work.

Source

Thrown at sys/src/main/java/com/alibaba/nacos/sys/file/WatchFileCenter.java:282

            }
        }
        
        private void eventOverflow() {
            File dir = Paths.get(paths).toFile();
            for (File file : Objects.requireNonNull(dir.listFiles())) {
                // Subdirectories do not participate in listening
                if (file.isDirectory()) {
                    continue;
                }
                eventProcess(file.getName());
            }
        }
        
    }
    
    private static void checkState() {
        if (CLOSED.get()) {
            throw new IllegalStateException("WatchFileCenter already shutdown");
        }
    }
}

View on GitHub (pinned to 9b989acdf1)

Solutions

  1. Check WatchFileCenter is still active (or guard with isClosed) before registering watchers during shutdown.
  2. Fix component lifecycle ordering so watchers register before shutdown.
  3. Avoid lazy watcher registration in @PreDestroy or shutdown paths.
  4. If re-registration is expected after a restart, ensure the center is re-initialized first.

Example fix

// before (lazy register during shutdown)
@PreDestroy
public void reload() {
    WatchFileCenter.registerWatcher(path, watcher); // center already closed
}

// after (register in init, not destroy)
@PostConstruct
public void init() {
    if (!WatchFileCenter.isClosed()) {
        WatchFileCenter.registerWatcher(path, watcher);
    }
}
Defensive patterns

Strategy: validation

Validate before calling

if (WatchFileCenter.isClosed()) {
    // skip registration; component is shutting down
    return;
}
WatchFileCenter.registerWatcher(path, watcher);

Try / catch

try {
    WatchFileCenter.registerWatcher(path, watcher);
} catch (IllegalStateException e) {
    if ("WatchFileCenter already shutdown".equals(e.getMessage())) {
        // skip; lifecycle ordering issue, do not retry
    }
    throw e;
}

Prevention

When it happens

Trigger: After WatchFileCenter.shutdown() (or JVM shutdown hook) runs and sets CLOSED, a subsequent registerWatcher()/watch request invokes checkState() and fails.

Common situations: A component initializes lazily during Spring context closing and tries to register a file watcher after WatchFileCenter already shut down; a restart/reload flow re-registers watchers without checking liveness; ordering issue between shutdown hooks.

Related errors


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