Index: pyOpenSSL-19.0.0/src/OpenSSL/SSL.py
===================================================================
--- pyOpenSSL-19.0.0.orig/src/OpenSSL/SSL.py
+++ pyOpenSSL-19.0.0/src/OpenSSL/SSL.py
@@ -1,5 +1,6 @@
 import os
 import socket
+import sys
 from sys import platform
 from functools import wraps, partial
 from itertools import count, chain
@@ -1371,7 +1372,11 @@ class Context(object):
         """
         @wraps(callback)
         def wrapper(ssl, alert, arg):
-            callback(Connection._reverse_mapping[ssl])
+            try:
+                callback(Connection._reverse_mapping[ssl])
+            except Exception:
+                sys.excepthook(*sys.exc_info())
+                return _lib.SSL_TLSEXT_ERR_ALERT_FATAL
             return 0
 
         self._tlsext_servername_callback = _ffi.callback(
Index: pyOpenSSL-19.0.0/tests/test_ssl.py
===================================================================
--- pyOpenSSL-19.0.0.orig/tests/test_ssl.py
+++ pyOpenSSL-19.0.0/tests/test_ssl.py
@@ -2096,6 +2096,48 @@ class TestApplicationLayerProtoNegotiati
             with pytest.raises(NotImplementedError):
                 conn.set_alpn_protos(None)
 
+    def test_servername_callback_exception(self, monkeypatch):
+        """
+        When the callback passed to `Context.set_tlsext_servername_callback`
+        raises an exception, ``sys.excepthook`` is called with the exception
+        and the handshake fails with an ``Error``.
+        """
+        exc = TypeError("server name callback failed")
+
+        def servername(conn):
+            raise exc
+
+        excepthook_calls = []
+
+        def custom_excepthook(exc_type, exc_value, exc_tb):
+            excepthook_calls.append((exc_type, exc_value, exc_tb))
+
+        context = Context(SSLv23_METHOD)
+        context.set_tlsext_servername_callback(servername)
+
+        # Necessary to actually accept the connection
+        context.use_privatekey(load_privatekey(FILETYPE_PEM, server_key_pem))
+        context.use_certificate(
+            load_certificate(FILETYPE_PEM, server_cert_pem)
+        )
+
+        # Do a little connection to trigger the logic
+        server = Connection(context, None)
+        server.set_accept_state()
+
+        client = Connection(Context(SSLv23_METHOD), None)
+        client.set_connect_state()
+        client.set_tlsext_host_name(b"foo1.example.com")
+
+        monkeypatch.setattr(sys, "excepthook", custom_excepthook)
+        with pytest.raises(Error):
+            interact_in_memory(server, client)
+
+        assert len(excepthook_calls) == 1
+        assert excepthook_calls[0][0] is TypeError
+        assert excepthook_calls[0][1] is exc
+        assert excepthook_calls[0][2] is not None
+
 
 class TestSession(object):
     """
