containers.execute() fails on Python3.12+

Bug #2084973 reported by Miha Purg
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
python-ws4py (Ubuntu)
Confirmed
Undecided
Unassigned

Bug Description

When trying to execute a command on a container with pylxd (on Ubuntu 24.04.1 / Python 3.12.3), the following exception is raised:

```
Traceback (most recent call last):
  File "/home/user/test.py", line 20, in <module>
    instance.execute(['hostname'])
  File "/usr/lib/python3/dist-packages/pylxd/models/container.py", line 412, in execute
    stdin.connect()
  File "/usr/lib/python3/dist-packages/ws4py/client/__init__.py", line 214, in connect
    self.sock = ssl.wrap_socket(self.sock, **self.ssl_options)
                ^^^^^^^^^^^^^^^
AttributeError: module 'ssl' has no attribute 'wrap_socket'

```

To reproduce:
1) snap install lxd
2) lxd init --auto
3) lxc config set core.https_address :8443
4) apt install python3-pylxd
5) Run below script
```
#!/usr/bin/env python3

import pylxd
import subprocess

subprocess.run("openssl req -x509 -newkey rsa:2048 -keyout lxd.key -nodes -out lxd.crt -subj '/CN=lxd.local'", shell=True)
lxd_token = subprocess.run("lxc config trust add --name test -q", capture_output=True, shell=True, text=True).stdout.strip()

# setup container
subprocess.run("lxc launch images:alpine/3.20 testc", shell=True)

client = pylxd.Client(endpoint='https://127.0.0.1:8443',
                      cert=('lxd.crt', 'lxd.key'),
                      verify='/var/snap/lxd/common/lxd/server.crt')

client.authenticate(lxd_token)
instance = client.containers.get('testc')
instance.execute(['hostname'])
```

The issue is with the `python3-ws4py` dependency calling `ssl.wrap_socket()`, which has been deprecated since 3.7 and removed in Python 3.12. Upstream has provided a patch, but not yet a release:
- https://github.com/Lawouach/WebSocket-for-Python/pull/283
- https://github.com/Lawouach/WebSocket-for-Python/issues/287

OS: Ubuntu 24.04.1
Python: 3.12.3
python3-pylxd: 2.2.10-3
python3-ws4py: 0.5.1+dfsg1-2

Thanks,
Miha

Miha Purg (mihap)
description: updated
Miha Purg (mihap)
summary: - containers.exeute() fails on Python3.12+
+ containers.execute() fails on Python3.12+
Revision history for this message
Simon Déziel (sdeziel) wrote :

@Miha, thanks for the simple reproducer steps, that's really useful. I moved the bug to ws4py as pyLXD itself is just a consumer of this lib.

affects: python-pylxd (Ubuntu) → python-ws4py (Ubuntu)
Revision history for this message
Simon Déziel (sdeziel) wrote (last edit ):

I can confirm the issue with the reproducer steps. Manually applying https://github.com/Lawouach/WebSocket-for-Python/pull/283/files to ws4py fixes the issue.

Changed in python-ws4py (Ubuntu):
status: New → Confirmed
Revision history for this message
Miha Purg (mihap) wrote :

Hi Simon, ok.
It was my initial plan to open it under ws4py but since the bug breaks pylxd functionality substantially, and is present on a default LTS installation and doesn't have a simple mitigation, I opened the bug there for better visibility.

Revision history for this message
Miha Purg (mihap) wrote :

Linking related issue on github: https://github.com/canonical/pylxd/issues/585

Revision history for this message
Simon Déziel (sdeziel) wrote :

The fix is included upstream in version 0.6.0: https://github.com/Lawouach/WebSocket-for-Python/releases/tag/0.6.0

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.