Skip to content

Commit

Permalink
The AsyncFile implementation will continue to read the file when the …
Browse files Browse the repository at this point in the history
…handler is set to null, instead it should stop reading. In addition report async file exception to the context when no exception handler is set.
  • Loading branch information
vietj committed May 23, 2024
1 parent bfe6e37 commit 6672988
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/main/java/io/vertx/core/file/impl/AsyncFileImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,7 @@ private void handleException(Throwable t) {
if (exceptionHandler != null && t instanceof Exception) {
exceptionHandler.handle(t);
} else {
log.error("Unhandled exception", t);

context.reportException(t);
}
}

Expand Down Expand Up @@ -398,6 +397,9 @@ private void doRead() {
}

private synchronized void doRead(ByteBuffer bb) {
if (handler == null) {
return;
}
Buffer buff = Buffer.buffer(readBufferSize);
int readSize = (int) Math.min((long)readBufferSize, readLength);
bb.limit(readSize);
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/io/vertx/core/http/Http2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
import org.junit.Test;

import javax.net.ssl.SSLHandshakeException;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -1129,4 +1133,41 @@ private void testUnsupportedAlpnVersion(SSLEngineOptions engine, boolean accept)
});
await();
}

@Test
public void testSendFileCancellation() throws Exception {

Path webroot = Files.createTempDirectory("webroot");
File res = new File(webroot.toFile(), "large.dat");
RandomAccessFile f = new RandomAccessFile(res, "rw");
f.setLength(1024 * 1024);

AtomicInteger errors = new AtomicInteger();
vertx.getOrCreateContext().exceptionHandler(err -> {
errors.incrementAndGet();
});

server.requestHandler(request -> {
request
.response()
.sendFile(res.getAbsolutePath())
.onComplete(onFailure(ar -> {
assertEquals(0, errors.get());
testComplete();
}));
});

startServer();

client.request(requestOptions)
.onComplete(onSuccess(req -> {
req.send().onComplete(onSuccess(resp -> {
assertEquals(200, resp.statusCode());
assertEquals(HttpVersion.HTTP_2, resp.version());
req.connection().close();
}));
}));

await();
}
}

0 comments on commit 6672988

Please sign in to comment.