diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp
index f690a4103f6d5c9db21fdae28a2fb4d7a93685fc..1eb347700e6114d8cc7e032a5d4f9b02ff301212 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()