Skip to content

Commit 3e0a3fa

Browse files
garyrussellartembilan
authored andcommitted
INT-4560: Fix Race in FileSystemPersistentAOFLF
JIRA: https://jira.spring.io/browse/INT-4560 Reproduced and tested with ```java @SpringBootApplication public class So53521593Application { private static final Logger logger = LoggerFactory.getLogger(So53521593Application.class); public static void main(String[] args) { SpringApplication.run(So53521593Application.class, args); } @bean public IntegrationFlow flow() { ExecutorService exec = Executors.newFixedThreadPool(10); return IntegrationFlows.from(Files.inboundAdapter(new File("/tmp/foo")).filter( new MyFilter(new SimpleMetadataStore(), "foo")), e -> e.poller(Pollers.fixedDelay(5, TimeUnit.SECONDS) .maxMessagesPerPoll(10))) .channel(MessageChannels.executor(exec)) .<File>handle((p, h) -> { try { p.delete(); logger.info(p.toString()); Thread.sleep(10_000); } catch (InterruptedException e1) { Thread.currentThread().interrupt(); } return null; }) .get(); } } class MyFilter extends FileSystemPersistentAcceptOnceFileListFilter { public MyFilter(ConcurrentMetadataStore store, String prefix) { super(store, prefix); } @OverRide protected long modified(File file) { long modified = super.modified(file); System.out.println(modified); return modified; } } ``` **cherry-pick to 5.0.x, 4.3.x** (cherry picked from commit 39fef2c) * Upgrade to Spring Security `5.0.10`
1 parent 9d276ce commit 3e0a3fa

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ subprojects { subproject ->
151151
springDataMongoVersion = '2.0.12.RELEASE'
152152
springDataRedisVersion = '2.0.12.RELEASE'
153153
springGemfireVersion = '2.0.12.RELEASE'
154-
springSecurityVersion = '5.0.8.RELEASE'
154+
springSecurityVersion = '5.0.10.RELEASE'
155155
springSocialTwitterVersion = '1.1.2.RELEASE'
156156
springRetryVersion = '1.2.2.RELEASE'
157157
springVersion = project.hasProperty('springVersion') ? project.springVersion : '5.0.11.RELEASE'

spring-integration-file/src/main/java/org/springframework/integration/file/filters/AbstractPersistentAcceptOnceFileListFilter.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,27 @@ public boolean accept(F file) {
7777
String oldValue = this.store.putIfAbsent(key, newValue);
7878
if (oldValue == null) { // not in store
7979
flushIfNeeded();
80-
return true;
80+
return fileStillExists(file);
8181
}
8282
// same value in store
8383
if (!isEqual(file, oldValue) && this.store.replace(key, oldValue, newValue)) {
8484
flushIfNeeded();
85-
return true;
85+
return fileStillExists(file);
8686
}
8787
return false;
8888
}
8989
}
9090

91+
/**
92+
* Check if the file still exists; default implementation returns true.
93+
* @param file the file.
94+
* @return true if the filter should return true.
95+
* @since 4.3.19
96+
*/
97+
protected boolean fileStillExists(F file) {
98+
return true;
99+
}
100+
91101
/**
92102
* {@inheritDoc}
93103
* @since 4.0.4

spring-integration-file/src/main/java/org/springframework/integration/file/filters/FileSystemPersistentAcceptOnceFileListFilter.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,15 @@ protected String fileName(File file) {
4242
return file.getAbsolutePath();
4343
}
4444

45+
/**
46+
* Check that the file still exists, to avoid a race condition when multi-threaded and
47+
* another thread removed the file while we were waiting for the lock.
48+
* @since 4.3.19
49+
*/
50+
@Override
51+
protected boolean fileStillExists(File file) {
52+
return file.exists();
53+
}
54+
55+
4556
}

spring-integration-file/src/test/java/org/springframework/integration/file/filters/PersistentAcceptOnceFileListFilterTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2015 the original author or authors.
2+
* Copyright 2013-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -126,6 +126,11 @@ public void testRollbackFileSystem() throws Exception {
126126
new SimpleMetadataStore(), "rollback:");
127127
File[] files = new File[] {new File("foo"), new File("bar"), new File("baz")};
128128
List<File> passed = filter.filterFiles(files);
129+
assertEquals(0, passed.size());
130+
for (File file : files) {
131+
file.createNewFile();
132+
}
133+
passed = filter.filterFiles(files);
129134
assertTrue(Arrays.equals(files, passed.toArray()));
130135
List<File> now = filter.filterFiles(files);
131136
assertEquals(0, now.size());
@@ -137,6 +142,9 @@ public void testRollbackFileSystem() throws Exception {
137142
now = filter.filterFiles(files);
138143
assertEquals(0, now.size());
139144
filter.close();
145+
for (File file : files) {
146+
file.delete();
147+
}
140148
}
141149

142150
@Test

0 commit comments

Comments
 (0)