INT-4296: Fix APPEND_NO_FLUSH Timing Problem

JIRA: https://jira.spring.io/browse/INT-4296

The `FileState.close()` method correctly acquires the lock to prevent closing while
writing. However, with a short flush interval, the close could occur between state
creation and the write operation acquiring the lock.

Move the state creation to within the scope of the lock acquired by the writer.

(cherry picked from commit 404c2c5)
This commit is contained in:
Gary Russell
2017-06-19 09:00:47 -04:00
committed by Artem Bilan
parent 742cbb72a4
commit fec5d764a6

View File

@@ -536,13 +536,12 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand
if (append) {
final File fileToWriteTo = this.determineFileToWrite(resultFile, tempFile);
final FileState state = getFileState(fileToWriteTo, false);
WhileLockedProcessor whileLockedProcessor = new WhileLockedProcessor(this.lockRegistry,
fileToWriteTo.getAbsolutePath()) {
@Override
protected void whileLocked() throws IOException {
FileState state = getFileState(fileToWriteTo, false);
BufferedOutputStream bos = null;
try {
bos = state != null ? state.stream : createOutputStream(fileToWriteTo, true);
@@ -620,8 +619,6 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand
throws IOException {
final File fileToWriteTo = this.determineFileToWrite(resultFile, tempFile);
final FileState state = getFileState(fileToWriteTo, false);
final boolean append = FileExistsMode.APPEND.equals(this.fileExistsMode);
WhileLockedProcessor whileLockedProcessor = new WhileLockedProcessor(this.lockRegistry,
@@ -629,6 +626,7 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand
@Override
protected void whileLocked() throws IOException {
FileState state = getFileState(fileToWriteTo, false);
BufferedOutputStream bos = null;
try {
bos = state != null ? state.stream : createOutputStream(fileToWriteTo, append);
@@ -664,8 +662,6 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand
throws IOException {
final File fileToWriteTo = this.determineFileToWrite(resultFile, tempFile);
final FileState state = getFileState(fileToWriteTo, true);
final boolean append = FileExistsMode.APPEND.equals(this.fileExistsMode);
WhileLockedProcessor whileLockedProcessor = new WhileLockedProcessor(this.lockRegistry,
@@ -673,6 +669,7 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand
@Override
protected void whileLocked() throws IOException {
FileState state = getFileState(fileToWriteTo, true);
BufferedWriter writer = null;
try {
writer = state != null ? state.writer : createWriter(fileToWriteTo, append);