alibaba/nacos · error · IllegalStateException

NacosLockService has been shut down

Error message

NacosLockService has been shut down

What it means

Thrown by NacosLockService.checkNotClosed() when any lock operation (lock, unLock, renew, remoteTryLock, remoteReleaseLock, getReentrantLock, getNonReentrantLock) is invoked after shutdown(). The service uses an AtomicBoolean 'closed' flag; shutdown() flips it to true via compareAndSet. After that, all public methods reject calls with IllegalStateException to prevent use of torn-down resources (gRPC client, watchdog, server list manager).

Source

Thrown at client/src/main/java/com/alibaba/nacos/client/lock/NacosLockService.java:77

    private ScheduledExecutorService executorService;
    
    public NacosLockService(Properties properties) throws NacosException {
        NacosClientProperties nacosClientProperties =
            NacosClientProperties.PROTOTYPE.derive(properties);
        this.serverListManager = new NamingServerListManager(properties);
        serverListManager.start();
        this.securityProxy = new SecurityProxy(serverListManager,
            NamingHttpClientManager.getInstance().getNacosRestTemplate());
        initSecurityProxy(nacosClientProperties);
        this.lockGrpcClient =
            new LockGrpcClient(nacosClientProperties, serverListManager, securityProxy);
        this.watchdog = new NacosLockWatchdog();
        this.clientId = UUID.randomUUID().toString();
    }
    
    private void checkNotClosed() {
        if (closed.get()) {
            throw new IllegalStateException("NacosLockService has been shut down");
        }
    }
    
    private void initSecurityProxy(NacosClientProperties properties) {
        this.executorService = new ScheduledThreadPoolExecutor(1, r -> {
            Thread t = new Thread(r);
            t.setName("com.alibaba.nacos.client.lock.security");
            t.setDaemon(true);
            return t;
        });
        final Properties nacosClientPropertiesView = properties.asProperties();
        this.securityProxy.login(nacosClientPropertiesView);
        this.executorService.scheduleWithFixedDelay(
            () -> securityProxy.login(nacosClientPropertiesView), 0,
            SECURITY_INFO_REFRESH_INTERVAL_MILLS, TimeUnit.MILLISECONDS);
    }
    
    @Override

View on GitHub (pinned to 9b989acdf1)

Solutions

  1. Order shutdown so no lock operations occur after NacosLockService.shutdown(); release all locks and finish critical sections first.
  2. Track service lifecycle in your own code and guard lock calls with an isOpen/isRunning check before invoking.
  3. In DI containers, ensure NacosLockService is destroyed last; use @DependsOn or explicit destroy-order so consumers shut down before the service.
  4. If the service was closed unexpectedly, create a new NacosLockService instance rather than reusing the shut-down one.

Example fix

// before
lockService.shutdown();
lockService.lock(instance); // throws IllegalStateException

// after
lockService.shutdown();
// ensure no further lock calls; if needed, create a fresh instance
NacosLockService fresh = new NacosLockService(properties);
Defensive patterns

Strategy: validation

Validate before calling

// Track your own lifecycle flag before calling lock operations
if (!closed) { lockService.lock(instance); }

Try / catch

try { lockService.lock(instance); } catch (IllegalStateException e) { if (e.getMessage().contains("shut down")) { // service closed; create a new instance or stop } }

Prevention

When it happens

Trigger: Calling lock/unlock/renew after NacosLockService.shutdown(); holding a NacosLock reference obtained before shutdown and calling unlock() on it; a dependency-injection or lifecycle container shutting down the service while application code still issues lock operations.

Common situations: Application shutdown sequences where the NacosLockService is closed before outstanding lock operations finish; Spring/CDI @PreDestroy ordering issues; frameworks (test teardown, servlet context destroy) closing shared resources too early; using a cached/stale service reference after a reconnect that created a new service.

Related errors


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