Skip to content

Commit 7572f5c

Browse files
author
Zhen
committed
Fixed expect strust but get xx bug by flipping a buffer back after connection error
Close the socket channel directly if server already mark the end of the channel
1 parent 21306c2 commit 7572f5c

File tree

3 files changed

+50
-17
lines changed

3 files changed

+50
-17
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: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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 " +

0 commit comments

Comments
 (0)