Repository navigation
Conversation
FileUtils.createTmpFile(InputStream, String, String) 每次调用都会通过 Files.createTempDirectory 新建一个 wxjava-temp 目录,调用方即使删除了返回的文件, 目录也会一直残留(deleteOnExit 只注册了文件),长期运行的服务会持续堆积空目录直到 inode 耗尽(Gitee I9DBP9、binarywang#1989)。小程序二维码/素材下载、开放平台备案素材下载、 视频号图片下载以及支付 GZIP 对账单/资金账单下载也各自在每次调用时新建临时目录。 - FileUtils 新增 getTmpDir(),首次调用时创建一个临时目录并在之后复用,目录被清理后会重新创建 - 小程序、开放平台、视频号的下载接口改用该共用目录 - 企业微信、小程序通过输入流上传时,在上传结束后删除内部创建的临时文件 - 支付 GZIP 对账单、资金账单解析完成后删除临时目录
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
FileUtils.createTmpFile(InputStream, String, String)每次调用都会用Files.createTempDirectory("wxjava-temp")新建一个目录,deleteOnExit只注册了文件,目录本身永远不会被删除。调用方即使用完后删掉了返回的文件,目录也会一直留在系统临时目录里,JVM 退出后也还在。长期运行的服务会持续堆积空目录,直到 inode 用完:同样的写法还出现在小程序
getQrCode(该方法会删除二维码文件,但目录仍残留)、getMedia,开放平台getIcpMedia,视频号getImg,以及支付 GZIP 对账单/资金账单下载。新的weixin-java-store模块在 #4117 里已经改为复用目录,这里把其余模块也改掉。另外,企业微信和小程序几个“通过输入流上传”的方法会先把流写成临时文件再上传,但上传后没有删除(公众号的
mediaUpload已经在finally里删除)。修改
FileUtils新增getTmpDir():首次调用时用Files.createTempDirectory创建一个目录,之后复用;目录被外部清理后会重新创建。createTmpFile(InputStream, String, String)改用这个目录getQrCode、getMedia,开放平台getIcpMedia,视频号getImg改用FileUtils.getTmpDir()WxCpMediaServiceImpl、WxCpTpMediaServiceImpl、WxCpExternalContactServiceImpl.uploadAttachment、WxMaMediaServiceImpl.uploadMedia、WxMaComplaintServiceImpl.uploadResponseImage的输入流重载在上传后删除临时文件,写法与WxMpMaterialServiceImpl.mediaUpload一致handleGzipBill、handleGzipFundFlow在finally中删除临时目录返回给调用方的文件(二维码、素材下载等)仍然由调用方负责删除,行为不变。
测试
新增测试并加入各模块的 testng.xml:
FileUtilsTest:两次createTmpFile位于同一目录;目录被删后getTmpDir()会重新创建WxMaTmpFileCleanupTest:用本地 HttpServer 调用两次getQrCode,两次下载位于同一目录且文件已删除;uploadMedia/uploadResponseImage输入流重载上传后临时文件已删除WxCpTmpFileCleanupTest:企业微信三个输入流上传方法上传后临时文件已删除WxPayGzipBillTmpFileTest:下载两次 GZIP 对账单后,系统临时目录中没有残留的bill*目录修改前这些测试共 8 个失败(例如
expected [wxjava-ma-FAKE_APP_ID2915741516220608339] but found [wxjava-ma-FAKE_APP_ID4789404034139378788]、Sets differ: expected [] but got [bill1006..., bill1236...]),修改后全部通过。common 38、cp 96、mp 72、pay 227、miniapp 16、open 24、channel 58 个测试全部通过;
mvn checkstyle:check和git diff --check通过。