From 052da8e110137ce6ec42fb4dfc7b9f1ffd228932 Mon Sep 17 00:00:00 2001 From: sunnylqm Date: Thu, 2 Jul 2026 10:41:01 +0800 Subject: [PATCH] feat: optimize cleanup logic and fix static resource bloat & fd leaks --- .../modules/update/DownloadTask.java | 12 +++- harmony/pushy/src/main/ets/DownloadTask.ts | 63 ++++++++++++++----- harmony/pushy/src/main/ets/UpdateContext.ts | 2 +- ios/RCTPushy/RCTPushy.mm | 2 +- 4 files changed, 58 insertions(+), 21 deletions(-) diff --git a/android/src/main/java/cn/reactnative/modules/update/DownloadTask.java b/android/src/main/java/cn/reactnative/modules/update/DownloadTask.java index 96db31bf..c75dfb82 100644 --- a/android/src/main/java/cn/reactnative/modules/update/DownloadTask.java +++ b/android/src/main/java/cn/reactnative/modules/update/DownloadTask.java @@ -258,6 +258,9 @@ private void doFullPatch() throws IOException { } } + if (params.targetFile.exists()) { + params.targetFile.delete(); + } } private void doPatchFromApk() throws IOException, JSONException { @@ -300,6 +303,9 @@ private void doPatchFromApk() throws IOException, JSONException { } bundledResourceCopier.copyFromResource(copyList, contents.copyCrcs); + if (params.targetFile.exists()) { + params.targetFile.delete(); + } } private void doPatchFromPpk() throws IOException, JSONException { @@ -326,7 +332,9 @@ private void doPatchFromPpk() throws IOException, JSONException { contents.copyTos.toArray(new String[0]), contents.deletes.toArray(new String[0]) ); - + if (params.targetFile.exists()) { + params.targetFile.delete(); + } } private void doCleanUp() { @@ -334,7 +342,7 @@ private void doCleanUp() { params.unzipDirectory.getAbsolutePath(), params.hash, params.originHash, - 7 + 3 ); } diff --git a/harmony/pushy/src/main/ets/DownloadTask.ts b/harmony/pushy/src/main/ets/DownloadTask.ts index 0aea177a..cb43a57d 100644 --- a/harmony/pushy/src/main/ets/DownloadTask.ts +++ b/harmony/pushy/src/main/ets/DownloadTask.ts @@ -442,6 +442,13 @@ export class DownloadTask { await this.downloadFile(params); await this.recreateDirectory(params.unzipDirectory); await zlib.decompressFile(params.targetFile, params.unzipDirectory); + try { + if (fileIo.accessSync(params.targetFile)) { + await fileIo.unlink(params.targetFile); + } + } catch (e) { + console.error('Failed to delete temporary zip file after decompression:', e); + } } private async doPatchFromApp(params: DownloadTaskParams): Promise { @@ -484,6 +491,13 @@ export class DownloadTask { ), params.unzipDirectory, ); + try { + if (fileIo.accessSync(params.targetFile)) { + await fileIo.unlink(params.targetFile); + } + } catch (e) { + console.error('Failed to delete temporary zip file after patching:', e); + } } private async doPatchFromPpk(params: DownloadTaskParams): Promise { @@ -517,6 +531,13 @@ export class DownloadTask { enableMerge: plan.enableMerge, }); console.info('Patch from PPK completed'); + try { + if (fileIo.accessSync(params.targetFile)) { + await fileIo.unlink(params.targetFile); + } + } catch (e) { + console.error('Failed to delete temporary patch file after patching:', e); + } } private async copyFromResource( @@ -548,27 +569,35 @@ export class DownloadTask { parentDirs.map(dir => this.ensureDirectory(dir)), ); await Promise.all( - targets.map(target => this.writeFileContent(target, mediaBuffer.buffer)), + targets.map(target => this.writeFileContent(target, mediaBuffer)), ); continue; } const fromContent = await resourceManager.getRawFd(currentFrom); - const [firstTarget, ...restTargets] = targets; - const parentDirs = [ - ...new Set( - targets.map(t => t.substring(0, t.lastIndexOf('/'))).filter(Boolean), - ), - ]; - await Promise.all( - parentDirs.map(dir => this.ensureDirectory(dir)) - ); - if (fileIo.accessSync(firstTarget)) { - await fileIo.unlink(firstTarget); + try { + const [firstTarget, ...restTargets] = targets; + const parentDirs = [ + ...new Set( + targets.map(t => t.substring(0, t.lastIndexOf('/'))).filter(Boolean), + ), + ]; + await Promise.all( + parentDirs.map(dir => this.ensureDirectory(dir)) + ); + if (fileIo.accessSync(firstTarget)) { + await fileIo.unlink(firstTarget); + } + saveFileToSandbox(fromContent, firstTarget); + await Promise.all( + restTargets.map(target => this.copySandboxFile(firstTarget, target)), + ); + } finally { + try { + await resourceManager.closeRawFd(currentFrom); + } catch (closeError) { + console.error(`Failed to close raw fd for ${currentFrom}:`, closeError); + } } - saveFileToSandbox(fromContent, firstTarget); - await Promise.all( - restTargets.map(target => this.copySandboxFile(firstTarget, target)), - ); } } catch (error) { error.message = @@ -589,7 +618,7 @@ export class DownloadTask { params.unzipDirectory, params.hash || '', params.originHash || '', - 7, + 3, ); } catch (error) { error.message = 'Cleanup failed:' + error.message; diff --git a/harmony/pushy/src/main/ets/UpdateContext.ts b/harmony/pushy/src/main/ets/UpdateContext.ts index daa37418..d8d6435c 100644 --- a/harmony/pushy/src/main/ets/UpdateContext.ts +++ b/harmony/pushy/src/main/ets/UpdateContext.ts @@ -513,7 +513,7 @@ export class UpdateContext { this.rootDir, state.currentVersion || '', state.lastVersion || '', - 7, + 3, ); } diff --git a/ios/RCTPushy/RCTPushy.mm b/ios/RCTPushy/RCTPushy.mm index 55f55981..decac4de 100644 --- a/ios/RCTPushy/RCTPushy.mm +++ b/ios/RCTPushy/RCTPushy.mm @@ -748,7 +748,7 @@ - (void)clearInvalidFiles PushyToStdString(downloadDir), state.current_version, state.last_version, - 7 + 3 ); if (!status.ok) { RCTLogWarn(@"Pushy cleanup error: %s", status.message.c_str());