@@ -1049,8 +1049,12 @@ def send_command(self, command, retry=True, binary=False):
1049
1049
reset = True
1050
1050
connection .close (reset )
1051
1051
if self ._should_retry (retry , connection , pne ):
1052
+ if pne .when == proto .ERROR_ON_SEND or pne .when == proto .EMPTY_RESPONSE :
1053
+ # For empty response, just try once more because now we reset
1054
+ # the connection, and should work if the other end is alive.
1055
+ retry = False
1052
1056
logging .info ("Exception while sending command." , exc_info = True )
1053
- response = self .send_command (command , binary = binary )
1057
+ response = self .send_command (command , retry , binary = binary )
1054
1058
else :
1055
1059
logging .exception (
1056
1060
"Exception while sending command." )
@@ -1107,7 +1111,8 @@ def _create_connection_guard(self, connection):
1107
1111
return GatewayConnectionGuard (self , connection )
1108
1112
1109
1113
def _should_retry (self , retry , connection , pne = None ):
1110
- return pne and pne .when == proto .ERROR_ON_SEND
1114
+ return retry and pne and (
1115
+ pne .when == proto .ERROR_ON_SEND or pne .when == proto .EMPTY_RESPONSE )
1111
1116
1112
1117
def close (self ):
1113
1118
"""Closes all currently opened connections.
@@ -1254,10 +1259,12 @@ def send_command(self, command):
1254
1259
# Happens when a the other end is dead. There might be an empty
1255
1260
# answer before the socket raises an error.
1256
1261
if answer .strip () == "" :
1257
- raise Py4JNetworkError ("Answer from Java side is empty" )
1262
+ raise Py4JNetworkError ("Answer from Java side is empty" , when = proto . EMPTY_RESPONSE )
1258
1263
return answer
1259
1264
except Exception as e :
1260
1265
logger .info ("Error while receiving." , exc_info = True )
1266
+ if isinstance (e , Py4JNetworkError ) and e .when == proto .EMPTY_RESPONSE :
1267
+ raise
1261
1268
raise Py4JNetworkError (
1262
1269
"Error while receiving" , e , proto .ERROR_ON_RECEIVE )
1263
1270
0 commit comments