Skip to content

fix: free observers and factories safely while they are still in use - #528

Merged
devopvoid merged 4 commits into
mainfrom
fix/dispose-edge-cases
Nov 4, 2026
Merged

devopvoid merged 4 commits into
mainfrom
fix/dispose-edge-cases

Conversation

@devopvoid

Copy link
Copy Markdown
Owner
  • An observer registered while its object is disposed is freed exactly once, and none stays registered after dispose(). Only one thread talks to WebRTC per object: a concurrent registration hands its observer over and returns, without waiting for WebRTC to take it. Covers RTCDataChannel, RTCDtlsTransport, RTCDtmfSender, RTCRtpSender and RTCRtpReceiver.
  • A track listener may dispose its track or remove a listener: removed observers are not called anymore and are freed once the notification under way is over. This used to crash the JVM.
  • PeerConnectionFactory.dispose() during a pending close() returns at once, and the factory is freed after the connection. This used to crash the JVM as well.

Registering, removing and disposing through one Java object may now run on several threads at once. WebRTC is told by one of them at a time, without anyone waiting, and an observer is freed once WebRTC has a newer one. A dispose frees the observer only once no registration through the object is running any more, so none is left registered.
WebRTC calls the observers of a track from a copy of their list, so a listener that disposed the track, or removed a listener, freed observers that were still to be called, its own included. They are now detached at once and freed, with the track, after the notification; detached ones are no longer called.
A connection closed from a callback, or while another thread was still in one of its methods, is freed later on the factory's signaling thread. A factory disposed meanwhile stopped and freed that thread first. The close now holds the factory's handle until the connection is freed, so the factory is released after it.
@devopvoid
devopvoid merged commit 0deb274 into main Nov 4, 2026
26 checks passed
@devopvoid
devopvoid deleted the fix/dispose-edge-cases branch November 4, 2026 21:55
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