Skip to content

Commit 922c282

Browse files
fix fd leak for io_uring transport
1 parent 620d51d commit 922c282

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

vertx-core/src/main/java/io/vertx/core/http/impl/http1/VertxHttpResponseEncoder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.netty.handler.codec.http.LastHttpContent;
2525
import io.vertx.core.http.impl.headers.Http1xHeaders;
2626
import io.vertx.core.impl.SysProps;
27+
import io.vertx.core.net.impl.UncloseableFileRegion;
2728

2829
/**
2930
* {@link io.netty.handler.codec.http.HttpResponseEncoder} which forces the usage of direct buffers for max performance.
@@ -60,7 +61,7 @@ public boolean acceptOutboundMessage(Object msg) throws Exception {
6061
msgClazz == VertxAssembledHttpResponse.class ||
6162
msgClazz == DefaultHttpContent.class ||
6263
msgClazz == VertxLastHttpContent.class ||
63-
msgClazz == DefaultFileRegion.class) {
64+
msgClazz == UncloseableFileRegion.class) {
6465
return true;
6566
}
6667
// Netty slow-path
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2011-2026 Contributors to the Eclipse Foundation
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10+
*/
11+
package io.vertx.core.net.impl;
12+
13+
import io.netty.channel.DefaultFileRegion;
14+
15+
import java.io.File;
16+
import java.nio.channels.FileChannel;
17+
18+
/**
19+
* A file region that does close the underlying resource, letting the file user control the lifecycle of
20+
* the file descriptor.
21+
*
22+
* @author <a href="mailto:dreamlike.vertx@gmail.com">MengYang Li</a>
23+
*/
24+
public class UncloseableFileRegion extends DefaultFileRegion {
25+
public UncloseableFileRegion(FileChannel fileChannel, long position, long count) {
26+
super(fileChannel, position, count);
27+
}
28+
29+
public UncloseableFileRegion(File file, long position, long count) {
30+
super(file, position, count);
31+
}
32+
33+
@Override
34+
protected void deallocate() {
35+
36+
}
37+
}

vertx-core/src/main/java/io/vertx/core/net/impl/VertxConnection.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -508,17 +508,11 @@ public boolean writeQueueFull() {
508508
*/
509509
private void sendFileRegion(FileChannel fc, long offset, long length, ChannelPromise writeFuture) {
510510
if (length < MAX_REGION_SIZE) {
511-
FileRegion region = new DefaultFileRegion(fc, offset, length);
512-
// Retain explicitly this file region so the underlying channel is not closed by the NIO channel when it
513-
// as been sent as the caller can need it again
514-
region.retain();
511+
FileRegion region = new UncloseableFileRegion(fc, offset, length);
515512
writeToChannel(region, writeFuture);
516513
} else {
517514
ChannelPromise promise = chctx.newPromise();
518-
FileRegion region = new DefaultFileRegion(fc, offset, MAX_REGION_SIZE);
519-
// Retain explicitly this file region so the underlying channel is not closed by the NIO channel when it
520-
// as been sent as we need it again
521-
region.retain();
515+
FileRegion region = new UncloseableFileRegion(fc, offset, MAX_REGION_SIZE);
522516
writeToChannel(region, promise);
523517
promise.addListener(future -> {
524518
if (future.isSuccess()) {

0 commit comments

Comments
 (0)