chinedufn / swift-bridge

swift-bridge facilitates Rust and Swift interop.
https://chinedufn.github.io/swift-bridge
Apache License 2.0
842 stars 62 forks source link

println! panics if called after Xcode debug connection was lost #291

Closed lukbukkit closed 2 months ago

lukbukkit commented 2 months ago

Hi,

Thanks for this great library. I've discovered a reproducible crash partly in connection to this library. The invocation of println! in Rust causes a panic (and a crash if not caught) if Xcode launches an iOS app with an enabled debug connection, but then the connection is lost.

Steps To Reproduce

  1. Clone https://github.com/lukbukkit/swift-rust-bridge-println to your Mac
  2. Open the Xcode project SwiftRustBridgePrintln/SwiftRustBridgePrintln.xcodeproj
  3. Build the app two times to generate all required files
  4. Run the app on a physical iPhone
  5. Abort the debugging connection by disconnecting USB cables and enabling flight mode on the iPhone
  6. Wait until Xcode shows the alert "Lost connection to debugger"
  7. After 30 seconds, press the button to call a native Rust function, triggering the panic
  8. You should see the panic payload and backtrace on screen (as the panic was caught and didn't crash the app)

Screenshot

IMG_5957

Versions

macOS: Sonoma 14.6.1 Xcode: 15.4 iOS: 17.6.1

rustc: 1.18.0 swift-bridge: 0.1.57

chinedufn commented 2 months ago

Can you clarify why you believe that this crash is related to swift-bridge?

lukbukkit commented 2 months ago

Hi, you're probably correct that the cause of the crash is unrelated to swift-bridge. Where would you recommend reporting this issue? Is the rust-lang/rust repository the correct place?

chinedufn commented 2 months ago

Does the program crash if you write to stdout from Swift instead of Rust?

lukbukkit commented 1 month ago

No, Swift's internal print function does not cause a crash in these conditions. I've updated the example repository with comments to highlight the line that causes the crash.

chinedufn commented 1 month ago

Where would you recommend reporting this issue?

Hard to say without knowing the root cause.

You can do the following:

Add a pub extern "C" fn print_hello() { println!("hello") } to your Rust library. https://github.com/lukbukkit/swift-rust-bridge-println/blob/main/src/lib.rs

Add a void print_hello(void); to your BridgingHeader.h https://github.com/lukbukkit/swift-rust-bridge-println/blob/main/SwiftRustBridgePrintln/BridgingHeader.h

Replace your call to rust.hello() with a call to print_hello() rustApp.rust.hello(). This will make us sure about whether swift-bridge is involved.

If the println! panics, you can try going even more minimal and just writing a string to stdout and seeing if that panics.

std::io::stdout().write_all(b"hello world").unwrap();