Quantcast
Channel: Active questions tagged redis+java - Stack Overflow
Viewing all articles
Browse latest Browse all 2204

IllegalMonitorStateException - Attempt to unlock read lock, not locked by current thread, but I don't know why

$
0
0
        RLock lock = lockService.getLock(TOPICNAME_LOCK_PREFIX + topicPrefix);        if (lock.tryLock(5, TimeUnit.SECONDS)) {            try {                // Process business CODE            } catch (Exception e) {               throw new ServiceException(String.format("error: %s!", e));            } finally {                lock.unlock();            }        }        // No lock obtained CODE

lockService code:

    private static RedissonClient redissonClient;    private static synchronized void initClient(String url) {        if (redissonClient == null) {            try {                URI uri = new URI(url);                Config config = new Config();                SingleServerConfig srvConfig = config.useSingleServer().                setAddress(String.format("%s://%s:%s", uri.getScheme(), uri.getHost(), uri.getPort()));                if (StringUtils.isNotBlank(uri.getUserInfo())) {                    srvConfig.setPassword(uri.getUserInfo());                }                config.setLockWatchdogTimeout(10000);                redissonClient = Redisson.create(config);            } catch (Exception e) {                log.error(String.format("cannot init redisson client by %s", url), e);            }        }    }    @PostConstruct    private void init() {        initClient(redisProperties.getUrl());    }    public RLock getLock(String key) {        initClient(redisProperties.getUrl());        return redissonClient.getLock(key);    }
<dependency><groupId>org.redisson</groupId><artifactId>redisson</artifactId><version>3.17.7</version></dependency>

Exceptions sometimes occur:

java.lang.IllegalMonitorStateException: attempt to unlock lock, notlocked by current thread by node id: 1118e65ad1 thread-id: 8545 atorg.redisson.RedissonBaseLock.lambda$unlockAsync$2(RedissonBaseLock.java:328)atjava.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:822)atjava.util.concurrent.CompletableFuture$UniHandle.tryFire(CompletableFuture.java:797)atjava.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:474)atjava.util.concurrent.CompletableFuture.complete(CompletableFuture.java:1962)atorg.redisson.command.CommandBatchService.lambda$executeAsync$7(CommandBatchService.java:322)atjava.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:760)atjava.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:736)atjava.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:474)atjava.util.concurrent.CompletableFuture.complete(CompletableFuture.java:1962)atorg.redisson.command.RedisCommonBatchExecutor.handleResult(RedisCommonBatchExecutor.java:163)atorg.redisson.command.RedisExecutor.checkAttemptPromise(RedisExecutor.java:524)atorg.redisson.command.RedisExecutor.lambda$execute$4(RedisExecutor.java:176)atjava.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:760)atjava.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:736)atjava.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:474)atjava.util.concurrent.CompletableFuture.complete(CompletableFuture.java:1962)atorg.redisson.client.handler.CommandDecoder.decodeCommandBatch(CommandDecoder.java:318)atorg.redisson.client.handler.CommandDecoder.decodeCommand(CommandDecoder.java:210)atorg.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:137)atorg.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:113)atio.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:508)atio.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:366)atio.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276)atio.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)atio.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)atio.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)atio.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)atio.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)atio.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)atio.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)atio.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)atio.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719)atio.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)atio.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) atio.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)atio.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)atio.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)at java.lang.Thread.run(Thread.java:882)

The business logic is that only one task is allowed in a certain range, so it needs to acquire the lock first, and other tasks are waiting. If it does not obtain the lock after waiting for 5 seconds, another set of logic will be executed.

This set of code has almost no errors, because lock.unlock() is only executed if lock.tryLock(5, TimeUnit.SECONDS) returns true.

However, for certain business peak hours, the above exceptions sometimes occur. The specific performance is that many tasks report errors: java.lang.IllegalMonitorStateException: attempt to unlock, not locked by current thread by node id: SameId thread id: DifferentThreadId.The above error reports occur in a centralized manner. Many threads release the same lock within a few seconds.

Is this caused by network? Or is there a problem with the code?


I solved this problem by using Semaphore(org.redisson.api.RPermitExpirableSemaphore)


Viewing all articles
Browse latest Browse all 2204

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>