Skip to content

Commit d2ef070

Browse files
author
taylor.smock
committed
Fix #22160: Retry on SocketException: Unexpected end of file from server
This allows additional exceptions to force a retry. Specifically, the following subclasses of `SocketException` were considered: * `BindException` -- shouldn't be thrown, "Signals that an error occurred while attempting to bind a socket to a local address and port. Typically, the port is in use, or the requested local address could not be assigned." This will be raised if it is ever encountered. * `ConnectException`-- replacing that here, "Signals that an error occurred while attempting to connect a socket to a remote address and port. Typically, the connection was refused remotely (e.g., no process is listening on the remote address/port)." * `ConnectionResetException` -- "Thrown to indicate a connection reset" This seems to be a Java internal class. It is rethrown. * `NoRouteToHostException` -- "Signals that an error occurred while attempting to connect a socket to a remote address and port. Typically, the remote host cannot be reached because of an intervening firewall, or if an intermediate router is down." * `PortUnreachableException` -- "Signals that an ICMP Port Unreachable message has been received on a connected datagram." `SocketException` is only thrown in one location in the JDK source code, but just in case someone decided to throw the exception in a library, we additionally check that the message matches that from the JDK source. This is, unfortunately, a bit more fragile than it should be. git-svn-id: https://josm.openstreetmap.de/svn/trunk@18532 0c6e7542-c601-0410-84e7-c038aed88b3b
1 parent c79ca72 commit d2ef070

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/org/openstreetmap/josm/io/OsmApi.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import java.net.ConnectException;
1313
import java.net.HttpURLConnection;
1414
import java.net.MalformedURLException;
15+
import java.net.NoRouteToHostException;
16+
import java.net.PortUnreachableException;
17+
import java.net.SocketException;
1518
import java.net.SocketTimeoutException;
1619
import java.net.URL;
1720
import java.nio.charset.StandardCharsets;
@@ -768,8 +771,18 @@ protected final String sendRequest(String requestMethod, String urlSuffix, Strin
768771
default:
769772
throw new OsmApiException(retCode, errorHeader, errorBody);
770773
}
771-
} catch (SocketTimeoutException | ConnectException e) {
772-
if (retries-- > 0) {
774+
} catch (SocketException | SocketTimeoutException e) {
775+
/*
776+
* See #22160. While it is only thrown once in JDK sources, it does have subclasses.
777+
* We check for those first, the explicit non-child exception, and then for the message.
778+
*/
779+
boolean validException = e instanceof SocketTimeoutException
780+
|| e instanceof ConnectException
781+
|| e instanceof NoRouteToHostException
782+
|| e instanceof PortUnreachableException
783+
|| (e.getClass().equals(SocketException.class) &&
784+
"Unexpected end of file from server".equals(e.getMessage()));
785+
if (retries-- > 0 && validException) {
773786
continue;
774787
}
775788
throw new OsmTransferException(e);

0 commit comments

Comments
 (0)