Skip to content

Commit 68e6344

Browse files
author
Zhen Li
authored
Merge pull request #249 from zhenlineo/1.0-buffer-flip
Fixed expect strust but get xx bug
2 parents 21306c2 + accb44b commit 68e6344

File tree

4 files changed

+75
-19
lines changed

4 files changed

+75
-19
lines changed

driver/src/main/java/org/neo4j/driver/internal/InternalSession.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,32 +108,38 @@ public boolean isOpen()
108108
public void close()
109109
{
110110
// Use atomic operation to protect from closing the connection twice (putting back to the pool twice).
111-
if( !isOpen.compareAndSet( true, false ) )
111+
if ( !isOpen.compareAndSet( true, false ) )
112112
{
113113
throw new ClientException( "This session has already been closed." );
114114
}
115-
else
115+
116+
if( !connection.isOpen() )
117+
{
118+
// the socket connection is already closed due to some error, cannot send more data
119+
connection.close();
120+
return;
121+
}
122+
123+
if ( currentTransaction != null )
116124
{
117-
if ( currentTransaction != null )
118-
{
119-
try
120-
{
121-
currentTransaction.close();
122-
}
123-
catch ( Throwable e )
124-
{
125-
// Best-effort
126-
}
127-
}
128125
try
129126
{
130-
connection.sync();
127+
currentTransaction.close();
131128
}
132-
finally
129+
catch ( Throwable e )
133130
{
134-
connection.close();
131+
// Best-effort
135132
}
136133
}
134+
try
135+
{
136+
connection.sync();
137+
}
138+
finally
139+
{
140+
connection.close();
141+
}
142+
137143
}
138144

139145
@Override

driver/src/main/java/org/neo4j/driver/internal/connector/socket/BufferingChunkedInput.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ else if ( buffer.remaining() >= 2 )
398398
* @param buffer The buffer to read into
399399
* @throws IOException
400400
*/
401-
private static void readNextPacket( ReadableByteChannel channel, ByteBuffer buffer ) throws IOException
401+
static void readNextPacket( ReadableByteChannel channel, ByteBuffer buffer ) throws IOException
402402
{
403403
assert !buffer.hasRemaining();
404404

@@ -408,11 +408,18 @@ private static void readNextPacket( ReadableByteChannel channel, ByteBuffer buff
408408
int read = channel.read( buffer );
409409
if ( read == -1 )
410410
{
411+
try
412+
{
413+
channel.close();
414+
}
415+
catch ( IOException e )
416+
{
417+
// best effort
418+
}
411419
throw new ClientException(
412420
"Connection terminated while receiving data. This can happen due to network " +
413421
"instabilities, or due to restarts of the database." );
414422
}
415-
buffer.flip();
416423
}
417424
catch ( ClosedByInterruptException e )
418425
{
@@ -429,6 +436,10 @@ private static void readNextPacket( ReadableByteChannel channel, ByteBuffer buff
429436
throw new ClientException(
430437
"Unable to process request: " + message + " buffer: \n" + BytePrinter.hex( buffer ), e );
431438
}
439+
finally
440+
{
441+
buffer.flip();
442+
}
432443
}
433444

434445
/**

driver/src/main/java/org/neo4j/driver/internal/connector/socket/SocketUtils.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ public static void blockingRead(ByteChannel channel, ByteBuffer buf) throws IOEx
4141
{
4242
if (channel.read( buf ) < 0)
4343
{
44+
try
45+
{
46+
channel.close();
47+
}
48+
catch ( IOException e )
49+
{
50+
// best effort
51+
}
4452
String bufStr = BytePrinter.hex( buf ).trim();
4553
throw new ClientException( String.format(
4654
"Connection terminated while receiving data. This can happen due to network " +
@@ -56,6 +64,14 @@ public static void blockingWrite(ByteChannel channel, ByteBuffer buf) throws IOE
5664
{
5765
if (channel.write( buf ) < 0)
5866
{
67+
try
68+
{
69+
channel.close();
70+
}
71+
catch ( IOException e )
72+
{
73+
// best effort
74+
}
5975
String bufStr = BytePrinter.hex( buf ).trim();
6076
throw new ClientException( String.format(
6177
"Connection terminated while sending data. This can happen due to network " +

driver/src/test/java/org/neo4j/driver/internal/connector/socket/BufferingChunkedInputTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import static org.hamcrest.CoreMatchers.equalTo;
3737
import static org.hamcrest.MatcherAssert.assertThat;
3838
import static org.junit.Assert.assertEquals;
39+
import static org.junit.Assert.assertFalse;
3940
import static org.junit.Assert.fail;
4041
import static org.mockito.Matchers.any;
4142
import static org.mockito.Mockito.mock;
@@ -491,6 +492,28 @@ public void shouldFailNicelyOnClosedConnections() throws IOException
491492
input.readByte();
492493
}
493494

495+
496+
@Test
497+
public void shouldKeepBufferCorrectWhenError() throws Throwable
498+
{
499+
// Given
500+
ReadableByteChannel channel = mock( ReadableByteChannel.class );
501+
when( channel.read( any( ByteBuffer.class ) ) ).thenReturn( -1 );
502+
ByteBuffer buffer = ByteBuffer.allocate( 8 );
503+
buffer.limit(0);
504+
505+
//Expect
506+
exception.expect( ClientException.class );
507+
exception.expectMessage( "Connection terminated while receiving data. This can happen due to network " +
508+
"instabilities, or due to restarts of the database." );
509+
// When
510+
BufferingChunkedInput.readNextPacket( channel, buffer );
511+
assertEquals( buffer.position(), 0 );
512+
assertEquals( buffer.limit(), 0 );
513+
assertEquals( buffer.capacity(), 8 );
514+
assertFalse( channel.isOpen() );
515+
}
516+
494517
private ReadableByteChannel fillPacket( int size, int value )
495518
{
496519
int[] ints = new int[size];
@@ -541,4 +564,4 @@ public void close() throws IOException
541564
};
542565
}
543566

544-
}
567+
}

0 commit comments

Comments
 (0)