Skip to content

Remove rt-multi-thread dependency from AFC file descriptor drop#93

Merged
jkcoxson merged 1 commit into
jkcoxson:masterfrom
maxslarsson:remove-rt-multi-thread
May 7, 2026
Merged

Remove rt-multi-thread dependency from AFC file descriptor drop#93
jkcoxson merged 1 commit into
jkcoxson:masterfrom
maxslarsson:remove-rt-multi-thread

Conversation

@maxslarsson
Copy link
Copy Markdown
Contributor

Hey! Love the project -- been using it and it's been great.

I ran into an issue where rt-multi-thread gets pulled in as a transitive dependency just by depending on idevice. I really don't need, want, or can afford dragging rt-multi-thread into my project. It also feels a bit odd for a library to pull in a runtime opinion like that, especially since it's only used in one place: block_in_place when closing an AFC file descriptor on drop, specifically when running on a multi-threaded runtime. And yet rt-multi-thread is pulled in even if you don't use AFC (which I don't) nor a multi-threaded runtime (which I'm not I'm using single-threaded).

Since there's no async Drop in Rust, there isn't a clean way to handle this. One option I explored was pulling in rt instead of rt-multi-thread (which feels a lot more reasonable but still a little weird for a library) and using crate::spawn to fire-and-forget the close packet instead of block_in_place. The upside is it works on any runtime flavour including single-threaded and wasm. The downside is the close becomes fire-and-forget -- you lose the guarantee that the device ack'd the close before the AfcClient is dropped, and if the runtime is shutting down the spawned task might get cancelled.

But on wasm and single-threaded runtimes, the file descriptor was already just silently dropped without closing. So I figured it doesn't hurt that bad to just drop the file without sending the close, and require callers to explicitly call .close().await. I added a debug_assert! so it panics in debug mode if someone forgets, and it also prints an error to stdout (matching the existing error printing pattern on line 392 of the same file). I also went through all the existing call sites in the repo (tools, tests, crashreportcopymobile) and added explicit .close().await calls.

Another option worth considering: we could add a multi-thread crate feature that enables rt-multi-thread and brings back the defensive block_in_place close-on-drop behaviour for users who want that safety net and are already running on a multi-threaded runtime.

Let me know what you think or if you'd like to see any changes -- I'd love to get rt-multi-thread removed as a hard dependency!

AFC's close-on-drop used block_in_place + block_on to synchronously send
a FileClose packet, requiring tokio's rt-multi-thread feature. This is
heavyweight for a best-effort cleanup that already did nothing on wasm
and single-threaded runtimes.

Replace with a simple no-op drop that warns (debug_assert + println) if
.close().await wasn't called. The device reclaims FDs when the AFC
session ends regardless. Also fix the afc tool to explicitly close file
descriptors after use.
@jkcoxson
Copy link
Copy Markdown
Owner

jkcoxson commented May 7, 2026

I know @abdullah-albanna and I discussed this when adding it in the discord. Any thoughts?

@jkcoxson
Copy link
Copy Markdown
Owner

jkcoxson commented May 7, 2026

I've given it some thought, and I think adding the debug assert is a fine trade-off. The drop was added to catch dumb mistakes, so this is fine.

@jkcoxson jkcoxson merged commit 79e0409 into jkcoxson:master May 7, 2026
3 checks passed
maxslarsson added a commit to maxslarsson/idevice that referenced this pull request May 7, 2026
…xson#93)

AFC's close-on-drop used block_in_place + block_on to synchronously send
a FileClose packet, requiring tokio's rt-multi-thread feature. This is
heavyweight for a best-effort cleanup that already did nothing on wasm
and single-threaded runtimes.

Replace with a simple no-op drop that warns (debug_assert + println) if
.close().await wasn't called. The device reclaims FDs when the AFC
session ends regardless. Also fix the afc tool to explicitly close file
descriptors after use.
jkcoxson pushed a commit that referenced this pull request May 8, 2026
…gClient (#96)

* refactor: remove pairing_file field and state param from RemotePairingClient

Move pairing_file from a struct field (&'a mut borrow) to a parameter
on connect() and its callees. This removes the lifetime parameter from
the struct and scopes the mutable borrow to the function call.

Also remove the unused state: S generic from connect/pair/request_pair_consent,
since every caller passed 0u8 and ignored it. Callers can capture state
via move closures instead.

* fix: feature gating for error conversions and dead deps (#95)

- Add internal _serde_json and _reqwest features to gate From impls
- Remove unused json and byteorder optional dependencies
- Add missing xpc dependency to remote_pairing feature
- Simplify map_err workarounds in tunnel code to use ? directly

* fix: make openssl PairingFile escrow_bag field Optional (#97)

The openssl variant of PairingFile had escrow_bag as Vec<u8> while
RawPairingFile has it as Option<Data> (it's None on Apple Watch).
This caused compile errors in the From/TryFrom impls when building
with the openssl feature. The rustls variant already had the correct
Option<Vec<u8>> type.

* fix: improve openssl crypto backend (#98)

* fix: make openssl crypto path a first-class backend

Un-gate `ca.rs` and `pair()` from rustls — they only use pure Rust
crates (rsa, x509-cert, sha2) so pairing now works with openssl.
Always detect legacy devices instead of only with openssl feature.
Add openssl feature to FFI crate. Clean up SNI string and gate
rustls-only PEM helpers to fix dead code warnings.

* refactor: return legacy flag from start_session, remove duplicate detection

`LockdownClient::start_session` already queries ProductVersion to
detect legacy devices. Four callers were making the same query
beforehand. Now `start_session` returns the legacy bool so callers
reuse it instead of round-tripping to the device twice.

* Remove rt-multi-thread dependency from AFC file descriptor drop (#93)

AFC's close-on-drop used block_in_place + block_on to synchronously send
a FileClose packet, requiring tokio's rt-multi-thread feature. This is
heavyweight for a best-effort cleanup that already did nothing on wasm
and single-threaded runtimes.

Replace with a simple no-op drop that warns (debug_assert + println) if
.close().await wasn't called. The device reclaims FDs when the AFC
session ends regardless. Also fix the afc tool to explicitly close file
descriptors after use.
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.

2 participants