From 6f27b7660eb6169dac8316959d63447d7a059dc1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Wed, 14 Apr 2021 15:56:24 -0400
Subject: [PATCH] peer_connection: fix notify_one() crash

This is a bit a hacky patch. The real fix will be to remove underlyingIce()
But tls must be checked before shutdown to avoid to use a non existing object.
Imho what really happen is that the ice is moved in IceSocketEndpoint::shutdown()
so when  TlsSocketEndpoint::Impl is detroyed, the callback is not reset, tls
become nullptr and then we try to shutdown this non existing tls.
However, removing underlyingIce is not straight forward as every shutdown event
must close all layers of the socket, so at first, just fix this issue.

Change-Id: Ifd9c3733e175787b97a48f3c3e282287963018bb
---
 src/peer_connection.cpp | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp
index f690a4103f..1eb347700e 100644
--- a/src/peer_connection.cpp
+++ b/src/peer_connection.cpp
@@ -198,7 +198,11 @@ public:
         };
         tls = std::make_unique<tls::TlsSession>(std::move(ep), tls_param, tls_cbs);
 
-        ep_->underlyingICE()->setOnShutdown([this]() { tls->shutdown(); });
+        if (const auto& ice = ep_->underlyingICE())
+            ice->setOnShutdown([this]() {
+                if (tls)
+                    tls->shutdown();
+            });
     }
 
     Impl(std::unique_ptr<IceSocketEndpoint>&& ep,
@@ -231,10 +235,11 @@ public:
         };
         tls = std::make_unique<tls::TlsSession>(std::move(ep), tls_param, tls_cbs);
 
-        ep_->underlyingICE()->setOnShutdown([this]() {
-            if (tls)
-                tls->shutdown();
-        });
+        if (const auto& ice = ep_->underlyingICE())
+            ice->setOnShutdown([this]() {
+                if (tls)
+                    tls->shutdown();
+            });
     }
 
     ~Impl()
-- 
GitLab