Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis 2.4.2 java.lang.ArrayIndexOutOfBoundsException: 8192 #636

Closed
xiaonanforever opened this issue May 20, 2014 · 10 comments
Closed

Redis 2.4.2 java.lang.ArrayIndexOutOfBoundsException: 8192 #636

xiaonanforever opened this issue May 20, 2014 · 10 comments
Milestone

Comments

@xiaonanforever
Copy link

In my log,I found 👍
redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Broken pipe
at redis.clients.jedis.Protocol.sendCommand(Protocol.java:83) ~[jedis-2.4.2.jar:na]
at redis.clients.jedis.Protocol.sendCommand(Protocol.java:63) ~[jedis-2.4.2.jar:na]
at redis.clients.jedis.Connection.sendCommand(Connection.java:84) ~[jedis-2.4.2.jar:na]
at redis.clients.jedis.BinaryClient.hgetAll(BinaryClient.java:273) ~[jedis-2.4.2.jar:na]
at redis.clients.jedis.Client.hgetAll(Client.java:197) ~[jedis-2.4.2.jar:na]
......
Caused by: java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method) ~[na:1.7.0_45]
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113) ~[na:1.7.0_45]
at java.net.SocketOutputStream.write(SocketOutputStream.java:159) ~[na:1.7.0_45]
at redis.clients.util.RedisOutputStream.flushBuffer(RedisOutputStream.java:31) ~[jedis-2.4.2.jar:na]
at redis.clients.util.RedisOutputStream.write(RedisOutputStream.java:39) ~[jedis-2.4.2.jar:na]
at redis.clients.jedis.Protocol.sendCommand(Protocol.java:69) ~[jedis-2.4.2.jar:na]
... 48 common frames omitted
Following:
java.lang.ArrayIndexOutOfBoundsException: 8192
at redis.clients.util.RedisOutputStream.write(RedisOutputStream.java:37) ~[jedis-2.4.2.jar:na]
at redis.clients.jedis.Protocol.sendCommand(Protocol.java:69) ~[jedis-2.4.2.jar:na]
at redis.clients.jedis.Protocol.sendCommand(Protocol.java:63) ~[jedis-2.4.2.jar:na]
at redis.clients.jedis.Connection.sendCommand(Connection.java:84) ~[jedis-2.4.2.jar:na]
at redis.clients.jedis.BinaryClient.hgetAll(BinaryClient.java:273) ~[jedis-2.4.2.jar:na]

8192? Yes it's here:

public final class RedisOutputStream extends FilterOutputStream {
protected final byte buf[];

protected int count;

public RedisOutputStream(final OutputStream out) {
    this(out, 8192);
}

public RedisOutputStream(final OutputStream out, final int size) {
    super(out);
    if (size <= 0) {
        throw new IllegalArgumentException("Buffer size <= 0");
    }
    buf = new byte[size];
}

If connection disconnet,and that time if count=8192,

private void flushBuffer() throws IOException {
if (count > 0) {
out.write(buf, 0, count);//my throw java.net.SocketException: Broken pipe,then the count allways is 8192,and ArrayIndexOutOfBoundsException 8193、8194 and so....
count = 0;
}
}

@xiaonanforever
Copy link
Author

out.write(buf, 0, count);
//may throw java.net.SocketException: Broken pipe,then the count allways is 8192,and ArrayIndexOutOfBoundsException 8193、8194 and so....

@marcosnils
Copy link
Contributor

Hi @xiaonanforever, thanks for reporting the error.

I could reproduce the scenario and actually made an automated test for it. I'll try to get it fixed by today.

Keep you posted.

Best,

Marcos.

@marcosnils
Copy link
Contributor

@xiaonanforever I've pushed a fix in the protocol_bound_fix branch. Is there a chance you can give it a try?

@HeartSaVioR
Copy link
Contributor

@marcosnils 👍
@xiaonanforever
Just FYI, User must discard Jedis instance when JedisConnectionException has occurred.
When RedisOutputStream.write() throws IOException, Protocol.sendCommand() catches it and throws JedisConnectionException.

@xiaonanforever
Copy link
Author

@marcosnils
ok.I will try it again!
Thank you!

@xiaonanforever
Copy link
Author

@marcosnils
My solution is:
if (count > 0) {
// 原来代码
// out.write(buf, 0, count);
// count = 0;

        /**
         * 如果在out.write发生异常时,而当时count刚好是Buffer上限,则会在buf[count++]出现数组越界
         * Modify By Forever
         * 2014-5-20
         */
        try {
            out.write(buf, 0, count);
        } catch (IOException ex) {
            //System.out.println("发生异常,当前count:" + count);
            throw ex;
        } finally {
            count = 0;
        }
    }

But yours is better.
Thank you again!

@allen-servedio
Copy link

We also hit this while testing under high load. I see it did not make 2.5.0 - any chance that it can make 2.5.1? What is needed to move this forward?

@xetorthio
Copy link
Contributor

PR #651 fixes this.
I'm creating version 2.5.1 with this fix and I will release it now!

@xetorthio xetorthio added this to the 2.5.1 milestone May 29, 2014
xetorthio added a commit that referenced this issue May 29, 2014
Checks for buffer out of bounds before writing to the stream. Fixes #636
@xetorthio
Copy link
Contributor

Ok... version 2.5.1 released. Please upgrade! And sorry about that!

@allen-servedio
Copy link

No problem and thanks for the fast turn around. I have pulled it down. We
will be load testing next week with it.

Thanks again,
Allen
On May 29, 2014 11:52 AM, "Jonathan Leibiusky" notifications@github.com
wrote:

Ok... version 2.5.1 released. Please upgrade! And sorry about that!


Reply to this email directly or view it on GitHub
#636 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants