Skip to content

Explicitly drop FFI handles on dispose#1174

Closed
alan-george-lk wants to merge 1 commit into
mainfrom
bugfix/ffi_handle_cleanup
Closed

Explicitly drop FFI handles on dispose#1174
alan-george-lk wants to merge 1 commit into
mainfrom
bugfix/ffi_handle_cleanup

Conversation

@alan-george-lk

@alan-george-lk alan-george-lk commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Before you submit your PR

Make sure the following is true before submitting your PR:

  • I have read the contributing guidelines and validated that this PR will be accepted.
  • I have read and followed the principles regarding breaking changes, testing, and code quality.

PR description

This PR resolves an issue observed in the downstream C++ SDK during repeated unit/integration test runs, where the FFI handle state was not completely cleared and caused issues for different test cases intended to be atomic, but within the same process boundary. This specifically raised a crash in PlatformAudio tests.

Breaking changes

N/A

MSRV

If the PR modifies the crate's MSRV (Minimum Supported Rust Version), document it here.

Testing

Validated in a downstream C++ SDK run against this branch.

Async

We want the project to be runtime-agnostic, so please reuse what's already in livekit-runtime and feel free to add anything missing. It's ok to use Tokio directly, when writing unit tests, if necessary. When testing, do not use artificial delays for the state to "catch up"; instead, respect the event flow and subscribe properly using channels or other mechanisms.

@github-actions

Copy link
Copy Markdown
Contributor

Changeset

The following package versions will be affected by this PR:

Package Bump
livekit-ffi patch

@alan-george-lk alan-george-lk force-pushed the bugfix/ffi_handle_cleanup branch from 860b96d to 9209583 Compare June 16, 2026 22:44
@alan-george-lk alan-george-lk marked this pull request as ready for review June 16, 2026 22:45
Comment thread livekit-ffi/src/server/mod.rs
Comment thread livekit-ffi/src/server/mod.rs Outdated
.iter()
.filter(|handle| handle.value().is::<platform_audio::FfiPlatformAudio>())
.count();
log::debug!(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless we add an explicit API to shutdown the PlatformAudio, otherwise it is expected that some platform_audio_handles will remain valid here.

@alan-george-lk alan-george-lk force-pushed the bugfix/ffi_handle_cleanup branch from 6881168 to 9361264 Compare June 26, 2026 22:58
Clear remaining FFI handles during dispose so native resources are
released across repeated initialize/shutdown cycles.

Stop and detach platform/synthetic audio I/O before peer connection
factory teardown so macOS CaptureWorkerThread cannot deliver frames
into AudioTransportImpl while transports are being destroyed. Close
rooms before dropping track handles and stop platform capture when
releasing the platform ADM reference.

Co-authored-by: Cursor <cursoragent@cursor.com>
@alan-george-lk alan-george-lk force-pushed the bugfix/ffi_handle_cleanup branch from 6f46fcd to b933f90 Compare June 26, 2026 23:16
@alan-george-lk

Copy link
Copy Markdown
Contributor Author

Moving this to #1193, closing this one.

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.

4 participants