CodisLabs / jodis

A java client for codis based on Jedis and Curator
MIT License
217 stars 97 forks source link

一个隐藏的异常处理风险 #43

Closed toruneko closed 7 years ago

toruneko commented 7 years ago
        for (ChildData childData : watcher.getCurrentData()) {
            try {
                CodisProxyInfo proxyInfo = MAPPER.readValue(childData.getData(),
                        CodisProxyInfo.class);
                if (!CODIS_PROXY_STATE_ONLINE.equals(proxyInfo.getState())) {
                    continue;
                }
                String addr = proxyInfo.getAddr();
                PooledObject pool = addr2Pool.remove(addr);
                if (pool == null) {
                    LOG.info("Add new proxy: " + addr);
                    String[] hostAndPort = addr.split(":");
                    String host = hostAndPort[0];
                    int port = Integer.parseInt(hostAndPort[1]);
                    pool = new PooledObject(addr,
                            new JedisPool(poolConfig, host, port, connectionTimeoutMs, soTimeoutMs,
                                    password, database, clientName, false, null, null, null));
                }
                builder.add(pool);
            } catch (Throwable t) {
                LOG.warn("parse " + childData.getPath() + " failed", t);
            }
        }

这里catch Throwable 实在是太大了。 Error继承了Throwable。如果出现jedis的包依赖冲突,引用了低版本的jedis,JedisPool构造函数不同导致NoSuchMethodError。结果被catch住了,变成了一个warn日志,应用还是正常启动了。T.T

建议只catch Exception吧。Error直接抛出来好了。

Apache9 commented 7 years ago

主要是为了防止出什么奇怪的问题导致这个线程退了不知道。不过你说的有道理,有兴趣提一个PR吗?