microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.29k stars 1.17k forks source link

Python Debugger terminates prematurely when debugging a module #1710

Closed roblourens closed 6 years ago

roblourens commented 6 years ago

From @ldorigo on May 19, 2018 15:3

Issue Type: Bug

When I launch my module from the command-line (pthon3 -m labot.mitm.launch), it executes as expected (it's a bot for a game: it stays active in the background and listens to network packets). When I launch it from the debugger (see configuration below), it immediately terminates with the following output:

cd /Users/ldorigo/Dropbox/Documents/Prog/LaBot ; env "PYTHONIOENCODING=UTF-8" "PYTHONUNBUFFERED=1" /usr/local/bin/python3 /Users/ldorigo/.vscode/extensions/ms-python.python-2018.4.0/pythonFiles/PythonTools/visualstudio_py_launcher.py /Users/ldorigo/Dropbox/Documents/Prog/LaBot 49588 34806ad9-833a-4524-8cd6-18ca4aa74f14 RedirectOutput,RedirectOutput -m labot.mitm.launch
[1]    42687 terminated  env "PYTHONIOENCODING=UTF-8" "PYTHONUNBUFFERED=1" /usr/local/bin/python3

I put relevant files below, please let me know if anything else is needed. If you want to try it yourself, the used code is at https://github.com/louisabraham/LaBot , feel free to clone and try it out (although it might take some setting up).

Launch Configuration:

        {
            "name": "Python: mitm",
            "type": "python",
            "request": "launch",
            "module": "labot.mitm.launch"
        },

Relevant files:

labot.mitm.launch:

#!/usr/bin/env python3

from .proxy import startProxyServer
from .proxychains import launchDofus

# to interrupt : httpd.shutdown()
httpd = startProxyServer()

# you can launch several instances
# of dofus with the same httpd
# the bot will be launched after the connexion
dofus = launchDofus()

proxy.py :

from socketserver import ThreadingMixIn
from http.server import HTTPServer, BaseHTTPRequestHandler
import socket
from threading import Thread

from .bridge import Bridge, PrintingMsgBridgeHandler

connection_servers = ['213.248.126.39', '213.248.126.40']

class ThreadingHTTPServer(ThreadingMixIn, HTTPServer):
    debug = False

class ProxyRequestHandler(BaseHTTPRequestHandler):

    def do_CONNECT(self):
        address = self.path.split(':', 1)
        address[1] = int(address[1]) or 443
        try:
            coServ = socket.create_connection(address, timeout=self.timeout)
        except Exception as e:
            self.send_error(502)
            return
        self.send_response(200, 'Connection Established')
        self.end_headers()

        # Here the main function is launched
        Bridge(self.connection, coServ, PrintingMsgBridgeHandler).run()
        # It MUST run in sync mode (not threaded)
        # The bot will run in sync mode and will have a Bridge in async
        # You can use the condition
        # if address[0] not in connection_servers
        # to start your bot only after the connexion

        # VERY IMPORTANT
        self.close_connection = True

    def log_message(self, format, *args):
        """To avoid silly debug messages"""
        if self.server.debug:
            super().log_message(format, *args)

def startProxyServer(port=8000):
    httpd = ThreadingHTTPServer(('localhost', port), ProxyRequestHandler)
    Thread(None, httpd.serve_forever).start()
    return httpd

proxychains.py:

#!/usr/bin/env python3

import sys
import os.path
import os
import subprocess
from ..logs import logger

confpath = os.path.join(os.path.dirname(__file__), 'proxychains.conf')
command = ['proxychains4', '-f', confpath,
           '/Applications/Dofus.app/Contents/Data/Dofus.app/Contents/MacOS/Dofus']
newenv = {**os.environ,
          'DYLD_INSERT_LIBRARIES':
          '/System/Library/Frameworks/OpenGL.framework/Resources/GLEngine.bundle/GLEngine'}

def launchDofus(stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE):
    """to interrupt : dofus.terminate()"""
    return subprocess.Popen(command, env=newenv, stdin=stdin, stdout=stdout, stderr=stderr)

if __name__ == '__main__':
    logger.setLevel('INFO')
    dofus = launchDofus(stdout=sys.stdout, stderr=sys.stderr)

VS Code version: Code 1.23.1 (d0182c3417d225529c6d5ad24b7572815d0de9ac, 2018-05-10T16:03:31.083Z) OS version: Darwin x64 17.5.0

System Info |Item|Value| |---|---| |CPUs|Intel(R) Core(TM) i7-3720QM CPU @ 2.60GHz (8 x 2600)| |GPU Status|2d_canvas: unavailable_software
flash_3d: enabled
flash_stage3d: enabled
flash_stage3d_baseline: enabled
gpu_compositing: enabled
multiple_raster_threads: enabled_on
native_gpu_memory_buffers: enabled
rasterization: unavailable_software
video_decode: enabled
video_encode: enabled
vpx_decode: enabled
webgl: enabled
webgl2: enabled| |Load (avg)|2, 2, 2| |Memory (System)|16.00GB (0.37GB free)| |Process Argv|/Applications/Visual Studio Code.app/Contents/MacOS/Electron| |Screen Reader|no| |VM|0%|
Extensions (35) Extension|Author (truncated)|Version ---|---|--- bracket-pair-colorizer|Coe|1.0.50 vscode-markdownlint|Dav|0.17.0 beautify|Hoo|1.3.0 quokka-vscode|Wal|1.0.124 html-css-class-completion|Zig|1.17.1 vscode-color|ans|0.4.5 vscode-nextgenas|bow|0.11.1 mapfile|chi|0.0.1 npm-intellisense|chr|1.3.0 path-intellisense|chr|1.4.2 gitignore|cod|0.5.0 vscode-eslint|dba|1.4.9 githistory|don|0.4.1 gitlens|eam|8.3.1 vscode-npm-script|eg2|0.3.4 json-tools|eri|1.0.2 semantic-data-model-language|fan|0.0.1 flow-for-vscode|flo|0.8.1 code-runner|for|0.9.3 sass-lint|gle|1.0.2 webpack|jer|2.0.2 intellij-idea-keybindings|k--|0.2.23 gitignore|mic|1.0.1 python|ms-|2018.4.0 debugger-for-chrome|msj|4.4.3 vscode-icons|rob|7.23.0 sass-indented|rob|1.4.9 bash-debug|rog|0.2.0 vscode-sass-format|sas|1.1.4 bash-beautify|sha|0.1.1 stylelint|shi|0.36.0 gitconfig|sid|1.0.1 open-in-browser|tec|1.1.0 shellcheck|tim|0.4.0 markdown-all-in-one|yzh|1.3.0

Copied from original issue: Microsoft/vscode#50160

DonJayamanne commented 6 years ago

@ldorigo

although it might take some setting up

Yes, I'm having trouble setting this up.

ldorigo commented 6 years ago

Sure, sorry. I didn't even realize the readme was in french.

Finally note that everything was developed on/for macOS, so all the paths correspond to a default macOS install - if you're one another environment you will need to change them a bit. Sorry about that, this was meant as a small project between friends - we never really cared about portability :-)

I will try the experimental debugger later when on my other computer.

Thanks for looking into it!

no-response[bot] commented 6 years ago

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.