chatgpt: clean up temporary files during uploadRepoFiles We noticed our gerrit instance running out of disk space and found a series of json files generated under `/tmp`. The structure looked like: /tmp/<repository_name><hash>.json We notice these temp files would get generated upon a CR change-merged event: "<timestamp & user> INFO com.googlesource.gerrit.plugins.aicodereview .listener.GerritListener : Processing event: com.google.gerrit.server .events.ChangeMergedEvent@commit-hash" The timestamp of this event would always match the timestamp of the json file created under `tmp`. These files also only get created for CRs merged in a repo where ai-code-review is created. We pin-pointed the issue to createTempFileWithContent in this plugin. There is a deleteOnExit() clause for this, but it does not seem to delete the files in all situations. Some of these files are quite large, as they contain the whole content of a CR. This change is a proposed fix for deleting a file in a finally clause in case it has not been removed yet and still exists. Change-Id: I1fb62f95c20b509cc756a0702d112e5eb44cc20a
diff --git a/src/main/java/com/googlesource/gerrit/plugins/aicodereview/mode/stateful/client/api/chatgpt/ChatGptAssistant.java b/src/main/java/com/googlesource/gerrit/plugins/aicodereview/mode/stateful/client/api/chatgpt/ChatGptAssistant.java index 4954814..ed96945 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/aicodereview/mode/stateful/client/api/chatgpt/ChatGptAssistant.java +++ b/src/main/java/com/googlesource/gerrit/plugins/aicodereview/mode/stateful/client/api/chatgpt/ChatGptAssistant.java
@@ -38,8 +38,11 @@ import com.googlesource.gerrit.plugins.aicodereview.mode.stateful.model.api.chatgpt.ChatGptResponse; import com.googlesource.gerrit.plugins.aicodereview.mode.stateful.model.api.chatgpt.ChatGptToolResources; import com.googlesource.gerrit.plugins.aicodereview.utils.HashUtils; + +import java.io.IOException; import java.net.URI; import java.nio.file.Path; +import java.nio.file.Files; import java.util.ArrayList; import java.util.List; import lombok.extern.slf4j.Slf4j; @@ -112,12 +115,28 @@ private String uploadRepoFiles() { String repoFiles = gitRepoFiles.getGitRepoFiles(config, change); - Path repoPath = - createTempFileWithContent(sanitizeFilename(change.getProjectName()), ".json", repoFiles); - ChatGptFiles chatGptFiles = new ChatGptFiles(config); - ChatGptFilesResponse chatGptFilesResponse = chatGptFiles.uploadFiles(repoPath); - - return chatGptFilesResponse.getId(); + Path repoPath = null; + try { + repoPath = + createTempFileWithContent(sanitizeFilename( + change.getProjectName()), + ".json", + repoFiles + ); + ChatGptFiles chatGptFiles = new ChatGptFiles(config); + ChatGptFilesResponse chatGptFilesResponse = chatGptFiles.uploadFiles(repoPath); + return chatGptFilesResponse.getId(); + } + finally { + if (repoPath != null) { + try { + Files.deleteIfExists(repoPath); + } + catch (IOException e) { + log.warn("Failed to delete temp file " + repoPath, e); + } + } + } } private String createAssistant(String vectorStoreId) {