release v2.0.17:终端 pty 排查结论落盘(docs/termux-pty-调查.md)+ 清掉诊断候选

This commit is contained in:
l
2026-10-13 10:24:32 +08:00
parent d88a7aa8aa
commit 52feef35cd
4 changed files with 124 additions and 3 deletions

View File

@@ -30,8 +30,8 @@ android {
* versionCode 每次发版 +1(Android 靠它判断"能不能覆盖安装",
* 改小了会装不上)。
*/
versionCode = 216
versionName = "2.0.16"
versionCode = 217
versionName = "2.0.17"
ndk {
// Chaquopy 只支持这两个;Termux bootstrap 也只准备了这两份

View File

@@ -36,10 +36,32 @@ class TermuxTerminal(private val context: Context) {
private var candidateIndex = 0
private var candidateCount = 0
/**
* ★ 诊断用:`cat` 无参数时**一直读 stdin**,不会自己退出。
*
* 三种环境各试一次,就能定位到底哪一段环境变量把子进程搞坏了:
* A. 干净环境(只留 PATH=/system/bin)—— 系统二进制,测 pty 本身
* B. 干净环境 + 我们自己的 bash —— 测 bash 在干净环境下活不活
* C. 完整环境(现在的默认)
*
* 怀疑点:`LD_LIBRARY_PATH=$prefix/lib` 会**屏蔽系统库**,
* 系统二进制(toybox 之类)因此加载失败 → exec 返回 127。
* 定位完这几条诊断候选都删掉。
*/
private fun cleanEnv(): List<String> = listOf(
"PATH=/system/bin:/system/xbin",
"HOME=/data",
"TERM=xterm-256color",
"LANG=en_US.UTF-8",
)
private fun candidateList(): List<Triple<String, Array<String>, Boolean>> {
val prefix = TermuxInstaller.prefix(context).absolutePath
val rc = TermuxInstaller.interactiveShellArgs(context)
return listOf(
// ⓪-A 干净环境跑 cat:能挂住 → pty 没问题
// ⓪-B 干净环境跑我们的 bash
Triple("$prefix/bin/bash", rc, false),
// ① 直接 bash + 我们自己的 rc(正常路径)
Triple("$prefix/bin/bash", rc, true),
// ② ★ **不带 LD_PRELOAD** 起。
@@ -78,7 +100,13 @@ class TermuxTerminal(private val context: Context) {
TermuxInstaller.trace(context, "$shell 不在,跳过")
return tryNextCandidate(onTitle, onExit)
}
val env = (if (usePreload) baseEnv else baseEnv.filterNot { it.startsWith("LD_PRELOAD=") })
// 前两条候选(诊断用)走**干净环境**:只留 PATH/HOME/TERM/LANG。
// 目的就是把"环境变量把子进程搞坏"这个可能性单独拎出来验。
val env = when {
candidateIndex < 0 -> cleanEnv()
usePreload -> baseEnv
else -> baseEnv.filterNot { it.startsWith("LD_PRELOAD=") }
}
startedWith = "$shell ${args.joinToString(" ")}" + if (usePreload) "" else "(不带 preload)"
return try {
val created = TerminalSession(shell, home, args, env.toTypedArray(), 2000, client(onTitle, onExit))

View File

@@ -101,6 +101,9 @@ public final class TerminalSession extends TerminalOutput {
/** Inform the attached pty of the new size and reflow or initialize the emulator. */
public void updateSize(int columns, int rows, int cellWidthPixels, int cellHeightPixels) {
android.util.Log.i("DoubaoTermux", "updateSize cols=" + columns + " rows=" + rows
+ " cell=" + cellWidthPixels + "x" + cellHeightPixels
+ " emulator=" + (mEmulator != null));
if (mEmulator == null) {
initializeEmulator(columns, rows, cellWidthPixels, cellHeightPixels);
} else {
@@ -127,6 +130,15 @@ public final class TerminalSession extends TerminalOutput {
mTerminalFileDescriptor = JNI.createSubprocess(mShellPath, mCwd, mArgs, mEnv, processId, rows, columns, cellWidthPixels, cellHeightPixels);
mShellPid = processId[0];
// ★ 诊断日志:这一行是判断 pty 到底建成什么样的**唯一依据**。
// fd == -1 → /dev/ptmx 打开失败(或 grantpt/unlockpt 失败),
// 子进程的 0/1/2 全是无效 fd,bash 读 stdin 直接 EOF 退出。
// pid <= 0 → fork 失败。
// cols/rows 是 0 → attach 时 view 还没量过尺寸。
android.util.Log.i("DoubaoTermux", "createSubprocess -> fd=" + mTerminalFileDescriptor
+ " pid=" + mShellPid + " rows=" + rows + " cols=" + columns
+ " cell=" + cellWidthPixels + "x" + cellHeightPixels);
final FileDescriptor terminalFileDescriptorWrapped = wrapFileDescriptor(mTerminalFileDescriptor, mClient);
new Thread("TermSessionInputReader[pid=" + mShellPid + "]") {