webrtc-sys: detach the frame transformer when a FrameCryptor is destroyed - #1408
Open
ctarabocchia wants to merge 1 commit into
Open
webrtc-sys: detach the frame transformer when a FrameCryptor is destroyed#1408ctarabocchia wants to merge 1 commit into
ctarabocchia wants to merge 1 commit into
Conversation
|
|
Contributor
There was a problem hiding this comment.
Devin Review found 1 potential issue.
1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
Comment on lines
+177
to
+181
| if (sender_) { | ||
| sender_->SetEncoderToPacketizerFrameTransformer(nullptr); | ||
| } | ||
| if (receiver_) { | ||
| receiver_->SetDepacketizerToDecoderFrameTransformer(nullptr); |
Contributor
There was a problem hiding this comment.
🟡 Cryptor destruction removes other transforms
Dropping a cryptor after set_packet_trailer_handler also detaches the retained packet-trailer handler. A stale cryptor can likewise detach a replacement cryptor.
Prompt for agents
FrameCryptor::~FrameCryptor unconditionally sets the sender or receiver transformer to null. FrameCryptor::set_packet_trailer_handler replaces that slot with chained_transformer_, which contains both the cryptor and a PacketTrailerTransformer. Clearing the slot therefore disables packet-trailer processing even when the independently retained PacketTrailerHandler remains alive. The same ownership problem lets an older FrameCryptor clear a newer transformer installed on the same sender or receiver when its final SharedPtr is dropped later. Redesign teardown so a cryptor removes only the transformer installation it owns and preserves or restores other processing. Account for replacement cryptors and chained packet-trailer handlers rather than blindly clearing the shared endpoint slot.
Was this helpful? React with 👍 or 👎 to provide feedback.
ctarabocchia
added a commit
to wherryapp/wherry
that referenced
this pull request
Sep 7, 2026
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…oyed FrameCryptor's constructors attach a FrameCryptorTransformer to the RtpSender or RtpReceiver they were given, and the destructor only unregistered the observer. libwebrtc keeps a transformer alive for as long as it is set on the sender/receiver, so every FrameCryptor left its transformer -- and the thread that transformer owns -- behind until the peer connection was destroyed. Measured from a Room that is connected, publishes one audio track under E2EE and closes: one leaked FrameCryptorTransformer thread per cryptor per room, never reclaimed. Clear the transformer on the sender/receiver in the destructor.
ctarabocchia
force-pushed
the
wherry/frame-cryptor-detach
branch
from
September 7, 2026 21:03
bf2ea2d to
73ff28c
Compare
ctarabocchia
added a commit
to wherryapp/wherry
that referenced
this pull request
Sep 7, 2026
… and the fork commit is re-authored Both gates the native media plan named are answered, so the per-device checkbox in Settings → Voice starts ticked on a fresh desktop device (client/src/voice/prefs.ts; a device that unticks it keeps its choice; the web and the phones have no engine and read the value as nothing). The maintainer's decision, made the same evening the second gate closed. Verified on macOS from tauri dev and one debug bundle; believed for the bundled release build and for Windows, which has never run the engine -- docs/prompts/windows-rebuild-plan.md is the plan for that pass, written for the Windows machine. The fork commit carrying the cryptor fix (livekit/rust-sdks#1408) was re-authored to the maintainer's GitHub noreply address so the CLA bot can link it -- a real address is refused by the account's email-privacy setting on push -- and force-pushed; Cargo.toml's [patch] rev, Cargo.lock, the patch files and every doc mention now name 73ff28ce. Signing the CLA is the maintainer's own step. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
rust-sdks-frame-cryptor-detach.patch
One commit on top of
livekit/rust-sdksatdee418bb(the revisionCargo.tomlpins):FrameCryptor::~FrameCryptorinwebrtc-sys/src/frame_cryptor.cppdetaches the transformer from theRtpSender/RtpReceiverit was attached to. Without it every cryptorleaves its
FrameCryptorTransformerthread behind until the peerconnection is destroyed — one thread per cryptor per call, never
reclaimed (
docs/prompts/native-media-plan.md§5.1, gate 1).Verified 2026-09-07 on the dev Mac against a browser peer: 20 leaked
threads after 10 calls on the unpatched bridge; 0 after 6 calls with the
patch, twice, with the audio still sealed both ways.
In the build since 2026-09-07 (evening) through the fork:
wherryapp/rust-sdks, branchwherry/frame-cryptor-detach, commitbf2ea2d59290d1411d6e659229ade397bb7e667d— the[patch]section inclient/src-tauri/Cargo.toml. The upstream pull request has not beenopened (the session that would have opened it was not allowed to);
the branch is pushed and the command is:
gh pr create --repo livekit/rust-sdks --base main \ --head wherryapp:wherry/frame-cryptor-detach \ --title "webrtc-sys: detach the frame transformer when a FrameCryptor is destroyed" \ --body-file client/src-tauri/patches/rust-sdks-frame-cryptor-detach.md(the body below the rule is written to be that PR's description). When it
merges, delete the
[patch]section and bumprevwithLIVEKIT_REV.Cargo cannot apply a patch file to a git dependency, so the choices were:
fork
livekit/rust-sdks(under thewherryapporg),git amthisfile on a branch from
dee418bb, and add toclient/src-tauri/Cargo.toml:(only
webrtc-sysis patched;livekititself keeps coming fromupstream at the pinned rev, and
webrtc-sys-buildfollows the patchedcrate's workspace), or
get the change merged upstream and bump
revinCargo.tomltogetherwith
LIVEKIT_REVinsrc/voice.rs.To reproduce the measurement build locally without a fork:
Upstream issue — drafted, not filed
Title: webrtc-sys: FrameCryptor never detaches its transformer, leaking one FrameCryptorTransformer thread per cryptor per room
FrameCryptor's constructors (webrtc-sys/src/frame_cryptor.cpp) attach aFrameCryptorTransformerto theRtpSender/RtpReceiverwithSetEncoderToPacketizerFrameTransformer/SetDepacketizerToDecoderFrameTransformer.FrameCryptor::~FrameCryptoronly unregisters the observer; it never clearsthe transformer on the sender/receiver, and the bindings expose no way for
the Rust side to. libwebrtc keeps a transformer for as long as it is set on
the sender/receiver, and the transformer owns a thread that stops only in
its destructor, so the thread outlives the
FrameCryptor, theRoomandevery handle the application holds.
Reproduction (macOS 15, Apple Silicon,
rev = dee418bb,livekitwithrustls-tls-native-roots, E2EEGcmwithKeyDerivationAlgorithm::HKDF):Room::connectwithE2eeOptions, publish one microphone audio track(
PlatformAudio), subscribe to one remote audio track.unpublish_track,Room::close, drop theRoom.sample <pid> 1 | grep -c FrameCryptorTransformer.Observed: the count grows by one per cryptor per call and never comes
back — 20
FrameCryptorTransformerthreads after 10 calls that each had asender and a receiver cryptor.
E2eeManager::cleanupdoes run on close(it disables the cryptors and clears the map, so the Rust
FrameCryptoris dropped), which is what points at the C++ destructor.
Fix that measured clean here — clear the transformer in the destructor:
With this, the same soak reads 0 threads after 6 calls (12 cryptors), and
encryption state stays
Okat both ends. Happy to open a PR.