poljar / weechat-matrix

Weechat Matrix protocol script written in python
Other
957 stars 119 forks source link

SSLContext.set_npn_protocols broken in Python 3.10 #308

Closed diabonas closed 2 years ago

diabonas commented 2 years ago

When trying to load the weechat-matrix script with Python 3.10, it fails with the following error:

19:23 python: stdout/stderr (matrix): Traceback (most recent call last):
19:23 python: stdout/stderr (matrix): File "/home/jonas/.local/share/weechat/python/autoload/weechat-matrix.py", line 708, in <module>
19:23 python: stdout/stderr (matrix): create_default_server(G.CONFIG)
19:23 python: stdout/stderr (matrix): File "/usr/lib/python3.10/site-packages/matrix/server.py", line 1989, in create_default_server
19:23 python: stdout/stderr (matrix): server = MatrixServer("matrix_org", config_file._ptr)
19:23 python: stdout/stderr (matrix): File "/usr/lib/python3.10/site-packages/matrix/server.py", line 307, in __init__
19:23 python: stdout/stderr (matrix): self.ssl_context.set_npn_protocols(["h2", "http/1.1"])
19:23 python: stdout/stderr (matrix): File "/usr/lib/python3.10/ssl.py", line 546, in set_npn_protocols
19:23 python: stdout/stderr (matrix): self._set_npn_protocols(protos)
19:23 python: stdout/stderr (matrix): AttributeError: 'SSLContext' object has no attribute '_set_npn_protocols'. Did you mean: 'set_npn_protocols'?

This is due to an upstream issue in Python 3.10 with SSLContext.set_npn_protocols, reported upstream as bpo-46067.

Since the function is deprecated in Python 3.10 anyway, the broken function can simply be removed, cf. e.g. the patch I use for the Arch Linux package:

From 4e585d5f4628e6fbeba9ec4560b440d731e076f5 Mon Sep 17 00:00:00 2001
From: Jonas Witschel <diabonas@archlinux.org>
Date: Sat, 11 Dec 2021 19:47:16 +0100
Subject: [PATCH] server: remove set_npn_protocols()

This function is deprecated in favour of set_alpn_protocols(), which is already
called, and is currently broken in Python 3.10, so remove it altogether.
---
 matrix/server.py | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/matrix/server.py b/matrix/server.py
index dda861e..0f34c1e 100644
--- a/matrix/server.py
+++ b/matrix/server.py
@@ -303,11 +303,6 @@ class MatrixServer(object):
         # Enable http2 negotiation on the ssl context.
         self.ssl_context.set_alpn_protocols(["h2", "http/1.1"])

-        try:
-            self.ssl_context.set_npn_protocols(["h2", "http/1.1"])
-        except NotImplementedError:
-            pass
-
         self.address = None
         self.homeserver = None
         self.client = None  # type: Optional[HttpClient]
-- 
2.34.1
ghost commented 2 years ago

Hello, Is there a fix ?

diabonas commented 2 years ago

For upstream Python 3.10 there is no fix as far as I am aware, but you can work around the issue in weechat-matrix by applying the patch I included above.

poljar commented 2 years ago

When trying to load the weechat-matrix script with Python 3.10, it fails with the following error:

19:23 python: stdout/stderr (matrix): Traceback (most recent call last):
19:23 python: stdout/stderr (matrix): File "/home/jonas/.local/share/weechat/python/autoload/weechat-matrix.py", line 708, in <module>
19:23 python: stdout/stderr (matrix): create_default_server(G.CONFIG)
19:23 python: stdout/stderr (matrix): File "/usr/lib/python3.10/site-packages/matrix/server.py", line 1989, in create_default_server
19:23 python: stdout/stderr (matrix): server = MatrixServer("matrix_org", config_file._ptr)
19:23 python: stdout/stderr (matrix): File "/usr/lib/python3.10/site-packages/matrix/server.py", line 307, in __init__
19:23 python: stdout/stderr (matrix): self.ssl_context.set_npn_protocols(["h2", "http/1.1"])
19:23 python: stdout/stderr (matrix): File "/usr/lib/python3.10/ssl.py", line 546, in set_npn_protocols
19:23 python: stdout/stderr (matrix): self._set_npn_protocols(protos)
19:23 python: stdout/stderr (matrix): AttributeError: 'SSLContext' object has no attribute '_set_npn_protocols'. Did you mean: 'set_npn_protocols'?

This is due to an upstream issue in Python 3.10 with SSLContext.set_npn_protocols, reported upstream as bpo-46067.

Since the function is deprecated in Python 3.10 anyway, the broken function can simply be removed, cf. e.g. the patch I use for the Arch Linux package:

From 4e585d5f4628e6fbeba9ec4560b440d731e076f5 Mon Sep 17 00:00:00 2001
From: Jonas Witschel <diabonas@archlinux.org>
Date: Sat, 11 Dec 2021 19:47:16 +0100
Subject: [PATCH] server: remove set_npn_protocols()

This function is deprecated in favour of set_alpn_protocols(), which is already
called, and is currently broken in Python 3.10, so remove it altogether.
---
 matrix/server.py | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/matrix/server.py b/matrix/server.py
index dda861e..0f34c1e 100644
--- a/matrix/server.py
+++ b/matrix/server.py
@@ -303,11 +303,6 @@ class MatrixServer(object):
         # Enable http2 negotiation on the ssl context.
         self.ssl_context.set_alpn_protocols(["h2", "http/1.1"])

-        try:
-            self.ssl_context.set_npn_protocols(["h2", "http/1.1"])
-        except NotImplementedError:
-            pass
-
         self.address = None
         self.homeserver = None
         self.client = None  # type: Optional[HttpClient]
-- 
2.34.1

Hi, could you turn this into a pull request?

diabonas commented 2 years ago

Sure, done: #309