Skip to content

Commit

Permalink
Fix pattern how Lock.unlock() is used
Browse files Browse the repository at this point in the history
Related to https://build.spring.io/browse/INT-MAIN-84/

The `lock.unlock()` must be called in the `finally` block of the
nested `try..catch`, not in the outer which may just fail on `lock.lockInterruptibly()`
in which case there is just not going to be anything we can `unlock()` in the end

**Cherry-pick to `5.4.x` & `5.3.x`**
  • Loading branch information
artembilan authored and garyrussell committed Jul 19, 2021
1 parent dbc8af9 commit 5ec8913
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -537,19 +537,19 @@ protected void handleMessageInternal(Message<?> message) {
boolean noOutput = true;
try {
lock.lockInterruptibly();
try {
noOutput = processMessageForGroup(message, correlationKey, groupIdUuid, lock);
}
finally {
if (noOutput || !this.releaseLockBeforeSend) {
lock.unlock();
}
}
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new MessageHandlingException(message, "Interrupted getting lock in the [" + this + ']', e);
}
try {
noOutput = processMessageForGroup(message, correlationKey, groupIdUuid, lock);
}
finally {
if (noOutput || !this.releaseLockBeforeSend) {
lock.unlock();
}
}
}

private boolean processMessageForGroup(Message<?> message, Object correlationKey, UUID groupIdUuid, Lock lock) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ else if (payload instanceof String) {
/**
* Retrieves the File instance from the {@link FileHeaders#ORIGINAL_FILE}
* header if available. If the value is not a File instance or a String
* representation of a file path, this will return <code>null</code>.
* representation of a file path, this will return {@code null}.
*/
private File retrieveOriginalFileFromHeader(Message<?> message) {
Object value = message.getHeaders().get(FileHeaders.ORIGINAL_FILE);
Expand Down Expand Up @@ -1076,15 +1076,15 @@ private boolean close() {
catch (IOException e) {
// ignore
}
finally {
this.lock.unlock();
}
return true;
}
catch (InterruptedException e1) {
Thread.currentThread().interrupt();
return false;
}
finally {
this.lock.unlock();
}
}

}
Expand Down Expand Up @@ -1175,6 +1175,7 @@ private static final class DefaultFlushPredicate implements MessageFlushPredicat
@Override
public boolean shouldFlush(String fileAbsolutePath, long firstWrite, long lastWrite,
Message<?> triggerMessage) {

Pattern pattern;
if (triggerMessage.getPayload() instanceof String) {
pattern = Pattern.compile((String) triggerMessage.getPayload());
Expand Down

0 comments on commit 5ec8913

Please sign in to comment.