Skip to content

fix(h5): 释放 chooseMedia 加载失败的临时资源 - #19555

Merged
Single-Dancer merged 1 commit into
NervJS:mainfrom
Jarvis636431:fix/h5-choose-media-url-cleanup
Oct 23, 2026
Merged

Single-Dancer merged 1 commit into
NervJS:mainfrom
Jarvis636431:fix/h5-choose-media-url-cleanup

Conversation

@Jarvis636431

@Jarvis636431 Jarvis636431 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

这个 PR 做了什么?

H5 chooseMedia 在图片解码、视频解码或视频文件读取失败时,会丢弃失败文件,却保留此前创建的 blob URL。现在统一捕获单文件加载失败并释放对应 URL,再沿用既有的失败文件丢弃策略;成功返回的图片 URL 保持可用。

新增和扩展回归测试,覆盖混合成功失败、重复选择与重复错误、视频解码/读取错误,以及成功后迟到的错误不释放已返回的 URL。

Closes #19554

这个 PR 是什么类型?

  • 错误修复 (Bugfix)

这个 PR 涉及以下平台:

  • Web 端(H5)

验证

  • 原实现运行用例:4 条资源释放断言失败;修复后 chooseMedia 6 条用例全部通过。
  • pnpm --filter @tarojs/taro-h5 run test:ci --watchman=false:27 个测试文件、209 个用例全部通过。
  • pnpm --filter @tarojs/taro-h5 run build:在完整执行环境下退出码为 0;构建仍存在未修改文件的既有类型与 Sass 等警告。
  • 修改文件 ESLint 和 git diff --check 通过。
  • 使用 Jest/jsdom 和受控媒体事件验证,未进行真实浏览器内存测量。

Summary by CodeRabbit

  • Bug 修复
    • 修复媒体文件加载失败时临时资源未及时释放的问题,涵盖视频解码失败和文件读取失败等情况。
    • 避免同一加载错误导致重复释放资源;媒体加载成功时,资源仍保持可用。

@coderabbitai

coderabbitai Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 51dea29c-876f-4c27-9a82-40386e96964d
📥 Commits

Reviewing files that changed from the base of the PR and between e6702ec and bc6d670.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8f951b55-a2db-4fd7-ab6f-e1608e1179c2
📥 Commits

Reviewing files that changed from the base of the PR and between 2673da5 and e6702ec.

📒 Files selected for processing: 2
  • packages/taro-h5/__tests__/media/chooseMedia.test.ts
  • packages/taro-h5/src/api/media/video/chooseMedia.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Walkthrough

H5 chooseMedia 在媒体加载失败时撤销对应的对象 URL,并重新抛出错误。测试覆盖图片和视频失败场景,以及成功文件的 URL 保留。

Changes

媒体加载失败时释放对象 URL

Layer / File(s) Summary
媒体加载与失败清理
packages/taro-h5/src/api/media/video/chooseMedia.ts, packages/taro-h5/__tests__/media/chooseMedia.test.ts
loadMedia 将媒体处理委托给 loadMediaFromUrl。加载失败时会撤销对象 URL 并重新抛出错误。测试验证成功文件的 URL 不会被释放,并覆盖重复失败、迟到错误、视频解码失败和视频读取失败。

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: jayya

Merge Risk: ⚪ Minimal · up to e6702

The failed-file cleanup path appears sound, and no merge-blocking issue remains after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 标题准确概括了 PR 的主要变更,即在 H5 chooseMedia 加载失败时释放临时资源。标题简洁且与改动范围一致。
Linked Issues check Passed [#19554] 要求三项编码目标:(1) 图片解码、视频解码或视频文件读取失败时释放失败文件的 object URL;(2) 成功返回文件的 URL 保持有效;(3) 混合选择时按 FileList 顺序返回成功文件。

证据:
- packages/taro-h5/src/api/media/video/chooseMedia.ts 中新增 loadMedia(file)。它先调用 `…
Out of Scope Changes check Passed 变更范围仅包含两类内容:
1. chooseMedia.ts 的 loadMedia 与 loadMediaFromUrl 拆分。该拆分是实现 #19554 失败释放逻辑的必要结构,属于同一目标。
2. 测试文件的修改。其中 chooseFiles 新增可选 type 参数,默认值仍为 image/png。该参数用于构造视频测试文件,属于支撑 #19554 回归测试的变更,…
Full details: Linked Issues check

Explanation

[#19554] 要求三项编码目标:(1) 图片解码、视频解码或视频文件读取失败时释放失败文件的 object URL;(2) 成功返回文件的 URL 保持有效;(3) 混合选择时按 FileList 顺序返回成功文件。

证据:

  • packages/taro-h5/src/api/media/video/chooseMedia.ts 中新增 loadMedia(file)。它先调用 URL.createObjectURL(file),再委托 loadMediaFromUrl(file, dataUrl)。当返回的 Promise 被 reject 时,.catch 调用 URL.revokeObjectURL(dataUrl) 并重新抛出错误。这覆盖图片 img.onerror、视频 video.onerror 和 reader.onerror 三条失败路径。
  • 成功路径的 Promise 已 resolve。之后到达的错误不会再触发 .catch,因此已返回的 URL 不会被释放。
  • 调用方的 arr.map(async ... try { await loadMedia } catch) 保留既有的失败文件丢弃策略。Promise.all 按输入数组顺序写入 res.tempFiles,满足 FileList 顺序要求。
  • 测试文件 packages/taro-h5/__tests__/media/chooseMedia.test.ts 新增或扩展了以下用例:混合成功与失败时只释放失败文件;重复选择和重复错误时每个失败 URL 只释放一次;成功后的迟到错误不释放已返回 URL;视频解码失败和视频读取失败时释放 URL。

说明:作者报告原实现有 4 条资源释放断言失败,修复后 6 条用例通过。本评估未重新执行测试。验证基于 Jest/jsdom 与受控媒体事件,未进行真实浏览器内存测量,此限制不影响上述代码路径的判定。

Full details: Out of Scope Changes check

Explanation

变更范围仅包含两类内容:

  1. chooseMedia.ts 的 loadMedia 与 loadMediaFromUrl 拆分。该拆分是实现 #19554 失败释放逻辑的必要结构,属于同一目标。
  2. 测试文件的修改。其中 chooseFiles 新增可选 type 参数,默认值仍为 image/png。该参数用于构造视频测试文件,属于支撑 #19554 回归测试的变更,不涉及无关功能。

未发现与 #19554 无关的功能改动、配置改动或文档改动。ignored_files 为空,未排除任何文件。

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

小兔听见加载声,
成功的 URL 留在原处。
若图片视频未能解码,
失败的地址便撤销。
测试逐项轻轻核对,
月光下,文件顺序依旧。

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 57.22%. Comparing base (18ea23f) to head (bc6d670).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #19555      +/-   ##
==========================================
+ Coverage   57.15%   57.22%   +0.06%     
==========================================
  Files         467      467              
  Lines       25689    25693       +4     
  Branches     6407     6419      +12     
==========================================
+ Hits        14683    14703      +20     
+ Misses       9065     9050      -15     
+ Partials     1941     1940       -1     
Flag Coverage Δ
taro-cli 60.27% <ø> (ø)
taro-runtime 63.55% <ø> (ø)
taro-web 65.50% <100.00%> (+0.44%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...ackages/taro-h5/src/api/media/video/chooseMedia.ts 76.97% <100.00%> (+12.53%) ⬆️

... and 7 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Single-Dancer
Single-Dancer self-requested a review October 23, 2026 06:37
@Single-Dancer Single-Dancer added this to the 4.3.2 milestone Oct 23, 2026
@Single-Dancer
Single-Dancer force-pushed the fix/h5-choose-media-url-cleanup branch from e6702ec to bc6d670 Compare October 23, 2026 06:42
@Single-Dancer
Single-Dancer merged commit 473dfaa into NervJS:main Oct 23, 2026
25 checks passed
@Jarvis636431
Jarvis636431 deleted the fix/h5-choose-media-url-cleanup branch November 3, 2026 03:19
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

Successfully merging this pull request may close these issues.

[Bug]: H5 chooseMedia 加载失败时未释放临时 blob URL

2 participants