diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/client/PeerImpl.java b/webrtc/src/main/java/dev/onvoid/webrtc/client/PeerImpl.java index 2e619bde5..8470bbae3 100644 --- a/webrtc/src/main/java/dev/onvoid/webrtc/client/PeerImpl.java +++ b/webrtc/src/main/java/dev/onvoid/webrtc/client/PeerImpl.java @@ -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 closeInternal() { if (closing.compareAndSet(false, true)) { @@ -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 ended, PeerClosedException error) { closedInternal.complete(null); events.execute(() -> { diff --git a/webrtc/src/test/java/dev/onvoid/webrtc/client/RuntimeCloseTests.java b/webrtc/src/test/java/dev/onvoid/webrtc/client/RuntimeCloseTests.java index 0514f7a54..9d6217499 100644 --- a/webrtc/src/test/java/dev/onvoid/webrtc/client/RuntimeCloseTests.java +++ b/webrtc/src/test/java/dev/onvoid/webrtc/client/RuntimeCloseTests.java @@ -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 created = CompletableFuture.supplyAsync(() -> runtime.createPeer(manualOptions())); + control.runUntil(created::isDone, TIMEOUT_SECONDS); + + PeerImpl peer = (PeerImpl) await(created); + + CompletableFuture 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.