Updater::begin picks the 4 KB buffer by total free heap, and does not fall back to 256 B when the allocation fails
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 76/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- cpp
- 领域
- embedded-iot
调研方向
阅读 cores/esp8266/Updater.cpp 中 UpdaterClass::begin() 和 _writeBuffer() 附近的代码,然后检查 Updater.h 中的流模板,以了解缓冲区大小的用法。验证 4 KB 分配失败时可以选择较小的缓冲区,并且使用 256 字节写入时扇区擦除仍能正常工作。完成标准是:在堆碎片化时,begin() 可以回退到较小的缓冲区,且不改变更新行为。
由索引模型根据 Issue 内容生成。
描述
What happens
UpdaterClass::begin() chooses its write buffer size from the total free heap:
// cores/esp8266/Updater.cpp, 3.1.2 lines 172-181 (same on master, lines 174-183)
if (ESP.getFreeHeap() > 2 * FLASH_SECTOR_SIZE) {
_bufferSize = FLASH_SECTOR_SIZE;
} else {
_bufferSize = 256;
}
_buffer = new (std::nothrow) uint8_t[_bufferSize];
if (!_buffer) {
_setError(UPDATE_ERROR_OOM);
_reset(false);
return false;
}
Total free heap says nothing about the largest free block. On a fragmented heap the check can pass while a 4096 byte block does not exist. Then new fails and begin() returns false with UPDATE_ERROR_OOM. The 256 byte buffer is never tried.
What I measured
- Core 3.1.2, ESP8266 (GeekMagic SmallTV-Ultra).
ESP.getFreeHeap()= 19.6 KB.ESP.getMaxFreeBlockSize()= 3,888 bytes.
What I did not measure
I did not run the upload in this state. I restarted the device first. That the upload would be refused with UPDATE_ERROR_OOM comes from reading the code above. 19.6 KB is more than 8192, so the 4 KB branch is taken. A 4096 byte allocation cannot fit in a 3,888 byte block.
Why a smaller buffer should be safe
_bufferSize is private and is only used inside Updater.cpp and the Updater.h stream template. The 256 byte case already exists for low free heap (commit 01e1c586, "Updater: if low on heap memory, use smaller write buffer"). _writeBuffer() erases a sector when _currentAddress % FLASH_SECTOR_SIZE == 0, which still works with 256 byte writes.
Proposed small change (sketch, untested)
- if (ESP.getFreeHeap() > 2 * FLASH_SECTOR_SIZE) {
+ if (ESP.getMaxFreeBlockSize() >= 2 * FLASH_SECTOR_SIZE) {
_bufferSize = FLASH_SECTOR_SIZE;
} else {
_bufferSize = 256;
}
_buffer = new (std::nothrow) uint8_t[_bufferSize];
+ if (!_buffer && _bufferSize > 256) {
+ _bufferSize = 256;
+ _buffer = new (std::nothrow) uint8_t[_bufferSize];
+ }
if (!_buffer) {
The first part picks by largest block. The second part is a safety net if the allocation still fails. Either part alone would already fix the case above. I can prepare a PR if this direction is fine.
- 主要语言
- C++
- 星标
- 16.7k
- 派生
- 13k
- 平均合并
- 1 天 2 小时
- 30 天内合并 PR
- 7
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
esp8266/Arduino 的其他 Issue
-
Typo in Scan Class Documentation可能已有人在做 @MukundaKatta 于 207 天前认领。 未关闭
难度 1/5 1 小时以内 新手友好度 70/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
esp8266/Arduino#9203 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 52/100
维护者通常 1 天内回复
-
难度 3/5 1-2 天 新手友好度 65/100
esp8266/Arduino#9324 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 42/100
维护者通常 1 天内回复
相似的 Issue
-
status:needs-triage
难度 2/5 1-3 小时 新手友好度 74/100
PX4/PX4-Autopilot#29698 ·
维护者通常 1 天内回复
-
go perft exits when EvalFile is unavailable可能已有人在做 @Muawiya-contact 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 68/100
official-stockfish/Stockfish#7292 · 2 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 77/100
arvidn/libtorrent#9149 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 75/100
rhalbersma/tabula#72 ·
-
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复