Skip to content

Fix TURN CreatePermission panic in stun crate Display impl#169

Closed
ale7714 wants to merge 1 commit intomainfrom
fix/stun-display-panic
Closed

Fix TURN CreatePermission panic in stun crate Display impl#169
ale7714 wants to merge 1 commit intomainfrom
fix/stun-display-panic

Conversation

@ale7714
Copy link
Member

@ale7714 ale7714 commented Mar 19, 2026

Summary

  • Patches the vendored stun crate's ErrorCodeAttribute::Display implementation to use String::from_utf8_lossy() instead of String::from_utf8(), which panics via format!() when the TURN error reason contains non-UTF-8 bytes.
  • This fixes WebRTC connection failures where the panic in create_permissions kills the TURN relay setup, causing a timeout and fallback to direct gRPC through app.viam.com, which returns unknown service viam.robot.v1.RobotService.
  • Verified fix with Python SDK connecting to a live cloud machine — WebRTC connects successfully after the patch.

Root Cause

The stun-0.7.0 crate's ErrorCodeAttribute::Display returns Err(fmt::Error) when the error reason bytes aren't valid UTF-8. Rust's format!() macro panics when a Display impl returns an error ("a formatting trait implementation returned an error"). This is triggered in turn-0.9.0/src/client/relay_conn.rs:436 during create_permissions.

The upstream stun/turn crates are archived and unmaintained (webrtc-rs), so a local patch via [patch.crates-io] is the appropriate fix.

Test plan

  • Built patched libviam_rust_utils.dylib locally
  • Replaced Python SDK's bundled dylib with patched version
  • Successfully connected to cloud machine (mugger1-main) via WebRTC
  • All resource operations (camera images, IMU readings) returned data
  • CI builds pass

🤖 Generated with Claude Code

…play impl

The stun crate's ErrorCodeAttribute::Display implementation panics when
the error reason contains non-UTF-8 bytes, because format!() panics when
a Display impl returns Err(fmt::Error). This patch uses from_utf8_lossy()
instead, which surfaces the actual TURN error gracefully rather than
panicking.

This fixes WebRTC connection failures where the TURN relay setup panic
killed the connection attempt, resulting in "unknown service
viam.robot.v1.RobotService" errors on fallback.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@ale7714 ale7714 requested a review from a team as a code owner March 19, 2026 16:19
@ale7714 ale7714 requested review from lia-viam and stuqdog March 19, 2026 16:19
@ale7714 ale7714 closed this Mar 19, 2026
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