#vulnerable code 
                                @Override
    public Raft.InstallSnapshotResponse installSnapshot(Raft.InstallSnapshotRequest request) {
        LOG.info("Receive installSnapshot request from server {} " +
                        "in term {} (my term was {})",
                request.getServerId(), request.getTerm(),
                raftNode.getCurrentTerm());
        raftNode.getLock().lock();
        try {
            Raft.InstallSnapshotResponse.Builder responseBuilder = Raft.InstallSnapshotResponse.newBuilder();
            responseBuilder.setTerm(raftNode.getCurrentTerm());
            if (request.getTerm() < raftNode.getCurrentTerm()) {
                return responseBuilder.build();
            }
            raftNode.stepDown(request.getTerm());
            if (raftNode.getLeaderId() == 0) {
                raftNode.setLeaderId(request.getServerId());
            }

            // write snapshot data to local
            String tmpSnapshotDir = raftNode.getSnapshot().getSnapshotDir() + ".tmp";
            File file = new File(tmpSnapshotDir);
            if (file.exists() && request.getIsFirst()) {
                file.delete();
                file.mkdir();
            }
            if (request.getIsFirst()) {
                raftNode.getSnapshot().updateMetaData(tmpSnapshotDir,
                        request.getSnapshotMetaData().getLastIncludedIndex(),
                        request.getSnapshotMetaData().getLastIncludedTerm());
            }
            // write to file
            RandomAccessFile randomAccessFile = null;
            try {
                String currentDataFileName = tmpSnapshotDir + File.separator
                        + "data" + File.separator + request.getFileName();
                File currentDataFile = new File(currentDataFileName);
                if (!currentDataFile.exists()) {
                    currentDataFile.createNewFile();
                }
                randomAccessFile = RaftFileUtils.openFile(
                        tmpSnapshotDir + File.separator + "data",
                        request.getFileName(), "rw");
                randomAccessFile.skipBytes((int) request.getOffset());
                randomAccessFile.write(request.getData().toByteArray());
                if (randomAccessFile != null) {
                    try {
                        randomAccessFile.close();
                        randomAccessFile = null;
                    } catch (Exception ex2) {
                        LOG.warn("close failed");
                    }
                }
                // move tmp dir to snapshot dir if this is the last package
                File snapshotDirFile = new File(raftNode.getSnapshot().getSnapshotDir());
                if (snapshotDirFile.exists()) {
                    snapshotDirFile.delete();
                }
                FileUtils.moveDirectory(new File(tmpSnapshotDir), snapshotDirFile);
                responseBuilder.setSuccess(true);
            } catch (IOException ex) {
                LOG.warn("io exception, msg={}", ex.getMessage());
            } finally {
                RaftFileUtils.closeFile(randomAccessFile);
            }
            return responseBuilder.build();
        } finally {
            raftNode.getLock().unlock();
        }
    } 
                            #location 6 
                            #vulnerability type THREAD_SAFETY_VIOLATION
#fixed code
                                @Override
    public Raft.InstallSnapshotResponse installSnapshot(Raft.InstallSnapshotRequest request) {
        raftNode.getLock().lock();
        try {
            Raft.InstallSnapshotResponse.Builder responseBuilder = Raft.InstallSnapshotResponse.newBuilder();
            responseBuilder.setTerm(raftNode.getCurrentTerm());
            if (request.getTerm() < raftNode.getCurrentTerm()) {
                return responseBuilder.build();
            }
            raftNode.stepDown(request.getTerm());
            if (raftNode.getLeaderId() == 0) {
                raftNode.setLeaderId(request.getServerId());
            }

            // write snapshot data to local
            String tmpSnapshotDir = raftNode.getSnapshot().getSnapshotDir() + ".tmp";
            File file = new File(tmpSnapshotDir);
            if (file.exists() && request.getIsFirst()) {
                file.delete();
                file.mkdir();
            }
            if (request.getIsFirst()) {
                raftNode.getSnapshot().updateMetaData(tmpSnapshotDir,
                        request.getSnapshotMetaData().getLastIncludedIndex(),
                        request.getSnapshotMetaData().getLastIncludedTerm());
            }
            // write to file
            RandomAccessFile randomAccessFile = null;
            try {
                String currentDataFileName = tmpSnapshotDir + File.separator
                        + "data" + File.separator + request.getFileName();
                File currentDataFile = new File(currentDataFileName);
                if (!currentDataFile.exists()) {
                    currentDataFile.createNewFile();
                }
                randomAccessFile = RaftFileUtils.openFile(
                        tmpSnapshotDir + File.separator + "data",
                        request.getFileName(), "rw");
                randomAccessFile.skipBytes((int) request.getOffset());
                randomAccessFile.write(request.getData().toByteArray());
                if (randomAccessFile != null) {
                    try {
                        randomAccessFile.close();
                        randomAccessFile = null;
                    } catch (Exception ex2) {
                        LOG.warn("close failed");
                    }
                }
                // move tmp dir to snapshot dir if this is the last package
                File snapshotDirFile = new File(raftNode.getSnapshot().getSnapshotDir());
                if (snapshotDirFile.exists()) {
                    snapshotDirFile.delete();
                }
                FileUtils.moveDirectory(new File(tmpSnapshotDir), snapshotDirFile);
                responseBuilder.setSuccess(true);
            } catch (IOException ex) {
                LOG.warn("io exception, msg={}", ex.getMessage());
            } finally {
                RaftFileUtils.closeFile(randomAccessFile);
            }
            LOG.info("Receive installSnapshot request from server {} " +
                            "in term {} (my term is {}), success={}",
                    request.getServerId(), request.getTerm(),
                    raftNode.getCurrentTerm(), responseBuilder.getSuccess());
            return responseBuilder.build();
        } finally {
            raftNode.getLock().unlock();
        }
    }