Skip to content

Commit

Permalink
Don't leak temporary file during test and inital loading of lib. (#141)
Browse files Browse the repository at this point in the history
Motivation:

We did leak a file during our tests and also during the initial loading of the native lib

Modifications:

Use a tempory file / directory and so ensure the file is deleted

Result:

No leaked file
  • Loading branch information
normanmaurer authored Feb 1, 2022
1 parent a9d0256 commit 60f7b89
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.channels.Selector;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Locale;

Expand Down Expand Up @@ -59,14 +61,17 @@ final class Native {
PeerCredentials.class, java.io.FileDescriptor.class
);

File tmpDir = PlatformDependent.tmpdir();
Path tmpFile = tmpDir.toPath().resolve("netty_io_uring.tmp");
try {
// First, try calling a side-effect free JNI method to see if the library was already
// loaded by the application.
Native.createFile();
Native.createFile(tmpFile.toString());
} catch (UnsatisfiedLinkError ignore) {
// The library was not previously loaded, load it now.
loadNativeLibrary();
} finally {
tmpFile.toFile().delete();
try {
if (selector != null) {
selector.close();
Expand Down Expand Up @@ -256,23 +261,23 @@ private static boolean checkKernelVersion0(String kernelVersion) {
private static native boolean ioUringProbe(int ringFd, int[] ios);
private static native long[][] ioUringSetup(int entries);

public static native int ioUringEnter(int ringFd, int toSubmit, int minComplete, int flags);
static native int ioUringEnter(int ringFd, int toSubmit, int minComplete, int flags);

public static native void eventFdWrite(int fd, long value);
static native void eventFdWrite(int fd, long value);

public static FileDescriptor newBlockingEventFd() {
static FileDescriptor newBlockingEventFd() {
return new FileDescriptor(blockingEventFd());
}

public static native void ioUringExit(long submissionQueueArrayAddress, int submissionQueueRingEntries,
static native void ioUringExit(long submissionQueueArrayAddress, int submissionQueueRingEntries,
long submissionQueueRingAddress, int submissionQueueRingSize,
long completionQueueRingAddress, int completionQueueRingSize,
int ringFd);

private static native int blockingEventFd();

// for testing(it is only temporary)
public static native int createFile();
// for testing only!
static native int createFile(String name);

private static native int registerUnix();

Expand Down
10 changes: 7 additions & 3 deletions transport-native-io_uring/src/main/c/netty_io_uring_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,12 @@ static jobjectArray netty_io_uring_setup(JNIEnv *env, jclass clazz, jint entries
return array;
}

static jint netty_create_file(JNIEnv *env, jclass class) {
return open("io-uring-test.txt", O_RDWR | O_TRUNC | O_CREAT, 0644);
static jint netty_create_file(JNIEnv *env, jclass class, jstring filename) {
const char *file = (*env)->GetStringUTFChars(env, filename, 0);

int fd = open(file, O_RDWR | O_TRUNC | O_CREAT, 0644);
(*env)->ReleaseStringUTFChars(env, filename, file);
return fd;
}


Expand Down Expand Up @@ -592,7 +596,7 @@ static const JNINativeMethod method_table[] = {
{"ioUringSetup", "(I)[[J", (void *) netty_io_uring_setup},
{"ioUringProbe", "(I[I)Z", (void *) netty_io_uring_probe},
{"ioUringExit", "(JIJIJII)V", (void *) netty_io_uring_ring_buffer_exit},
{"createFile", "()I", (void *) netty_create_file},
{"createFile", "(Ljava/lang/String;)I", (void *) netty_create_file},
{"ioUringEnter", "(IIII)I", (void *) netty_io_uring_enter},
{"blockingEventFd", "()I", (void *) netty_epoll_native_blocking_event_fd},
{"eventFdWrite", "(IJ)V", (void *) netty_io_uring_eventFdWrite },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

import io.netty.channel.unix.FileDescriptor;

import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;

Expand All @@ -37,6 +39,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.io.TempDir;

public class NativeTest {

Expand All @@ -46,13 +49,14 @@ public static void loadJNI() {
}

@Test
public void canWriteFile() throws Exception {
public void canWriteFile(@TempDir Path tmpDir) throws Exception {
ByteBufAllocator allocator = new UnpooledByteBufAllocator(true);
final ByteBuf writeEventByteBuf = allocator.directBuffer(100);
final String inputString = "Hello World!";
writeEventByteBuf.writeCharSequence(inputString, Charset.forName("UTF-8"));

int fd = Native.createFile();
Path file = tmpDir.resolve("io_uring.tmp");
int fd = Native.createFile(file.toString());

RingBuffer ringBuffer = Native.createRingBuffer(32);
IOUringSubmissionQueue submissionQueue = ringBuffer.ioUringSubmissionQueue();
Expand Down

0 comments on commit 60f7b89

Please sign in to comment.