Skip to content

Commit

Permalink
[modbus] Workaround for nrjavaserial issues: do not disconnect serial (
Browse files Browse the repository at this point in the history
…#2272)

Workaround for #1842.
Typically serial connections are disconnected and reconnected on IO
errors. This has turned out to be problematic with the recent
nrjavaserial updates brought by OH3 (locking behaviour of nrjavaserial
seems to be prone to deadlocks).

We workaround the issues by trying to keep the serial connection open
as long as possible.

Signed-off-by: Sami Salonen <[email protected]>
  • Loading branch information
ssalonen authored Apr 7, 2021
1 parent 9493137 commit b1b5843
Showing 1 changed file with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -627,9 +627,15 @@ private <R, C extends ModbusResultCallback, F extends ModbusFailureCallback<R>,
"Last try {} failed when executing request ({}). Aborting. Error was I/O error, so reseting the connection. Error details: {} {} [operation ID {}]",
tryIndex, request, e.getClass().getName(), e.getMessage(), operationId);
}
// Invalidate connection, and empty (so that new connection is acquired before new retry)
timer.connection.timeConsumer(c -> invalidate(endpoint, c), connection);
connection = Optional.empty();
if (endpoint instanceof ModbusSerialSlaveEndpoint) {
// Workaround for https://github.com/openhab/openhab-core/issues/1842
// Avoid disconnect/re-connect serial interfaces
logger.debug("Skipping invalidation of serial connection to workaround openhab-core#1842.");
} else {
// Invalidate connection, and empty (so that new connection is acquired before new retry)
timer.connection.timeConsumer(c -> invalidate(endpoint, c), connection);
connection = Optional.empty();
}
continue;
} catch (ModbusIOException e) {
lastError.set(new ModbusSlaveIOExceptionImpl(e));
Expand All @@ -644,9 +650,15 @@ private <R, C extends ModbusResultCallback, F extends ModbusFailureCallback<R>,
"Last try {} failed when executing request ({}). Aborting. Error was I/O error, so reseting the connection. Error details: {} {} [operation ID {}]",
tryIndex, request, e.getClass().getName(), e.getMessage(), operationId);
}
// Invalidate connection, and empty (so that new connection is acquired before new retry)
timer.connection.timeConsumer(c -> invalidate(endpoint, c), connection);
connection = Optional.empty();
if (endpoint instanceof ModbusSerialSlaveEndpoint) {
// Workaround for https://github.com/openhab/openhab-core/issues/1842
// Avoid disconnect/re-connect serial interfaces
logger.debug("Skipping invalidation of serial connection to workaround openhab-core#1842.");
} else {
// Invalidate connection, and empty (so that new connection is acquired before new retry)
timer.connection.timeConsumer(c -> invalidate(endpoint, c), connection);
connection = Optional.empty();
}
continue;
} catch (ModbusSlaveException e) {
lastError.set(new ModbusSlaveErrorResponseExceptionImpl(e));
Expand Down Expand Up @@ -674,9 +686,15 @@ private <R, C extends ModbusResultCallback, F extends ModbusFailureCallback<R>,
"Last try {} failed when executing request ({}). Aborting. The response did not match the request. Reseting the connection. Error details: {} {} [operation ID {}]",
tryIndex, request, e.getClass().getName(), e.getMessage(), operationId);
}
// Invalidate connection, and empty (so that new connection is acquired before new retry)
timer.connection.timeConsumer(c -> invalidate(endpoint, c), connection);
connection = Optional.empty();
if (endpoint instanceof ModbusSerialSlaveEndpoint) {
// Workaround for https://github.com/openhab/openhab-core/issues/1842
// Avoid disconnect/re-connect serial interfaces
logger.debug("Skipping invalidation of serial connection to workaround openhab-core#1842.");
} else {
// Invalidate connection, and empty (so that new connection is acquired before new retry)
timer.connection.timeConsumer(c -> invalidate(endpoint, c), connection);
connection = Optional.empty();
}
continue;
} catch (ModbusException e) {
lastError.set(e);
Expand Down Expand Up @@ -717,9 +735,15 @@ private <R, C extends ModbusResultCallback, F extends ModbusFailureCallback<R>,
} catch (InterruptedException e) {
logger.warn("Poll task was canceled -- not executing/proceeding with the poll: {} [operation ID {}]",
e.getMessage(), operationId);
// Invalidate connection, and empty (so that new connection is acquired before new retry)
timer.connection.timeConsumer(c -> invalidate(endpoint, c), connection);
connection = Optional.empty();
if (endpoint instanceof ModbusSerialSlaveEndpoint) {
// Workaround for https://github.com/openhab/openhab-core/issues/1842
// Avoid disconnect/re-connect serial interfaces
logger.debug("Skipping invalidation of serial connection to workaround openhab-core#1842.");
} else {
// Invalidate connection, and empty (so that new connection is acquired before new retry)
timer.connection.timeConsumer(c -> invalidate(endpoint, c), connection);
connection = Optional.empty();
}
} finally {
timer.connection.timeConsumer(c -> returnConnection(endpoint, c), connection);
logger.trace("Connection was returned to the pool, ending operation [operation ID {}]", operationId);
Expand Down

0 comments on commit b1b5843

Please sign in to comment.