Skip to content

Control a real USB-connected iPhone from Linux (no Mac, no paid developer account) and expose it to AI agents as MCP tools

fix(update): try the candidate again when its file is still busy (#81)

Qinghe Lie14b20cd4fe0
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 02ffa05..97608d9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -34,6 +34,7 @@ All notable changes to this project are documented here. The format is based on
### Fixed
+- Checking a new version during an update (running its `--version`) could fail now and then with "Text file busy": another thread started a process while the new program was still open for writing. It now waits a moment and tries again.
- `task run`: a command that ended its task with `task finish` is no longer stopped on its way out with exit code 1. It now has 5 seconds to exit with its own code; if it is stopped after that, the exit code is 0 when the task completed.
- Holding an arrow key in a setup menu keeps moving the selection without echoing key sequences into the menu.
- AI task summaries on the control page are formatted: bold, lists, inline code and line breaks show as such instead of raw Markdown.
diff --git a/CHANGELOG.zh-CN.md b/CHANGELOG.zh-CN.md
index b892647..1634326 100644
--- a/CHANGELOG.zh-CN.md
+++ b/CHANGELOG.zh-CN.md
@@ -34,6 +34,7 @@
### 修复
+- 升级时检查新程序(运行它的 `--version`)偶尔会因为「Text file busy」失败:同一进程的其他线程恰好在新程序还以写方式打开时启动了子进程。现在遇到这种情况会稍等重试。
- `task run`:命令自己用 `task finish` 结束任务后,不再在它正常退出时被立刻停掉、退出码变成 1。现在它有 5 秒自己退出,退出码就是它自己的;超时才停掉,这时任务完成则退出码为 0。
- 在设置菜单中长按方向键时,选项会连续移动,按键序列不会回显到菜单里。
- 控制台上 AI 任务的总结按格式显示:粗体、列表、行内代码和换行不再显示成 Markdown 原文。
diff --git a/apps/iphone-control/src/update/archive.rs b/apps/iphone-control/src/update/archive.rs
index a7ed252..a49e5cd 100644
--- a/apps/iphone-control/src/update/archive.rs
+++ b/apps/iphone-control/src/update/archive.rs
@@ -214,11 +214,23 @@ pub(super) async fn stage(
}
pub(super) async fn preflight(candidate: &Path, version: &str) -> anyhow::Result<()> {
- let mut cmd = tokio::process::Command::new(candidate);
- cmd.arg("--version").kill_on_drop(true);
- let out = tokio::time::timeout(Duration::from_secs(10), cmd.output())
- .await
- .context("candidate --version timed out")??;
+ // "text file busy" (ETXTBSY): another thread forked while the candidate was still open for writing, and the
+ // child's copy of that descriptor lives until it execs. It is gone in a moment, so try again
+ let mut busy = 0;
+ let out = loop {
+ let mut cmd = tokio::process::Command::new(candidate);
+ cmd.arg("--version").kill_on_drop(true);
+ match tokio::time::timeout(Duration::from_secs(10), cmd.output())
+ .await
+ .context("candidate --version timed out")?
+ {
+ Err(e) if e.raw_os_error() == Some(libc::ETXTBSY) && busy < 20 => {
+ busy += 1;
+ tokio::time::sleep(Duration::from_millis(50)).await;
+ }
+ r => break r?,
+ }
+ };
ensure!(
out.status.success() && std::str::from_utf8(&out.stdout)?.trim() == format!("iphone-control {version}"),
"unexpected candidate --version"
@@ -257,6 +269,22 @@ mod tests {
k.public_key().as_ref().iter().map(|v| format!("{v:02x}")).collect()
}
+ #[tokio::test]
+ async fn the_candidate_runs_once_it_is_no_longer_open_for_writing() {
+ // the race behind "text file busy": the candidate is still open for writing somewhere when it is run
+ let root = tempfile::tempdir().unwrap();
+ let file = root.path().join("iphone-control");
+ std::fs::write(&file, "#!/bin/sh\necho iphone-control 0.2.1\n").unwrap();
+ std::fs::set_permissions(&file, std::fs::Permissions::from_mode(0o755)).unwrap();
+ let open = std::fs::OpenOptions::new().append(true).open(&file).unwrap();
+ let released = tokio::spawn(async move {
+ tokio::time::sleep(Duration::from_millis(200)).await;
+ drop(open);
+ });
+ preflight(&file, "0.2.1").await.unwrap();
+ released.await.unwrap();
+ }
+
#[tokio::test]
async fn verifies_publisher_signed_bytes_before_staging_an_executable() {
let root = tempfile::tempdir().unwrap();