Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZeroCopyFileWriter leaks file handlers #93

Open
agori opened this issue Jan 20, 2020 · 0 comments
Open

ZeroCopyFileWriter leaks file handlers #93

agori opened this issue Jan 20, 2020 · 0 comments

Comments

@agori
Copy link

agori commented Jan 20, 2020

Basically that AuditFileWriter implementation opens new files on every attempt and never closes them. After some time the server crashes because of "too many open files".
I have to say this is a quite bewildering and severe bug for an audit library, whose purpose is, after all, to write to a file.

To me this method is clearly the culprit:

   @Override
    public ZeroCopyFileWriter write(String event) {
//        String realPath = FileHandlerUtil.generateOutputFilePath(path);

        String realPath = FileHandlerUtil.generateOutputFilePath(
    			path, 
    			FileHandlerUtil.generateAuditFileName());

        try {
            if (FileHandlerUtil.isFileAlreadyExists(realPath)) {
                randomAccessFile = new RandomAccessFile(realPath, CoreConstants.READ_WRITE);
            } else {
                randomAccessFile = new RandomAccessFile(new File(realPath), CoreConstants.READ_WRITE);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        fileChannel = randomAccessFile.getChannel();
        
        String str2 = event + CoreConstants.NEW_LINE;

        long numBytes = str2.getBytes().length;

        InputStream inputStream = new ByteArrayInputStream(str2.getBytes(Charset.forName("UTF-8")));

        try {

            // move the cursor to the end of the file
            randomAccessFile.seek(randomAccessFile.length());

            // obtain the a file channel from the RandomAccessFile
            try (ReadableByteChannel inputChannel = Channels.newChannel(inputStream);) {
                fileChannel.transferFrom(inputChannel, randomAccessFile.length(), numBytes);
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return this;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant