Ubuntu, VSCode - autopep8 client: couldn't create connection to server.

## 1. solution

```
$ sudo apt reinstall python3
```


<details><summary>details</summary>

I encountered an error.

```
domodomodomo@home:~$ sudo apt reinstall python3
[sudo] password for domodomodomo: 
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 libldb2 : Breaks: samba-libs (< 2:4.19.0~) but 2:4.15.13+dfsg-0ubuntu1.6 is to be installed
E: Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages.
domodomodomo@home:~$ 
```

I asked ChatGPT 4 for details, and install samba-libs.

```
sudo apt update
sudo apt --fix-broken install
sudo apt install --only-upgrade samba-libs
```

After above commands and restart, GNOME was disabled and GUI has gone. So I reinstall GNOME.

```
sudo apt install ubuntu-desktop
```

</details>



## 2. situation

After upgrading from Ubuntu `22.04` to `24.04`, the autopep8 extension in VSCode started throwing errors. It was found that changing the Python interpreter (e.g. `/usr/bin/python3`, `venv/bin/python`) resolved the issue, so I reinstalled the Python interpreter, and the problem was resolved.



## 3. cause
I found out that running `lsp_server.py` causes an error. The specific reason for it is not known.

```
$ python3 /home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/tool/lsp_server.py
```


## 4. logs

You can see an error when running `lsp_server.py` below.

<details><summary>logs</summary>

```
2024-04-26 06:39:00.926 [info] Name: autopep8
2024-04-26 06:39:00.926 [info] Module: autopep8
2024-04-26 06:39:00.926 [info] Default formatter is set to ms-python.autopep8 for workspace /home/domodomodomo/Projects/hello-nihao
2024-04-26 06:39:00.926 [info] Python extension loading
2024-04-26 06:39:00.926 [info] Waiting for interpreter from python extension.
2024-04-26 06:39:00.926 [info] No interpreter found from setting autopep8.interpreter
2024-04-26 06:39:00.926 [info] Getting interpreter from ms-python.python extension for workspace /home/domodomodomo/Projects/hello-nihao
2024-04-26 06:39:00.926 [info] Python extension loaded
2024-04-26 06:39:00.926 [info] Interpreter from ms-python.python extension for /home/domodomodomo/Projects/hello-nihao: /usr/bin/python3
2024-04-26 06:39:00.926 [info] No interpreter found from setting autopep8.interpreter
2024-04-26 06:39:00.926 [info] Getting interpreter from ms-python.python extension for workspace /home/domodomodomo/Projects/hello-nihao
2024-04-26 06:39:00.926 [info] Interpreter from ms-python.python extension for /home/domodomodomo/Projects/hello-nihao: /usr/bin/python3
2024-04-26 06:39:00.926 [info] Server run command: /usr/bin/python3 /home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/tool/lsp_server.py
2024-04-26 06:39:00.926 [info] Server: Start requested.
2024-04-26 06:39:02.523 [info] Traceback (most recent call last):
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/tool/lsp_server.py", line 48, in <module>
    from pygls import server, uris, workspace
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/pygls/server.py", line 35, in <module>
    import cattrs
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/cattrs/__init__.py", line 1, in <module>
    from .converters import BaseConverter, Converter, GenConverter, UnstructureStrategy
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/cattrs/converters.py", line 25, in <module>
    from ._compat import (
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/cattrs/_compat.py", line 40, in <module>
    from exceptiongroup import ExceptionGroup
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/exceptiongroup/__init__.py", line 20, in <module>
    from ._formatting import (
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/exceptiongroup/_formatting.py", line 394, in <module>
    assert sys.excepthook is apport_python_hook.apport_excepthook
AssertionError

2024-04-26 06:39:02.643 [info] [Error - 6:39:02 AM] Server initialization failed.
2024-04-26 06:39:02.643 [info]   Message: Pending response rejected since connection got disposed
  Code: -32097 
2024-04-26 06:39:02.643 [info] [Info  - 6:39:02 AM] Connection to server got closed. Server will restart.
2024-04-26 06:39:02.643 [info] true
2024-04-26 06:39:02.644 [info] [Error - 6:39:02 AM] autopep8 client: couldn't create connection to server.
2024-04-26 06:39:02.644 [info]   Message: Pending response rejected since connection got disposed
  Code: -32097 
2024-04-26 06:39:03.724 [info] Traceback (most recent call last):
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/tool/lsp_server.py", line 48, in <module>
    from pygls import server, uris, workspace
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/pygls/server.py", line 35, in <module>
    import cattrs
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/cattrs/__init__.py", line 1, in <module>

2024-04-26 06:39:03.725 [info]     from .converters import BaseConverter, Converter, GenConverter, UnstructureStrategy
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/cattrs/converters.py", line 25, in <module>
    from ._compat import (
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/cattrs/_compat.py", line 40, in <module>
    from exceptiongroup import ExceptionGroup
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/exceptiongroup/__init__.py", line 20, in <module>
    from ._formatting import (
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/exceptiongroup/_formatting.py", line 394, in <module>

2024-04-26 06:39:03.726 [info]     assert sys.excepthook is apport_python_hook.apport_excepthook
AssertionError

2024-04-26 06:39:03.863 [info] [Error - 6:39:03 AM] Server initialization failed.
2024-04-26 06:39:03.863 [info]   Message: Pending response rejected since connection got disposed
  Code: -32097 
2024-04-26 06:39:03.863 [info] [Info  - 6:39:03 AM] Connection to server got closed. Server will restart.
2024-04-26 06:39:03.863 [info] true
2024-04-26 06:39:03.863 [info] [Error - 6:39:03 AM] autopep8 client: couldn't create connection to server.
2024-04-26 06:39:03.863 [info]   Message: Pending response rejected since connection got disposed
  Code: -32097 
2024-04-26 06:39:03.863 [error] Server: Start failed: Error: Pending response rejected since connection got disposed
2024-04-26 06:39:06.534 [info] Traceback (most recent call last):
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/tool/lsp_server.py", line 48, in <module>
    from pygls import server, uris, workspace
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/pygls/server.py", line 35, in <module>
    import cattrs
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/cattrs/__init__.py", line 1, in <module>
    from .converters import BaseConverter, Converter, GenConverter, UnstructureStrategy
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/cattrs/converters.py", line 25, in <module>
    from ._compat import (
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/cattrs/_compat.py", line 40, in <module>
    from exceptiongroup import ExceptionGroup
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/exceptiongroup/__init__.py", line 20, in <module>
    from ._formatting import (
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/exceptiongroup/_formatting.py", line 394, in <module>
    assert sys.excepthook is apport_python_hook.apport_excepthook
AssertionError

2024-04-26 06:39:06.557 [info] [Error - 6:39:06 AM] Server initialization failed.
2024-04-26 06:39:06.557 [info]   Message: Pending response rejected since connection got disposed
  Code: -32097 
2024-04-26 06:39:06.557 [info] [Info  - 6:39:06 AM] Connection to server got closed. Server will restart.
2024-04-26 06:39:06.557 [info] true
2024-04-26 06:39:06.559 [info] [Error - 6:39:06 AM] autopep8 client: couldn't create connection to server.
2024-04-26 06:39:06.559 [info]   Message: Pending response rejected since connection got disposed
  Code: -32097 
2024-04-26 06:39:06.560 [info] [Error - 6:39:06 AM] Restarting server failed
2024-04-26 06:39:06.560 [info]   Message: Pending response rejected since connection got disposed
  Code: -32097 
2024-04-26 06:39:08.576 [info] Traceback (most recent call last):
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/tool/lsp_server.py", line 48, in <module>

2024-04-26 06:39:08.577 [info]     from pygls import server, uris, workspace
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/pygls/server.py", line 35, in <module>

2024-04-26 06:39:08.577 [info]     import cattrs
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/cattrs/__init__.py", line 1, in <module>

2024-04-26 06:39:08.577 [info]     from .converters import BaseConverter, Converter, GenConverter, UnstructureStrategy

2024-04-26 06:39:08.578 [info]   File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/cattrs/converters.py", line 25, in <module>

2024-04-26 06:39:08.578 [info]     from ._compat import (

2024-04-26 06:39:08.579 [info]   File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/cattrs/_compat.py", line 40, in <module>

2024-04-26 06:39:08.579 [info]     from exceptiongroup import ExceptionGroup
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/exceptiongroup/__init__.py", line 20, in <module>

2024-04-26 06:39:08.579 [info]     from ._formatting import (
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/exceptiongroup/_formatting.py", line 394, in <module>

2024-04-26 06:39:08.580 [info]     assert sys.excepthook is apport_python_hook.apport_excepthook
AssertionError

2024-04-26 06:39:08.701 [info] [Error - 6:39:08 AM] Server initialization failed.
2024-04-26 06:39:08.701 [info]   Message: Pending response rejected since connection got disposed
  Code: -32097 
2024-04-26 06:39:08.701 [info] [Info  - 6:39:08 AM] Connection to server got closed. Server will restart.
2024-04-26 06:39:08.701 [info] true
2024-04-26 06:39:08.701 [info] [Error - 6:39:08 AM] autopep8 client: couldn't create connection to server.
2024-04-26 06:39:08.712 [info]   Message: Pending response rejected since connection got disposed
  Code: -32097 
2024-04-26 06:39:08.713 [info] [Error - 6:39:08 AM] Restarting server failed
2024-04-26 06:39:08.713 [info]   Message: Pending response rejected since connection got disposed
  Code: -32097 
2024-04-26 06:39:09.781 [info] Traceback (most recent call last):
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/tool/lsp_server.py", line 48, in <module>

2024-04-26 06:39:09.781 [info]     from pygls import server, uris, workspace
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/pygls/server.py", line 35, in <module>
    import cattrs
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/cattrs/__init__.py", line 1, in <module>
    from .converters import BaseConverter, Converter, GenConverter, UnstructureStrategy
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/cattrs/converters.py", line 25, in <module>
    from ._compat import (
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/cattrs/_compat.py", line 40, in <module>
    from exceptiongroup import ExceptionGroup
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/exceptiongroup/__init__.py", line 20, in <module>

2024-04-26 06:39:09.781 [info]     from ._formatting import (
  File "/home/domodomodomo/.vscode/extensions/ms-python.autopep8-2024.0.0/bundled/libs/exceptiongroup/_formatting.py", line 394, in <module>
    assert sys.excepthook is apport_python_hook.apport_excepthook
AssertionError

2024-04-26 06:39:09.894 [info] [Error - 6:39:09 AM] Server initialization failed.
2024-04-26 06:39:09.894 [info]   Message: Pending response rejected since connection got disposed
  Code: -32097 
2024-04-26 06:39:09.894 [info] [Error - 6:39:09 AM] The autopep8 server crashed 5 times in the last 3 minutes. The server will not be restarted. See the output for more information.
2024-04-26 06:39:09.895 [info] [Error - 6:39:09 AM] autopep8 client: couldn't create connection to server.
2024-04-26 06:39:09.895 [info]   Message: Pending response rejected since connection got disposed
  Code: -32097 
2024-04-26 06:39:09.898 [info] [Error - 6:39:09 AM] Restarting server failed
2024-04-26 06:39:09.898 [info]   Message: Pending response rejected since connection got disposed
  Code: -32097 
```

</details>