【问题标题】:Netty Channel.close() hangs intermittentlyNetty Channel.close() 间歇性挂起
【发布时间】:2012-03-23 07:48:51
【问题描述】:

Netty Channel.close() 偶尔会挂起。在我们的特定用例中,我们有一个通道池,我们的测试检查对网络故障的容忍度。因此,在我们的测试中,我们尝试关闭一个频道。

在下面的代码 sn-p 中,我们在调用 Channel.close() 之前、Channel.close() 之后和 ChannelFuture.await() 之后打印调试语句。为了确保线程没有被中断,我们检查 InterruptedException。

        Channel c = partitionChannelMap.get(partition);
        if (c != null) {
            for (int retries = 0; retries < numRetries; retries++) {
                try {
                    logger.debug("Attempt {}: Closing channel to partition {}", retries + 1, partition);
                    logger.debug("Channel Properties - isBound() isConnected() isOpen() " + c.isBound() + " "
                            + c.isConnected() + " " + c.isOpen());
                    ChannelFuture closeFuture = c.close();
                    logger.debug("About to wait");
                    closeFuture.await(nettyTimeout);
                    if (closeFuture.isSuccess()) {
                        logger.debug("Attempt {}: CLOSED channel to partition {}", retries + 1, partition);
                        partitionChannelMap.remove(partition);
                        break;
                    } else {
                        logger.error("Attempt {}: FAILED to close partition {}", retries + 1, partition);
                        continue;
                    }
                } catch (InterruptedException e) {
                    logger.error("Attempt {}: FAILED to close partition {}", retries + 1, partition);
                    e.printStackTrace();
                    continue;
                }
            }
        }
    }

在某些运行(错误的)中,Channel.close() 之前的调试语句会被执行,而紧随其后的则不会。由于 Channel.close() 是异步的,我们希望它立即返回。在这些情况下,调用 Channel.close() 后执行会挂起。

我在这里假设或做错了什么?

错误执行的示例输出 -

15:12:32.497 [Thread-7] DEBUG org.apache.s4.comm.tcp.TCPEmitter - Attempt 1: Closing channel to partition 0
15:12:32.497 [Thread-7] DEBUG org.apache.s4.comm.tcp.TCPEmitter - Channel Properties - isBound() isConnected() isOpen() true true true

我非常感谢您对此的任何帮助。

谢谢

【问题讨论】:

    标签: netty


    【解决方案1】:

    问题在于我的代码。

    我在同步块中调用 Channel.close()。 close() 会干扰并发进行中的消息传输,并异步调用失败传输的 operationComplete()。碰巧 operationComplete() 处理程序也尝试关闭同一个通道,导致死锁。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-21
      • 2016-09-14
      • 2016-08-18
      • 2014-05-25
      • 1970-01-01
      相关资源
      最近更新 更多