tornadoweb / tornado

Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed.

Home Page:http://www.tornadoweb.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

autoreload: Conflicts with jetbrains debugger

akoidan opened this issue · comments

To reproduce the issue you can use default example:

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hellof, world!")

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ], debug=True, autoreload=True)

if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

Pycharm starts the script with
/usr/bin/python3.6 /opt/pycharm-eap/helpers/pydev/pydevd.py --multiproc --qt-support=auto --client 127.0.0.1 --port 33261 --file file_name.py
If I edit the file and then save it I receive

Failed to import the site module
Traceback (most recent call last):
  File "/usr/lib/python3.6/site.py", line 73, in <module>
    import os
  File "/usr/lib/python3.6/os.py", line 652, in <module>
    from _collections_abc import MutableMapping
  File "/usr/lib/python3.6/_collections_abc.py", line 288, in <module>
    Iterator.register(str_iterator)
  File "/usr/lib/python3.6/abc.py", line 158, in register
    if issubclass(subclass, cls):
  File "/usr/lib/python3.6/abc.py", line 207, in __subclasscheck__
    ok = cls.__subclasshook__(subclass)
KeyboardInterrupt

This issue reproduces with python 3.6. On python 2.7 autoreload works ok. I also tried to turn off Safe write in pycharm, but that doesn't affect it at all.

It looks like pycharm is trying to do its own automatic reloading when you save the file by sending an interrupt signal to the original process. You don't need two kinds of reloading, so you probably want to turn off one or the other.

Probably related. https://youtrack.jetbrains.com/issue/PY-28604
Notes:

  • autoreload=false doesn't reload the code while in debug
  • If pycharm performs run instead of debug it doesn't run the script via pydevd.py so autoreload works fines.
  • I didn't find a way to turn off sending interrupting signal in pycharm, I guess it's hardcoded in pydevd.py

So atm it's impossible to have autoreloaded code while in debug in pycharm.

Ah, this is just while the debugger is attached? I wouldn't expect that to work at all - reloading the process will at least break the connection to the debugger. If the debugger has a reloading feature then you may be able to use that, but I think you'll need to turn off tornado's autoreload feature to debug. I don't think there's anything we can do about this on the tornado side; it's a question for jetbrains.

Well, it works somehow with python2, I guess there should be a chance get it working with py3

@Deathangel908 actually it was working for python < 3.4 :( You can check https://youtrack.jetbrains.com/issue/PY-14980, there was a workaround

@bdarnell The behavior has changed in 2018.3

It now does

[I 190219 15:17:05 autoreload:199] file.py modified; restarting server
Process finished with exit code 0

Regardless if you debug it or not

python -m ***.server --debug=true
...
[I 190219 15:19:53 autoreload:199] file.py modified; restarting server
Process finished with exit code 0

Adding -m tornado.autoreload -m ***.server trick doesn't help either.

I am having the above issue too!

commented

I'm using pycharm's Attaching to a process to debug, and run tornado server manual.

the attache still broken, but the serve still runing and reload.

I am currently having the same problem running py3.7. I was running intellij 2019.1 and it wouldn't work when I upgraded my application to py3 so I updated intellij ultimate to 2019.3 and it still breaks with KeyboardInterrupt only while using the debugger.

Traceback (most recent call last): File "/Users/robert/Library/Application Support/IntelliJIdea2019.3/python/helpers/pydev/pydevd.py", line 7, in <module> from contextlib import contextmanager File "<frozen importlib._bootstrap>", line 983, in _find_and_load File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 677, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 724, in exec_module File "<frozen importlib._bootstrap_external>", line 818, in get_code File "<frozen importlib._bootstrap_external>", line 916, in get_data KeyboardInterrupt

commented

Faced with the same issue in PyCharm 2021 and python 3.6. Regardless debug or not after any changes the server stops with Process finished with exit code 0
Autoreload works when I start the server from cmd

It is not expected that autoreload would work while a debugger is attached. If you use a debugger (I don't) you should turn off autoreload while debugging. If there's some way to detect the presence of a debugger and automatically disable autoreload while one is present, I would welcome the suggestion. Otherwise, I don't think there's anything for Tornado to do here and any questions should be directed to the debugger vendor (jetbrains, it seems, from every comment posted here).