-
-
Notifications
You must be signed in to change notification settings - Fork 429
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
[modbus] Fix error handling with DNS resolution / Unknown host errors #3490
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,14 +59,14 @@ | |
*/ | ||
@NonNullByDefault | ||
public class ModbusSlaveConnectionFactoryImpl | ||
extends BaseKeyedPooledObjectFactory<ModbusSlaveEndpoint, ModbusSlaveConnection> { | ||
extends BaseKeyedPooledObjectFactory<ModbusSlaveEndpoint, @Nullable ModbusSlaveConnection> { | ||
|
||
class PooledConnection extends DefaultPooledObject<ModbusSlaveConnection> { | ||
class PooledConnection extends DefaultPooledObject<@Nullable ModbusSlaveConnection> { | ||
|
||
private volatile long lastConnected; | ||
private volatile @Nullable ModbusSlaveEndpoint endpoint; | ||
|
||
public PooledConnection(ModbusSlaveConnection object) { | ||
public PooledConnection(@Nullable ModbusSlaveConnection object) { | ||
super(object); | ||
} | ||
|
||
|
@@ -97,6 +97,9 @@ public boolean maybeResetConnection(String activityName) { | |
long localLastConnected = lastConnected; | ||
|
||
ModbusSlaveConnection connection = getObject(); | ||
if (connection == null) { | ||
return false; | ||
} | ||
|
||
EndpointPoolConfiguration configuration = getEndpointPoolConfiguration(localEndpoint); | ||
long reconnectAfterMillis = configuration.getReconnectAfterMillis(); | ||
|
@@ -147,8 +150,8 @@ public ModbusSlaveConnectionFactoryImpl( | |
} | ||
|
||
@Override | ||
public ModbusSlaveConnection create(ModbusSlaveEndpoint endpoint) throws Exception { | ||
return endpoint.accept(new ModbusSlaveEndpointVisitor<ModbusSlaveConnection>() { | ||
public @Nullable ModbusSlaveConnection create(ModbusSlaveEndpoint endpoint) throws Exception { | ||
return endpoint.accept(new ModbusSlaveEndpointVisitor<@Nullable ModbusSlaveConnection>() { | ||
@Override | ||
public @Nullable ModbusSlaveConnection visit(ModbusSerialSlaveEndpoint modbusSerialSlavePoolingKey) { | ||
SerialConnection connection = new SerialConnection(modbusSerialSlavePoolingKey.getSerialParameters()); | ||
|
@@ -183,27 +186,34 @@ public ModbusSlaveConnection create(ModbusSlaveEndpoint endpoint) throws Excepti | |
} | ||
|
||
@Override | ||
public PooledObject<ModbusSlaveConnection> wrap(ModbusSlaveConnection connection) { | ||
public PooledObject<@Nullable ModbusSlaveConnection> wrap(@Nullable ModbusSlaveConnection connection) { | ||
return new PooledConnection(connection); | ||
} | ||
|
||
@Override | ||
public void destroyObject(ModbusSlaveEndpoint endpoint, @Nullable PooledObject<ModbusSlaveConnection> obj) { | ||
public void destroyObject(ModbusSlaveEndpoint endpoint, | ||
@Nullable PooledObject<@Nullable ModbusSlaveConnection> obj) { | ||
if (obj == null) { | ||
return; | ||
} | ||
logger.trace("destroyObject for connection {} and endpoint {} -> closing the connection", obj.getObject(), | ||
endpoint); | ||
obj.getObject().resetConnection(); | ||
ModbusSlaveConnection connection = obj.getObject(); | ||
if (connection == null) { | ||
return; | ||
} | ||
logger.trace("destroyObject for connection {} and endpoint {} -> closing the connection", connection, endpoint); | ||
connection.resetConnection(); | ||
} | ||
|
||
@Override | ||
public void activateObject(ModbusSlaveEndpoint endpoint, @Nullable PooledObject<ModbusSlaveConnection> obj) | ||
throws Exception { | ||
public void activateObject(ModbusSlaveEndpoint endpoint, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method raised NPE as well, but it was caught by the |
||
@Nullable PooledObject<@Nullable ModbusSlaveConnection> obj) throws Exception { | ||
if (obj == null) { | ||
return; | ||
} | ||
ModbusSlaveConnection connection = obj.getObject(); | ||
if (connection == null) { | ||
return; | ||
} | ||
try { | ||
EndpointPoolConfiguration config = getEndpointPoolConfiguration(endpoint); | ||
if (!connection.isConnected()) { | ||
|
@@ -226,7 +236,8 @@ public void activateObject(ModbusSlaveEndpoint endpoint, @Nullable PooledObject< | |
} | ||
|
||
@Override | ||
public void passivateObject(ModbusSlaveEndpoint endpoint, @Nullable PooledObject<ModbusSlaveConnection> obj) { | ||
public void passivateObject(ModbusSlaveEndpoint endpoint, | ||
@Nullable PooledObject<@Nullable ModbusSlaveConnection> obj) { | ||
if (obj == null) { | ||
return; | ||
} | ||
|
@@ -238,8 +249,9 @@ public void passivateObject(ModbusSlaveEndpoint endpoint, @Nullable PooledObject | |
} | ||
|
||
@Override | ||
public boolean validateObject(ModbusSlaveEndpoint key, @Nullable PooledObject<ModbusSlaveConnection> p) { | ||
boolean valid = p != null && p.getObject().isConnected(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one likely raised unhandled NullPointerException before -- with unknown implications. Might not be expected in apache commons pool2 |
||
public boolean validateObject(ModbusSlaveEndpoint key, @Nullable PooledObject<@Nullable ModbusSlaveConnection> p) { | ||
@SuppressWarnings("null") // p.getObject() cannot be null due to short-circuiting boolean condition | ||
boolean valid = p != null && p.getObject() != null && p.getObject().isConnected(); | ||
ModbusSlaveConnection slaveConnection = p != null ? p.getObject() : null; | ||
logger.trace("Validating endpoint {} connection {} -> {}", key, slaveConnection, valid); | ||
return valid; | ||
|
@@ -272,7 +284,7 @@ public EndpointPoolConfiguration getEndpointPoolConfiguration(ModbusSlaveEndpoin | |
.orElseGet(() -> defaultPoolConfigurationFactory.apply(endpoint)); | ||
} | ||
|
||
private void tryConnect(ModbusSlaveEndpoint endpoint, PooledObject<ModbusSlaveConnection> obj, | ||
private void tryConnect(ModbusSlaveEndpoint endpoint, PooledObject<@Nullable ModbusSlaveConnection> obj, | ||
ModbusSlaveConnection connection, EndpointPoolConfiguration config) throws Exception { | ||
if (connection.isConnected()) { | ||
return; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the thing IDE did not catch -- the anonymous
ModbusSlaveEndpointVisitor
methods were returning@Nullable ModbusSlaveConnection
even though the generic type argument was<ModbusSlaveConnection>