Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion webrtc/src/main/java/dev/onvoid/webrtc/client/PeerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,8 @@ public String toString() {
* Starts closing, once.
*
* @return a future that completes on the control executor once the
* connection is closed, before the handlers hear of it.
* connection is closed and the control tasks queued before that have run,
* before the handlers hear of it.
*/
CompletableFuture<Void> closeInternal() {
if (closing.compareAndSet(false, true)) {
Expand Down Expand Up @@ -1774,6 +1775,19 @@ private void teardown() {

state = PeerState.CLOSED;
rtc.removePeer(this);

// Finished only once the tasks queued before the connection closed
// have run: a callback of WebRTC may have queued one behind this
// teardown, such as a channel the remote side opened, and that task
// still calls into its native objects. The runtime frees the factory,
// and with it WebRTC's threads, as soon as closedInternal completes.
// Nothing is queued for the connection once it has closed.
Runnable finish = () -> finishTeardown(ended, error);

control.execute(finish, finish);
}

private void finishTeardown(List<RemoteTrackImpl> ended, PeerClosedException error) {
closedInternal.complete(null);

events.execute(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,39 @@ void closeWhileOpening() throws Exception {
await(peer.whenClosed());
}

@Test
void closeWaitsForTasksQueuedBehindTeardown() throws Exception {
// A callback of WebRTC queued a task while the teardown waited to run,
// as one does for a channel the remote side opens. The task reaches
// the peer once it has closed and still calls into the native channel,
// whose calls run on the factory's threads: the runtime must not free
// the factory before it ran, as it did once, crashing the process.
ManualExecutor control = new ManualExecutor();
WebRtc runtime = runtime(control);

CompletableFuture<Peer> created = CompletableFuture.supplyAsync(() -> runtime.createPeer(manualOptions()));
control.runUntil(created::isDone, TIMEOUT_SECONDS);

PeerImpl peer = (PeerImpl) await(created);

CompletableFuture<Void> closed = runtime.closeAsync();
control.awaitQueued(TIMEOUT_SECONDS);

AtomicBoolean ran = new AtomicBoolean();
AtomicBoolean closedBefore = new AtomicBoolean();

peer.control.execute(() -> {
ran.set(true);
closedBefore.set(peer.closeInternal().isDone());
});

control.runUntil(closed::isDone, TIMEOUT_SECONDS);
await(closed);

assertTrue(ran.get());
assertFalse(closedBefore.get(), "the peer counted as closed before the task queued behind its teardown ran");
}

@Test
void closeTimeoutReported() throws Exception {
// The peer's teardown never runs, so neither close finishes.
Expand Down
Loading