Skip to content

fix(mobile): cap TCP fallback listen with 5s timeout so reader can't silently hang#204

Open
codedogQBY wants to merge 1 commit into
mainfrom
fix/file-server-tcp-fallback-timeout
Open

fix(mobile): cap TCP fallback listen with 5s timeout so reader can't silently hang#204
codedogQBY wants to merge 1 commit into
mainfrom
fix/file-server-tcp-fallback-timeout

Conversation

@codedogQBY
Copy link
Copy Markdown
Owner

@codedogQBY codedogQBY commented May 24, 2026

Summary

症状:当 dev client 二进制里两个文件服务器 native 模块(@dr.pogodin/react-native-static-server 的 Lighttpd 和 react-native-tcp-socket)都没正确链接时,开书后阅读器一直卡在 spinner,Metro 日志在 Lighttpd 8s 超时警告之后完全没有任何后续输出

根因

  • _startNativeServer 已经给 Lighttpd.start() 加了 8s timeout,超时就 catch 并转 _startTcpFallback
  • _startTcpFallbackserver.listen(callback) 在 native binding 缺失时 callback 永不触发,promise 永久 hang
  • ReaderScreen 的 await startFileServer() 继承这个 hang,错误从未 throw → 用户看到永久 spinner

修复:把 server.listen() / server.on("error") 用一个 settle helper 包起来,5s 超时关 server 并 reject 一个可操作的错误("rebuild with expo run:ios")。Lighttpd 已经有同样的保护,TCP 这条路径补齐对称。

正常 build 的环境下两个服务器都在 1s 内返回,这个 timeout 仅在 dev 环境守底,不会误触发。

Test plan

  • 模拟 native 模块未链接:spinner 不再卡死,5s 后 ReaderScreen 弹出明确错误信息
  • 正常环境(rebuild 后):文件服务器正常启动,无回退

…silently hang

Symptom: on a dev client where neither @dr.pogodin/react-native-static-server
nor react-native-tcp-socket native modules are linked into the binary, the
reader spins forever on a sepia background with no error in Metro logs after
the Lighttpd 8s timeout warning.

Root cause: _startNativeServer caps Lighttpd.start() at 8s and falls through to
_startTcpFallback on timeout. But _startTcpFallback's server.listen() callback
never fires when the tcp-socket native binding is missing, and the promise sits
unresolved. ReaderScreen's await startFileServer() inherits the hang.

Fix: wrap server.listen()/server.on("error") in a settle helper with a 5s
timeout that closes the server and rejects with a clear actionable message
("rebuild with expo run:ios"). Lighttpd already had the same safety net.

This is a dev-environment guardrail — in a properly built binary both servers
return in well under 1s.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@codedogQBY
Copy link
Copy Markdown
Owner Author

已在这个 PR 里改了:移动端文件服务器 TCP fallback 的 listen 流程增加 5 秒超时保护,native 模块未正确链接时不会再让阅读器一直卡在 spinner,而是抛出可操作的错误提示。合并后会随下个版本修复。

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.

1 participant