Skip to content

Cancel publication jobs when preparing for a full reconnect - #999

Open
adrian-niculescu wants to merge 1 commit into
livekit:mainfrom
adrian-niculescu:fix-reconnect-publication-job-leak
Open

Cancel publication jobs when preparing for a full reconnect#999
adrian-niculescu wants to merge 1 commit into
livekit:mainfrom
adrian-niculescu:fix-reconnect-publication-job-leak

Conversation

@adrian-niculescu

@adrian-niculescu adrian-niculescu commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

prepareForFullReconnect() clears the track publications, so the unpublishTrack() calls in republishTracks() can't find the old publications and never cancel their jobs. Each full reconnect therefore leaks the feature collector of every published audio track, which keeps running and sends UpdateLocalAudioTrack with the sid of the replaced publication whenever the track's features change.

Cancel the jobs when preparing for the reconnect. Republishing creates fresh jobs for the new publications.

The reconnect coroutine, publishAudioTrack, and unpublishTrack can all touch the jobs map from different threads, so job registration and draining are serialized: retiring paths remove the publication from trackPublications before draining its job, and registration re-checks under the same monitor that the publication is still current. That re-check alone can't tell a stale publish from a live one, since a publish accepted on the old session can resume after the teardown and re-insert its publication. Acceptances are therefore validated against a full reconnect epoch: each signal connection captures the epoch current when it opens, a TrackPublished response carries its connection's epoch, preparing a full reconnect advances the epoch, and the collector only starts when the acceptance's epoch is still current. Closing the old connection doesn't join an in-flight response handler, so a TrackPublished that already passed the connection guard can complete after the close and the epoch advance; carrying the epoch with the response keeps that acceptance attributed to the old session. A publish accepted by the replacement session registers normally, and soft reconnects preserve publishes without advancing the epoch.

Split out of #987, which was closed since its main feature turned out to be unnecessary (#375). Five regression tests pin the behavior: a features change after a full reconnect produces exactly one update, a publish completing across reconnect preparation sends no stale-sid updates, an acceptance delivered by the old session after preparation is treated as stale, a publish accepted by the replacement session keeps reporting feature changes, and feature updates flow unaffected across a soft reconnect.

@changeset-bot

changeset-bot Bot commented Aug 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f961eb3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
client-sdk-android Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@adrian-niculescu
adrian-niculescu force-pushed the fix-reconnect-publication-job-leak branch 5 times, most recently from 610cf41 to 2ec04f0 Compare August 13, 2026 13:45
@adrian-niculescu
adrian-niculescu force-pushed the fix-reconnect-publication-job-leak branch from 2ec04f0 to f961eb3 Compare August 13, 2026 14:02
@adrian-niculescu
adrian-niculescu marked this pull request as ready for review August 13, 2026 14:18

@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: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

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