diff --git a/src/main/java/io/vertx/core/file/impl/AsyncFileImpl.java b/src/main/java/io/vertx/core/file/impl/AsyncFileImpl.java index 1ba5e0601..a2e929fd4 100644 --- a/src/main/java/io/vertx/core/file/impl/AsyncFileImpl.java +++ b/src/main/java/io/vertx/core/file/impl/AsyncFileImpl.java @@ -400,22 +400,30 @@ public class AsyncFileImpl implements AsyncFile { }); } - private synchronized void handleBuffer(Buffer buff) { + + private void handleBuffer(Buffer buff) { + Handler handler; + synchronized (this) { + handler = this.handler; + } if (handler != null) { checkContext(); handler.handle(buff); } } - private synchronized void handleEnd() { - handler = null; + private void handleEnd() { + Handler endHandler; + synchronized (this) { + handler = null; + endHandler = this.endHandler; + } if (endHandler != null) { checkContext(); endHandler.handle(null); } } - private synchronized void doFlush(Handler> handler) { checkClosed(); context.executeBlockingInternal((Promise fut) -> { diff --git a/src/test/java/io/vertx/core/file/FileSystemTest.java b/src/test/java/io/vertx/core/file/FileSystemTest.java index cbfe331af..49c69b120 100644 --- a/src/test/java/io/vertx/core/file/FileSystemTest.java +++ b/src/test/java/io/vertx/core/file/FileSystemTest.java @@ -1564,6 +1564,25 @@ public class FileSystemTest extends VertxTestBase { await(); } + @Test + public void testReadStreamNoLock() throws Exception { + String fileName = "some-file.dat"; + int chunkSize = 16384; + int chunks = 1; + byte[] content = TestUtils.randomByteArray(chunkSize * chunks); + createFile(fileName, content); + vertx.fileSystem().open(testDir + pathSep + fileName, new OpenOptions(), onSuccess(rs -> { + rs.handler(buff -> { + assertFalse(Thread.holdsLock(rs)); + }); + rs.endHandler(v -> { + assertFalse(Thread.holdsLock(rs)); + testComplete(); + }); + })); + await(); + } + @Test @SuppressWarnings("unchecked") public void testPumpFileStreams() throws Exception {