Repository: spyder-ide/spyder

Issue:
Spyder 5.4.1 is closing alone when saving files and auto-formatting on save is enabled
<!--- **PLEASE READ:** When submitting here, please ensure you've completed the following checklist and checked the boxes to confirm. Issue reports without it may be closed. Thanks! --->

### Issue Report Checklist

* [ ] Searched the [issues page](https://github.com/spyder-ide/spyder/issues?q=is%3Aissue) for similar reports
* [ ] Read the relevant sections of the [Spyder Troubleshooting Guide](https://github.com/spyder-ide/spyder/wiki/Troubleshooting-Guide-and-FAQ) and followed its advice
* [ ] Reproduced the issue after updating with ``conda update spyder`` (or ``pip``, if not using Anaconda)
* [ ] Could not reproduce inside ``jupyter qtconsole`` (if console-related)
* [ ] Tried basic troubleshooting (if a bug/error)
    * [ ] Restarted Spyder
    * [ ] Reset preferences with ``spyder --reset``
    * [ ] Reinstalled the latest version of [Anaconda](https://www.anaconda.com/download/)
    * [ ] Tried the other applicable steps from the Troubleshooting Guide
* [ ] Completed the **Problem Description**, **Steps to Reproduce** and **Version** sections below


## Problem Description
Spyder is closing when saving files. Toolbars are often disabled after that. I'm started using spyder 5.4.0 again and it is normal. 
The same error occurs in python 3.8, 3.9 and 3.10

### What steps reproduce the problem?

1. 
2. 
3. 

### What is the expected output? What do you see instead?



### Paste Traceback/Error Below (if applicable)
<!--- Copy from error dialog or View > Panes > Internal Console --->

```python-traceback

PASTE TRACEBACK HERE

```

## Versions
<!--- You can get this information from Help > About Spyder...
or (if Spyder won't launch) the "conda list" command
from the Anaconda Prompt/Terminal/command line. --->

* Spyder version: 5.4.1  (conda)
* Python version: 3.10.8 64-bit
* Qt version: 5.15.6
* PyQt5 version: 5.15.7
* Operating System: Linux 5.15.79.1-microsoft-standard-WSL2

### Dependencies
<!--- Please go to the menu entry Help > Dependencies,
press the Copy to clipboard button and paste below --->

<details>
```
# Name                    Version                   Build  Channel
_ipython_minor_entry_point 8.7.0                hb6b4a82_0    conda-forge
_libgcc_mutex             0.1                 conda_forge    conda-forge
_openmp_mutex             4.5                       2_gnu    conda-forge
alabaster                 0.7.12                     py_0    conda-forge
alsa-lib                  1.2.8                h166bdaf_0    conda-forge
anyio                     3.6.2                    pypi_0    pypi
argon2-cffi               21.3.0                   pypi_0    pypi
argon2-cffi-bindings      21.2.0                   pypi_0    pypi
arrow                     1.2.3              pyhd8ed1ab_0    conda-forge
astroid                   2.12.13         py310hff52083_0    conda-forge
asttokens                 2.2.1              pyhd8ed1ab_0    conda-forge
atomicwrites              1.4.1              pyhd8ed1ab_0    conda-forge
attr                      2.5.1                h166bdaf_1    conda-forge
attrs                     22.2.0             pyh71513ae_0    conda-forge
autopep8                  1.6.0              pyhd8ed1ab_1    conda-forge
babel                     2.11.0             pyhd8ed1ab_0    conda-forge
backcall                  0.2.0              pyh9f0ad1d_0    conda-forge
backports                 1.0                pyhd8ed1ab_3    conda-forge
backports.functools_lru_cache 1.6.4              pyhd8ed1ab_0    conda-forge
beautifulsoup4            4.11.1             pyha770c72_0    conda-forge
binaryornot               0.4.4                      py_1    conda-forge
black                     22.10.0         py310hff52083_2    conda-forge
bleach                    5.0.1              pyhd8ed1ab_0    conda-forge
brotlipy                  0.7.0           py310h5764c6d_1005    conda-forge
bzip2                     1.0.8                h7f98852_4    conda-forge
ca-certificates           2022.12.7            ha878542_0    conda-forge
category-encoders         2.5.1.post0              pypi_0    pypi
certifi                   2022.12.7          pyhd8ed1ab_0    conda-forge
cffi                      1.15.1          py310h255011f_3    conda-forge
chardet                   5.1.0           py310hff52083_0    conda-forge
charset-normalizer        2.1.1              pyhd8ed1ab_0    conda-forge
click                     8.1.3           unix_pyhd8ed1ab_2    conda-forge
cloudpickle               2.2.0              pyhd8ed1ab_0    conda-forge
colorama                  0.4.6              pyhd8ed1ab_0    conda-forge
comm                      0.1.2              pyhd8ed1ab_0    conda-forge
contourpy                 1.0.6                    pypi_0    pypi
cookiecutter              2.1.1              pyh6c4a22f_0    conda-forge
cryptography              38.0.4          py310h600f1e7_0    conda-forge
cycler                    0.11.0                   pypi_0    pypi
dask                      2022.12.1                pypi_0    pypi
dbus                      1.13.6               h5008d03_3    conda-forge
debugpy                   1.6.4           py310hd8f1fbe_0    conda-forge
decorator                 5.1.1              pyhd8ed1ab_0    conda-forge
defusedxml                0.7.1              pyhd8ed1ab_0    conda-forge
diff-match-patch          20200713           pyh9f0ad1d_0    conda-forge
dill                      0.3.6              pyhd8ed1ab_1    conda-forge
distributed               2022.12.1                pypi_0    pypi
docstring-to-markdown     0.11               pyhd8ed1ab_0    conda-forge
docutils                  0.19            py310hff52083_1    conda-forge
entrypoints               0.4                pyhd8ed1ab_0    conda-forge
executing                 1.2.0              pyhd8ed1ab_0    conda-forge
expat                     2.5.0                h27087fc_0    conda-forge
feather-format            0.4.1                    pypi_0    pypi
featurewiz                0.2.4                    pypi_0    pypi
fftw                      3.3.10          nompi_hf0379b8_106    conda-forge
flake8                    6.0.0              pyhd8ed1ab_0    conda-forge
font-ttf-dejavu-sans-mono 2.37                 hab24e00_0    conda-forge
font-ttf-inconsolata      3.000                h77eed37_0    conda-forge
font-ttf-source-code-pro  2.038                h77eed37_0    conda-forge
font-ttf-ubuntu           0.83                 hab24e00_0    conda-forge
fontconfig                2.14.1               hc2a2eb6_0    conda-forge
fonts-conda-ecosystem     1                             0    conda-forge
fonts-conda-forge         1                             0    conda-forge
fonttools                 4.38.0                   pypi_0    pypi
fqdn                      1.5.1                    pypi_0    pypi
freetype                  2.12.1               hca18f0e_1    conda-forge
fsspec                    2022.11.0                pypi_0    pypi
gettext                   0.21.1               h27087fc_0    conda-forge
giflib                    5.2.1                h36c2ea0_2    conda-forge
glib                      2.74.1               h6239696_1    conda-forge
glib-tools                2.74.1               h6239696_1    conda-forge
gst-plugins-base          1.21.3               h4243ec0_1    conda-forge
gstreamer                 1.21.3               h25f0c4b_1    conda-forge
gstreamer-orc             0.4.33               h166bdaf_0    conda-forge
heapdict                  1.0.1                    pypi_0    pypi
icu                       70.1                 h27087fc_0    conda-forge
idna                      3.4                pyhd8ed1ab_0    conda-forge
imagesize                 1.4.1              pyhd8ed1ab_0    conda-forge
imbalanced-learn          0.10.1                   pypi_0    pypi
importlib-metadata        5.2.0              pyha770c72_0    conda-forge
importlib_metadata        5.2.0                hd8ed1ab_0    conda-forge
importlib_resources       5.10.1             pyhd8ed1ab_1    conda-forge
inflection                0.5.1              pyh9f0ad1d_0    conda-forge
intervaltree              3.0.2                      py_0    conda-forge
ipykernel                 6.19.4             pyh210e3f2_0    conda-forge
ipython                   8.7.0              pyh41d4057_0    conda-forge
ipython_genutils          0.2.0                      py_1    conda-forge
ipywidgets                8.0.4                    pypi_0    pypi
isoduration               20.11.0                  pypi_0    pypi
isort                     5.11.4             pyhd8ed1ab_0    conda-forge
jack                      1.9.21               h583fa2b_2    conda-forge
jaraco.classes            3.2.3              pyhd8ed1ab_0    conda-forge
jedi                      0.18.2             pyhd8ed1ab_0    conda-forge
jeepney                   0.8.0              pyhd8ed1ab_0    conda-forge
jellyfish                 0.9.0           py310h5764c6d_2    conda-forge
jinja2                    3.1.2              pyhd8ed1ab_1    conda-forge
jinja2-time               0.2.0              pyhd8ed1ab_3    conda-forge
joblib                    1.2.0                    pypi_0    pypi
jpeg                      9e                   h166bdaf_2    conda-forge
jsonpointer               2.3                      pypi_0    pypi
jsonschema                4.17.3             pyhd8ed1ab_0    conda-forge
jupyter                   1.0.0                    pypi_0    pypi
jupyter-console           6.4.4                    pypi_0    pypi
jupyter-events            0.5.0                    pypi_0    pypi
jupyter-server            2.0.6                    pypi_0    pypi
jupyter-server-terminals  0.4.3                    pypi_0    pypi
jupyter_client            7.4.8              pyhd8ed1ab_0    conda-forge
jupyter_core              5.1.1           py310hff52083_0    conda-forge
jupyterlab-widgets        3.0.5                    pypi_0    pypi
jupyterlab_pygments       0.2.2              pyhd8ed1ab_0    conda-forge
keyring                   23.13.1         py310hff52083_0    conda-forge
keyutils                  1.6.1                h166bdaf_0    conda-forge
kiwisolver                1.4.4                    pypi_0    pypi
krb5                      1.20.1               h81ceb04_0    conda-forge
lame                      3.100             h166bdaf_1003    conda-forge
lazy-object-proxy         1.8.0           py310h5764c6d_0    conda-forge
ld_impl_linux-64          2.39                 hcc3a1bd_1    conda-forge
lerc                      4.0.0                h27087fc_0    conda-forge
libblas                   3.9.0           16_linux64_openblas    conda-forge
libcap                    2.66                 ha37c62d_0    conda-forge
libcblas                  3.9.0           16_linux64_openblas    conda-forge
libclang                  15.0.6          default_h2e3cab8_0    conda-forge
libclang13                15.0.6          default_h3a83d3e_0    conda-forge
libcups                   2.3.3                h36d4200_3    conda-forge
libdb                     6.2.32               h9c3ff4c_0    conda-forge
libdeflate                1.14                 h166bdaf_0    conda-forge
libedit                   3.1.20191231         he28a2e2_2    conda-forge
libevent                  2.1.10               h28343ad_4    conda-forge
libffi                    3.4.2                h7f98852_5    conda-forge
libflac                   1.4.2                h27087fc_0    conda-forge
libgcc-ng                 12.2.0              h65d4601_19    conda-forge
libgcrypt                 1.10.1               h166bdaf_0    conda-forge
libgfortran-ng            12.2.0              h69a702a_19    conda-forge
libgfortran5              12.2.0              h337968e_19    conda-forge
libglib                   2.74.1               h606061b_1    conda-forge
libgomp                   12.2.0              h65d4601_19    conda-forge
libgpg-error              1.45                 hc0c96e0_0    conda-forge
libiconv                  1.17                 h166bdaf_0    conda-forge
liblapack                 3.9.0           16_linux64_openblas    conda-forge
libllvm15                 15.0.6               h63197d8_0    conda-forge
libnsl                    2.0.0                h7f98852_0    conda-forge
libogg                    1.3.4                h7f98852_1    conda-forge
libopenblas               0.3.21          pthreads_h78a6416_3    conda-forge
libopus                   1.3.1                h7f98852_1    conda-forge
libpng                    1.6.39               h753d276_0    conda-forge
libpq                     15.1                 hb675445_2    conda-forge
libsndfile                1.1.0                hcb278e6_1    conda-forge
libsodium                 1.0.18               h36c2ea0_1    conda-forge
libspatialindex           1.9.3                h9c3ff4c_4    conda-forge
libsqlite                 3.40.0               h753d276_0    conda-forge
libstdcxx-ng              12.2.0              h46fd767_19    conda-forge
libsystemd0               252                  h2a991cd_0    conda-forge
libtiff                   4.5.0                h82bc61c_0    conda-forge
libtool                   2.4.7                h27087fc_0    conda-forge
libudev1                  252                  h166bdaf_0    conda-forge
libuuid                   2.32.1            h7f98852_1000    conda-forge
libvorbis                 1.3.7                h9c3ff4c_0    conda-forge
libwebp                   1.2.4                h1daa5a0_1    conda-forge
libwebp-base              1.2.4                h166bdaf_0    conda-forge
libxcb                    1.13              h7f98852_1004    conda-forge
libxkbcommon              1.0.3                he3ba5ed_0    conda-forge
libxml2                   2.10.3               h7463322_0    conda-forge
libzlib                   1.2.13               h166bdaf_4    conda-forge
lightgbm                  3.3.3                    pypi_0    pypi
locket                    1.0.0                    pypi_0    pypi
lz4-c                     1.9.3                h9c3ff4c_1    conda-forge
markupsafe                2.1.1           py310h5764c6d_2    conda-forge
matplotlib                3.6.2                    pypi_0    pypi
matplotlib-inline         0.1.6              pyhd8ed1ab_0    conda-forge
mccabe                    0.7.0              pyhd8ed1ab_0    conda-forge
mistune                   2.0.4              pyhd8ed1ab_0    conda-forge
more-itertools            9.0.0              pyhd8ed1ab_0    conda-forge
mpg123                    1.31.1               h27087fc_0    conda-forge
msgpack                   1.0.4                    pypi_0    pypi
mypy_extensions           0.4.3           py310hff52083_6    conda-forge
mysql-common              8.0.31               h26416b9_0    conda-forge
mysql-libs                8.0.31               hbc51c84_0    conda-forge
nbclassic                 0.4.8                    pypi_0    pypi
nbclient                  0.7.2              pyhd8ed1ab_0    conda-forge
nbconvert                 7.2.7              pyhd8ed1ab_0    conda-forge
nbconvert-core            7.2.7              pyhd8ed1ab_0    conda-forge
nbconvert-pandoc          7.2.7              pyhd8ed1ab_0    conda-forge
nbformat                  5.7.1              pyhd8ed1ab_0    conda-forge
ncurses                   6.3                  h27087fc_1    conda-forge
nest-asyncio              1.5.6              pyhd8ed1ab_0    conda-forge
networkx                  2.8.8                    pypi_0    pypi
notebook                  6.5.2                    pypi_0    pypi
notebook-shim             0.2.2                    pypi_0    pypi
nspr                      4.35                 h27087fc_0    conda-forge
nss                       3.82                 he02c5a1_0    conda-forge
numpy                     1.24.1          py310h08bbf29_0    conda-forge
numpydoc                  1.5.0              pyhd8ed1ab_0    conda-forge
openssl                   3.0.7                h0b41bf4_1    conda-forge
packaging                 22.0               pyhd8ed1ab_0    conda-forge
pandas                    1.5.2           py310h769672d_0    conda-forge
pandoc                    2.19.2               h32600fe_1    conda-forge
pandocfilters             1.5.0              pyhd8ed1ab_0    conda-forge
parso                     0.8.3              pyhd8ed1ab_0    conda-forge
partd                     1.3.0                    pypi_0    pypi
pathspec                  0.10.3             pyhd8ed1ab_0    conda-forge
patsy                     0.5.3                    pypi_0    pypi
pcre2                     10.40                hc3806b6_0    conda-forge
pexpect                   4.8.0              pyh1a96a4e_2    conda-forge
pickleshare               0.7.5                   py_1003    conda-forge
pillow                    9.3.0                    pypi_0    pypi
pip                       22.3.1             pyhd8ed1ab_0    conda-forge
pkgutil-resolve-name      1.3.10             pyhd8ed1ab_0    conda-forge
platformdirs              2.6.2              pyhd8ed1ab_0    conda-forge
pluggy                    1.0.0              pyhd8ed1ab_5    conda-forge
ply                       3.11                       py_1    conda-forge
polars                    0.15.8          py310h4426083_0    conda-forge
prometheus-client         0.15.0                   pypi_0    pypi
prompt-toolkit            3.0.36             pyha770c72_0    conda-forge
psutil                    5.9.4           py310h5764c6d_0    conda-forge
pthread-stubs             0.4               h36c2ea0_1001    conda-forge
ptyprocess                0.7.0              pyhd3deb0d_0    conda-forge
pulseaudio                16.1                 h126f2b6_0    conda-forge
pure_eval                 0.2.2              pyhd8ed1ab_0    conda-forge
pyarrow                   7.0.0                    pypi_0    pypi
pycodestyle               2.10.0             pyhd8ed1ab_0    conda-forge
pycparser                 2.21               pyhd8ed1ab_0    conda-forge
pydocstyle                6.1.1              pyhd8ed1ab_0    conda-forge
pyflakes                  3.0.1              pyhd8ed1ab_0    conda-forge
pygments                  2.13.0             pyhd8ed1ab_0    conda-forge
pylint                    2.15.9             pyhd8ed1ab_0    conda-forge
pylint-venv               2.3.0              pyhd8ed1ab_0    conda-forge
pyls-spyder               0.4.0              pyhd8ed1ab_0    conda-forge
pyopenssl                 22.1.0             pyhd8ed1ab_0    conda-forge
pyparsing                 3.0.9                    pypi_0    pypi
pyqt                      5.15.7          py310h29803b5_2    conda-forge
pyqt5-sip                 12.11.0         py310hd8f1fbe_2    conda-forge
pyqtwebengine             5.15.7          py310h29803b5_2    conda-forge
pyrsistent                0.19.2          py310h5764c6d_0    conda-forge
pysocks                   1.7.1              pyha2e5f31_6    conda-forge
python                    3.10.8          h4a9ceb5_0_cpython    conda-forge
python-dateutil           2.8.2              pyhd8ed1ab_0    conda-forge
python-fastjsonschema     2.16.2             pyhd8ed1ab_0    conda-forge
python-json-logger        2.0.4                    pypi_0    pypi
python-lsp-black          1.2.1              pyhd8ed1ab_0    conda-forge
python-lsp-jsonrpc        1.0.0              pyhd8ed1ab_0    conda-forge
python-lsp-server         1.7.0                hd8ed1ab_0    conda-forge
python-lsp-server-base    1.7.0              pyhd8ed1ab_0    conda-forge
python-slugify            7.0.0              pyhd8ed1ab_0    conda-forge
python_abi                3.10                    3_cp310    conda-forge
pytoolconfig              1.2.4              pyhd8ed1ab_1    conda-forge
pytz                      2022.7             pyhd8ed1ab_0    conda-forge
pyxdg                     0.28               pyhd8ed1ab_0    conda-forge
pyyaml                    6.0             py310h5764c6d_5    conda-forge
pyzmq                     24.0.1          py310h330234f_1    conda-forge
qdarkstyle                3.0.3              pyhd8ed1ab_0    conda-forge
qstylizer                 0.2.2              pyhd8ed1ab_0    conda-forge
qt-main                   5.15.6               hf6cd601_5    conda-forge
qt-webengine              5.15.4               h325cec9_3    conda-forge
qtawesome                 1.2.2              pyhd8ed1ab_0    conda-forge
qtconsole                 5.4.0              pyhd8ed1ab_0    conda-forge
qtconsole-base            5.4.0              pyha770c72_0    conda-forge
qtpy                      2.3.0              pyhd8ed1ab_0    conda-forge
readline                  8.1.2                h0f457ee_0    conda-forge
requests                  2.28.1             pyhd8ed1ab_1    conda-forge
rfc3339-validator         0.1.4                    pypi_0    pypi
rfc3986-validator         0.1.1                    pypi_0    pypi
rope                      1.6.0              pyhd8ed1ab_0    conda-forge
rtree                     1.0.1           py310hbdcdc62_1    conda-forge
scikit-learn              1.2.0                    pypi_0    pypi
scipy                     1.9.3                    pypi_0    pypi
seaborn                   0.12.2                   pypi_0    pypi
secretstorage             3.3.3           py310hff52083_1    conda-forge
send2trash                1.8.0                    pypi_0    pypi
setuptools                65.6.3             pyhd8ed1ab_0    conda-forge
sip                       6.7.5           py310hd8f1fbe_0    conda-forge
six                       1.16.0             pyh6c4a22f_0    conda-forge
sniffio                   1.3.0                    pypi_0    pypi
snowballstemmer           2.2.0              pyhd8ed1ab_0    conda-forge
sortedcontainers          2.4.0              pyhd8ed1ab_0    conda-forge
soupsieve                 2.3.2.post1        pyhd8ed1ab_0    conda-forge
sphinx                    6.0.0              pyhd8ed1ab_1    conda-forge
sphinxcontrib-applehelp   1.0.2                      py_0    conda-forge
sphinxcontrib-devhelp     1.0.2                      py_0    conda-forge
sphinxcontrib-htmlhelp    2.0.0              pyhd8ed1ab_0    conda-forge
sphinxcontrib-jsmath      1.0.1                      py_0    conda-forge
sphinxcontrib-qthelp      1.0.3                      py_0    conda-forge
sphinxcontrib-serializinghtml 1.1.5              pyhd8ed1ab_2    conda-forge
spyder                    5.4.1           py310hff52083_1    conda-forge
spyder-kernels            2.4.1           unix_pyhd8ed1ab_0    conda-forge
stack_data                0.6.2              pyhd8ed1ab_0    conda-forge
statsmodels               0.13.5                   pypi_0    pypi
tblib                     1.7.0                    pypi_0    pypi
terminado                 0.17.1                   pypi_0    pypi
text-unidecode            1.3                        py_0    conda-forge
textdistance              4.5.0              pyhd8ed1ab_0    conda-forge
threadpoolctl             3.1.0                    pypi_0    pypi
three-merge               0.1.1              pyh9f0ad1d_0    conda-forge
tinycss2                  1.2.1              pyhd8ed1ab_0    conda-forge
tk                        8.6.12               h27826a3_0    conda-forge
toml                      0.10.2             pyhd8ed1ab_0    conda-forge
tomli                     2.0.1              pyhd8ed1ab_0    conda-forge
tomlkit                   0.11.6             pyha770c72_0    conda-forge
toolz                     0.12.0                   pypi_0    pypi
tornado                   6.2             py310h5764c6d_1    conda-forge
tqdm                      4.64.1                   pypi_0    pypi
traitlets                 5.8.0              pyhd8ed1ab_0    conda-forge
typing                    3.10.0.0           pyhd8ed1ab_0    conda-forge
typing-extensions         4.4.0                hd8ed1ab_0    conda-forge
typing_extensions         4.4.0              pyha770c72_0    conda-forge
tzdata                    2022g                h191b570_0    conda-forge
ujson                     5.5.0           py310hd8f1fbe_1    conda-forge
unidecode                 1.3.6              pyhd8ed1ab_0    conda-forge
uri-template              1.2.0                    pypi_0    pypi
urllib3                   1.26.13            pyhd8ed1ab_0    conda-forge
watchdog                  2.2.0           py310hff52083_0    conda-forge
wcwidth                   0.2.5              pyh9f0ad1d_2    conda-forge
webcolors                 1.12                     pypi_0    pypi
webencodings              0.5.1                      py_1    conda-forge
websocket-client          1.4.2                    pypi_0    pypi
whatthepatch              1.0.3              pyhd8ed1ab_0    conda-forge
wheel                     0.38.4             pyhd8ed1ab_0    conda-forge
widgetsnbextension        4.0.5                    pypi_0    pypi
wrapt                     1.14.1          py310h5764c6d_1    conda-forge
wurlitzer                 3.0.3              pyhd8ed1ab_0    conda-forge
xcb-util                  0.4.0                h166bdaf_0    conda-forge
xcb-util-image            0.4.0                h166bdaf_0    conda-forge
xcb-util-keysyms          0.4.0                h166bdaf_0    conda-forge
xcb-util-renderutil       0.3.9                h166bdaf_0    conda-forge
xcb-util-wm               0.4.1                h166bdaf_0    conda-forge
xgboost                   1.7.2                    pypi_0    pypi
xlrd                      2.0.1                    pypi_0    pypi
xorg-libxau               1.0.9                h7f98852_0    conda-forge
xorg-libxdmcp             1.1.3                h7f98852_0    conda-forge
xz                        5.2.6                h166bdaf_0    conda-forge
yaml                      0.2.5                h7f98852_2    conda-forge
yapf                      0.32.0             pyhd8ed1ab_0    conda-forge
zeromq                    4.3.4                h9c3ff4c_1    conda-forge
zict                      2.2.0                    pypi_0    pypi
zipp                      3.11.0             pyhd8ed1ab_0    conda-forge
zstd                      1.5.2                h6239696_4    conda-forge
```
</details>


Discussion:
Hi @arturdaraujo thank you for the feedback! I tried installing Spyder 5.4.1 in a new env on Windows and creating a new file but I was unable to reproduce this. Checking your info setup seems like you are running Spyder using WSL2? Maybe this is only happening for WSL2 setups? If you have any other info that would help reproduce/understand why this is happening with your setup let us know!
I was saving files from the windows directory and using a dropbox folder (quite a complicated setup, I know).
So after installing spyder on WSL (which is quite complicated, I will post my notes later for installing conda and spyder on WSL). After installed, I use my directory from Windows, so the path is something like this: 

/mnt/c/Users/arturdaraujo/Dropbox/python_directory/tsfa

After creating a few variables and running a bit, when I try to save it spyder dies but the RAM memory usage keeps on the being used. Anyway quite complicated. My point here is more of make you aware in case more of this shows up. But I can't run 5.4.1 on my setup.  
Also, thanks for being answering for such problems. In case you plan to actually do it, this is how I use Spyder on WSL:

(GENERAL INSTRUCTIONS)
https://medium.com/@macasaetjohn/setting-up-a-spyder-environment-with-wsl-bb83716a44f3

(INSTALL)
libgl1-mesa-dev
https://github.com/open-mmlab/mmsegmentation/issues/1567

(INSTALL)
sudo apt-get install libgl1 

sudo apt-get install libxcursor-dev libxcomposite-dev libxdamage-dev libxrandr-dev libxtst-dev libxss-dev libdbus-1-dev libevent-dev libfontconfig1-dev libcap-dev libpulse-dev libudev-dev libpci-dev libnss3-dev libasound2-dev libegl1-mesa-dev gperf bison nodejs

https://stackoverflow.com/questions/64664094/i-cannot-use-opencv2-and-received-importerror-libgl-so-1-cannot-open-shared-obj

https://stackoverflow.com/questions/66607640/unable-to-import-qtwebenginepage-from-pyqt5-on-aws-linux

(Solution for Brazilian keyboard)
sudo apt install x11-xkb-utils

nano ~/.bashrc
- This code in the end

WAYLAND_DISPLAY=“wayland-1”
setxkbmap -model abnt2 -layout br -variant abnt2
WAYLAND_DISPLAY=“wayland-1”

THanks for the detailed explanation! Maybe this could be related with https://github.com/spyder-ide/spyder/issues/19458 too? I will try to reproduce this at some point :+1:

Also, maybe could be worthy to see if something (error tracebacks or more info) gets captured by running Spyder with the debug info verbose flag (`--debug-info verbose`)
@arturdaraujo or @LeifParr, please go to this entry in the File menu

![imagen](https://user-images.githubusercontent.com/365293/138761482-598fa96c-13f8-4dde-ae9a-e18ac20a7c69.png)

and select `Verbose`:

![imagen](https://user-images.githubusercontent.com/365293/138761597-e833892d-888c-4e93-99fd-6acf98dde1cc.png)

After Spyder restarts, go to the `Tools > Debug logs` menu

![imagen](https://user-images.githubusercontent.com/365293/138761756-2af03a24-21c6-47bb-8354-1f2ec87a1e83.png)

check where those three files are placed, open their respective directories with the Windows file explorer, save a file in Spyder to cause a crash, and finally upload them here so that we can check their contents.

Thanks!

*Note*: The numbers at the end of `server_python` and `transport_python` will be different in your case. Don't worry about it.
I opened Spyder, tweaked around a bit until it crashed and then followed your steps to upload the log files.

Here it is:
[spyder-debug.log](https://github.com/spyder-ide/spyder/files/10332779/spyder-debug.log)
[server_python_7658.log](https://github.com/spyder-ide/spyder/files/10332788/server_python_7658.log)
[transport_python_7658.log](https://github.com/spyder-ide/spyder/files/10332789/transport_python_7658.log)

@ccordoba12 Thanks for feedback. I am actually not able to get Spyder into debug mode, so no files from me yet. I press the restart in debug mode -> verbose, then Spyder closes but does not restart. 
@arturdaraujo, I don't see any problem in your logs. Perhaps the bug you're experiencing is related to your Dropbox usage. Could you try to do the same in another directory not inside Dropbox?

@LeifParr, sorry to hear that. @dalthviz knows a command to start Spyder in debug mode from a terminal, but he's on vacations right now, so you'll have to wait a couple of days for that.
Hi, Just joining from the above comment on #5344 

I'm not using dropbox. All my activities are on c drive. It seemed to only be .py file saving. My guess is the auto linting.
I was able to reproduce this on Linux and I think I know the cause of it (it should be PR #20167). I'll come up with a fix in our 5.4.2 version, to be released as soon as possible due to the severity of this bug.
Thanks!

Resolving patch:
diff --git a/spyder/plugins/editor/widgets/codeeditor.py b/spyder/plugins/editor/widgets/codeeditor.py
--- a/spyder/plugins/editor/widgets/codeeditor.py
+++ b/spyder/plugins/editor/widgets/codeeditor.py
@@ -526,7 +526,7 @@ def __init__(self, parent=None):
         self.format_on_save = False
         self.format_eventloop = QEventLoop(None)
         self.format_timer = QTimer(self)
-        self.__cursor_position_before_format = 0
+        self.__line_number_before_format = 0
 
         # Mouse tracking
         self.setMouseTracking(True)
@@ -1804,10 +1804,6 @@ def handle_go_to_definition(self, position):
     # -------------------------------------------------------------------------
     def format_document_or_range(self):
         """Format current document or selected text."""
-        # Save current cursor position to restore it after the current text has
-        # been replaced by the auto-formatted one.
-        self.__cursor_position_before_format = self.textCursor().position()
-
         if self.has_selected_text() and self.range_formatting_enabled:
             self.format_document_range()
         else:
@@ -1816,6 +1812,8 @@ def format_document_or_range(self):
     @schedule_request(method=CompletionRequestTypes.DOCUMENT_FORMATTING)
     def format_document(self):
         """Format current document."""
+        self.__line_number_before_format = self.textCursor().blockNumber()
+
         if not self.formatting_enabled:
             return
         if self.formatting_in_progress:
@@ -1849,6 +1847,8 @@ def format_document(self):
     @schedule_request(method=CompletionRequestTypes.DOCUMENT_RANGE_FORMATTING)
     def format_document_range(self):
         """Format selected text."""
+        self.__line_number_before_format = self.textCursor().blockNumber()
+
         if not self.range_formatting_enabled or not self.has_selected_text():
             return
         if self.formatting_in_progress:
@@ -2007,18 +2007,22 @@ def _apply_document_edits(self, edits):
             # Insert formatted text in place of the previous one
             cursor.insertText(merged_text)
 
-            # Don't run the lines below when testing because they give
-            # segfaults.
-            if not running_under_pytest():
-                # Restore previous cursor position and center it.
-                # Fixes spyder-ide/spyder#19958
-                cursor.setPosition(self.__cursor_position_before_format)
-                self.setTextCursor(cursor)
-                self.centerCursor()
-
             # End text insertion
             cursor.endEditBlock()
 
+            # Restore previous cursor line and center it.
+            # Fixes spyder-ide/spyder#19958
+            if self.__line_number_before_format < self.blockCount():
+                self.moveCursor(QTextCursor.Start)
+                cursor = self.textCursor()
+                cursor.movePosition(
+                    QTextCursor.Down,
+                    QTextCursor.MoveAnchor,
+                    self.__line_number_before_format
+                )
+                self.setTextCursor(cursor)
+                self.centerCursor()
+
     # ---- LSP: Code folding
     # -------------------------------------------------------------------------
     def compute_whitespace(self, line):


Test patch:
diff --git a/spyder/plugins/editor/widgets/tests/test_formatting.py b/spyder/plugins/editor/widgets/tests/test_formatting.py
--- a/spyder/plugins/editor/widgets/tests/test_formatting.py
+++ b/spyder/plugins/editor/widgets/tests/test_formatting.py
@@ -9,9 +9,11 @@
 # Standard library imports
 import os
 import os.path as osp
+import random
 
 # Third party imports
 import pytest
+from qtpy.QtCore import Qt
 from qtpy.QtGui import QTextCursor
 from qtpy.QtWidgets import QMessageBox
 import yapf
@@ -257,3 +259,60 @@ def test_closing_document_formatting(
     code_editor = editorstack.load(str(file_path)).editor
 
     assert code_editor.get_text_with_eol() == expected
+
+
+@pytest.mark.order(1)
+@pytest.mark.parametrize('formatter', [autopep8, black])
+def test_formatting_on_save(completions_editor, formatter, qtbot):
+    """
+    Check that auto-formatting on save works as expected and that we restore
+    the current line after doing it.
+
+    This includes a regression test for issue spyder-ide/spyder#19958
+    """
+    file_path, editorstack, code_editor, completion_plugin = completions_editor
+    text, expected = get_formatter_values(formatter, newline='\n')
+
+    # Set formatter
+    editorstack.set_format_on_save(True)
+    CONF.set(
+        'completions',
+        ('provider_configuration', 'lsp', 'values', 'formatting'),
+        formatter
+    )
+
+    with qtbot.waitSignal(completion_plugin.sig_editor_rpc):
+        completion_plugin.after_configuration_update([])
+    qtbot.wait(2000)
+
+    # Set text in editor
+    code_editor.set_text(text)
+
+    # Notify changes
+    with qtbot.waitSignal(
+            code_editor.completions_response_signal, timeout=30000):
+        code_editor.document_did_change()
+
+    # Set a random current line
+    current_line = random.randint(0, code_editor.blockCount())
+    code_editor.moveCursor(QTextCursor.Start)
+    cursor = code_editor.textCursor()
+    cursor.movePosition(QTextCursor.Down, QTextCursor.MoveAnchor, current_line)
+    code_editor.setTextCursor(cursor)
+    qtbot.wait(500)
+
+    # Make a simple change to the file
+    code_editor.moveCursor(QTextCursor.EndOfLine)
+    qtbot.keyPress(code_editor, Qt.Key_Space)
+    qtbot.wait(500)
+
+    # Save the file
+    with qtbot.waitSignal(
+            code_editor.completions_response_signal, timeout=30000):
+        editorstack.save(force=True)
+    qtbot.wait(500)
+
+    # Check that auto-formatting was applied on save and that we restored the
+    # previous line.
+    assert code_editor.get_text_with_eol() == expected
+    assert code_editor.textCursor().blockNumber() == current_line