Skip to content

Fix "local track published for unknown sid" warning - #288

Merged
alan-george-lk merged 1 commit into
mainfrom
alan/bugfix-local-track-publication-ordering
Oct 15, 2026
Merged

alan-george-lk merged 1 commit into
mainfrom
alan/bugfix-local-track-publication-ordering

Conversation

@alan-george-lk

@alan-george-lk alan-george-lk commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

I have been noticing a log warning pop up a lot in tests/CI:

[2026-10-14 23:25:13.115] [livekit] [warning] local_track_published for unknown sid: TR_As5Q7gL6gGeAob

This resolves the issue. Changed integration tests to wait for track publish to ensure they're not dropped, as previously this warning meant the callbacks would be skipped as it was unknown.


Before the fix

  1. The application calls LocalParticipant::publishTrack().
  2. C++ sends the publish request and blocks in fut.get().
  3. Rust publishes the track and sends PublishTrackCallback.
  4. The C++ FFI thread handles that callback and calls promise.set_value(publication).
  5. The future is now ready, but the application thread has only been made runnable—it has not necessarily resumed.
  6. Rust sends LocalTrackPublished.
  7. Room::onEvent() immediately searches published_tracks_by_sid_.
  8. If the application thread has not resumed yet, the map is empty. C++ logs local_track_published for unknown sid and drops the delegate event.
  9. The application thread eventually resumes and installs the publication, but it is too late.

A mutex prevents simultaneous unsafe access, but it does not guarantee that the application thread performs the write before the FFI thread performs the lookup.

What the PR changes

FfiClient::publishTrackAsync() now accepts an on_success function. Its event handler executes that function before satisfying the promise:

        const proto::OwnedTrackPublication& pub = cb.publication();
        try {
          if (on_success) {
            on_success(pub);
          }
          pr.set_value(pub);
        } catch (...) {
          pr.set_exception(std::current_exception());
        }

LocalParticipant uses it to install all publication state directly on the FFI callback thread:

  auto fut = FfiClient::instance().publishTrackAsync(
      static_cast<std::uint64_t>(participant_handle), static_cast<std::uint64_t>(track_handle), options,
      [this, track](const proto::OwnedTrackPublication& owned_pub) {
        auto publication = std::make_shared<LocalTrackPublication>(owned_pub);
        const std::scoped_lock<std::mutex> guard(published_tracks_mutex_);
        published_tracks_by_sid_[publication->sid()] = track;
        track->setPublication(publication);
      });

  // Publication state is installed on the FFI thread before this future is
  // completed and before Rust can emit LocalTrackPublished.
  (void)fut.get();

After the fix

  1. Rust sends PublishTrackCallback.
  2. The C++ FFI thread constructs the publication.
  3. It inserts the track into published_tracks_by_sid_.
  4. It calls track->setPublication().
  5. Only then does it satisfy the promise.
  6. The publish callback returns to Rust.
  7. Rust sends LocalTrackPublished.
  8. Room::onEvent() finds the fully initialized publication and invokes onLocalTrackPublished.

The important change is not merely “moving work to another thread.” It places the state update inside an existing ordering boundary: Rust guarantees that LocalTrackPublished is sent after the publish callback finishes.

The integration tests now register a delegate and verify that both events are actually received:

  • LocalTrackPublishSidTest.PublishVideoTrackAssignsSid
  • LocalTrackPublishSidTest.PublishAudioTrackAssignsSid

The condition variable also makes the tests safe whether the event arrives before or after the test begins waiting.


Fixes BOT-577

@alan-george-lk
alan-george-lk marked this pull request as ready for review October 15, 2026 03:07
@alan-george-lk
alan-george-lk requested a review from a team as a code owner October 15, 2026 03:08

@devin-ai-integration devin-ai-integration Bot left a comment

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.

🔍 Devin Review: 2 flags

Not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)

Devin Review

@alan-george-lk
alan-george-lk merged commit 83e19fd into main Oct 15, 2026
52 checks passed
@alan-george-lk
alan-george-lk deleted the alan/bugfix-local-track-publication-ordering branch October 15, 2026 14:34
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.

3 participants