Python Debugger on VS Code 2019 hangs - python

I'm trying to use the Python debugger on VS Code 2019 on my Mac, but it looks like it is causing the program to hang indefinitely (the print statement never gets executed). If I run the program without the debugger, everything works. I don't have any breakpoints configured either.
Here is my code:
#!/usr/bin/env python3
dummy = input("Enter something: ")
print(dummy)
Edit: I'm not sure if it's related, but when I click the stop button "square", the program doesn't immediately quit. It takes a few seconds, and then I see this in my terminal window:
E+01487.808: Failed to kill Debuggee[PID=2791]
Traceback (most recent call last):
File "/Users/XXXXX/.vscode/extensions/ms-python.python-2020.8.101144/pythonFiles/lib/python/debugpy/launcher/../../debugpy/launcher/debuggee.py", line 160, in kill
os.killpg(process.pid, signal.SIGKILL)
PermissionError: [Errno 1] Operation not permitted
Stack where logged:
File "/Users/XXXXX/.vscode/extensions/ms-python.python-2020.8.101144/pythonFiles/lib/python/debugpy/launcher/../../debugpy/launcher/debuggee.py", line 162, in kill
log.swallow_exception("Failed to kill {0}", describe())
File "/Users/XXXXX/.vscode/extensions/ms-python.python-2020.8.101144/pythonFiles/lib/python/debugpy/launcher/../../debugpy/common/log.py", line 215, in swallow_exception
_exception(format_string, *args, **kwargs)
(.venv) Scripts %

Related

Fabric python script outputting "Encountered a bad command exit code!"

For context:
I have an HP DL380p and the fans are annoyingly loud because of PCIe cards and non HP-drives. So I flashed the iLO (HPE Integrated Lights-Out) on the server with a modded version which allows to edit fan curves. I already tried it manually and it works fine, PCIe cards and CPU never go over 60 °C with my usage. There is problem with the mod however, which makes to if you're SSH connection turns off, the fan command (used to change the fan curves) doesn't work anymore. You have to reset the iLO with the "reset /map1" command, which resets previous fan curves adjustments. Which is why I found fabric and decided to use for this instead of a bash script.
I wrote this code:
from fabric import Connection
import time
connection = Connection(host = "username#ip", connect_kwargs = {"password" : "password"})
connection.run("reset /map1")
time.sleep(40)
connection = Connection(host = "username#ip", connect_kwargs = {"password" : "password"})
connection.run("fan info g")
But every time I run it, I get this error:
reset /map1
status=0
status_tag=COMMAND COMPLETED
Tue Jan 31 11:35:12 2023
Resetting iLO.
CLI session stopped
Traceback (most recent call last):
File "C:\Users\ewenlau\Desktop\silence fan.py", line 4, in <module>
connection.run("reset /map1")
File "<decorator-gen-3>", line 2, in run
File "C:\Users\ewenlau\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\fabric\connection.py", line 27, in opens
return method(self, *args, **kwargs)
File "C:\Users\ewenlau\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\fabric\connection.py", line 739, in run
return self._run(self._remote_runner(), command, **kwargs)
File "C:\Users\ewenlau\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\invoke\context.py", line 98, in _run
return runner.run(command, **kwargs)
File "C:\Users\ewenlau\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\fabric\runners.py", line 75, in run
return super().run(command, **kwargs)
File "C:\Users\ewenlau\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\invoke\runners.py", line 376, in run
return self._run_body(command, **kwargs)
File "C:\Users\ewenlau\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\invoke\runners.py", line 432, in _run_body
return self.make_promise() if self._asynchronous else self._finish()
File "C:\Users\ewenlau\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\invoke\runners.py", line 499, in _finish
raise UnexpectedExit(result)
invoke.exceptions.UnexpectedExit: Encountered a bad command exit code!
Command: 'reset /map1'
Exit code: -1
Stdout: already printed
Stderr: already printed
What I was expecting to happen is this script to return me the current fan speed (fan info g). Instead it outputted the error above. I tried removing the sleep command, nothing changed. I have to note that the reset /map1 command closes the connection host side, and I think the problem comes from there, but I have no idea how to fix it.

Python psutil.wait raise timeout without good reason

I'm facing a strange situation, I've searched on google without any good results.
I'm running a python script as a subprocess from a parent subprocess with nohup using subprocess package:
cmd = list()
cmd.append("nohup")
cmd.append(sys.executable)
cmd.append(os.path.abspath(script))
cmd.append(os.path.abspath(conf_path))
_env = os.environ.copy()
if env:
_env.update({k: str(v) for k, v in env.items()})
p = subprocess.Popen(cmd, env=_env, cwd=os.getcwd())
After some time the parent process exists and the subprocess (the one with the nohup continues to run).
After another minute or two the process with the nohup exits, and with obvious reasons, becomes a zombie.
When running it on local PC with python3.6 and ubuntu 18.04, I manage to run the following code and everything works like a charm:
comp_process = psutil.Process(pid)
if comp_process.status() == "zombie":
comp_status_code = comp_process.wait(timeout=10)
As I said, everything works like a charm, The zombie process removed and I got the status code of the mentioned process.
But for some reason, when doing the SAME at docker container with the SAME python version and Ubuntu version, It fails after the timeout (Doesn't matter if its 10 seconds or 10 minutes)
The error:
psutil.TimeoutExpired timeout after 60 seconds (pid=779)
Traceback (most recent call last): File
"/usr/local/lib/python3.6/dist-packages/psutil/_psposix.py", line 84,
in wait_pid
retpid, status = waitcall() File "/usr/local/lib/python3.6/dist-packages/psutil/_psposix.py", line 75,
in waitcall
return os.waitpid(pid, os.WNOHANG) ChildProcessError: [Errno 10] No child processes
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File ".py", line 41, in
run
comp_status_code = comp_process.wait(timeout=60) File "/usr/local/lib/python3.6/dist-packages/psutil/init.py", line
1383, in wait
return self._proc.wait(timeout) File "/usr/local/lib/python3.6/dist-packages/psutil/_pslinux.py", line
1517, in wrapper
return fun(self, *args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/psutil/_pslinux.py", line
1725, in wait
return _psposix.wait_pid(self.pid, timeout, self._name) File "/usr/local/lib/python3.6/dist-packages/psutil/_psposix.py", line 96,
in wait_pid
delay = check_timeout(delay) File "/usr/local/lib/python3.6/dist-packages/psutil/_psposix.py", line 68,
in check_timeout
raise TimeoutExpired(timeout, pid=pid, name=proc_name) psutil.TimeoutExpired: psutil.TimeoutExpired timeout after 60 seconds
(pid=779)
One possibility may be the lack of an init process to reap zombies. You can fix this by running with docker run --init, or using e.g. tini. See https://hynek.me/articles/docker-signals/

Python exclude CWD file in proc

I have a script called client that does a few things around the OS and it's failing when it comes to /proc/1/cwd
I'm trying to ignore these types of files but the script is still crashing when it comes to this file. I get Memory Map output and also a Stack Trace before the script crashes.
I did a file /proc/1/cwd command on the file and I can see it's a symlink, where I then included an if os.path.islink(file) is False: IF statement to skip it and proceed.
However, I seem to still be hitting it.
The below is the Traceback after the crash.
Traceback (most recent call last):
File "client.py", line 235, in <module>
File "client.py", line 120, in runner
OSError: [Errno 13] Permission denied: '/proc/1/cwd'
Failed to execute script client
The error suggests the script client is working on /proc/1/cwd and then right after seems to say that it can't execute itself?
Any help would be appreciated.
-----Additional -------
Code lines from error:
like 235 is literally the line runner(path)
runner() represents a function that has a for loop that cycles through files in a folder to find the correct file. During this process, runner will do the following:
do line 120 mode = os.stat(fullfilename).st_mode
check if os.path.isdir(fullfilename) is False:
then if stat.S_ISREG(mode) is True:
then if os.path.islink(fullfilename) is False:
then if fullfilename not in blacklist:
Line 120 is mode = os.stat(fullfilename).st_mode

Stopping a python process so that context managers still call __exit__ (in windows)

How can I stop a python process in such a way that any active context managers will gracefully call their __exit__ function before closing?
I use context managers (__enter__() and __exit__()) to reliably and safely close connections to optical hardware. This has been working great, although we are now starting to execute routines that run for hours. Often we will realize shortly after starting one that we have a bug, and would rather to stop the process short.
I have been running code from PyCharm, which has allows you to "stop" a running process. This seems to instantly kill the process, whether I'm in debug or run. The __exit__ functions don't seem to get called.
Also, the computer that controls the hardware runs windows, if that somehow comes into play. ***
***Indeed in comes into play. Macosx seems to call the exit function while windows does not.
I decided to write a basic test:
from abc import *
import time
class Test(object):
__metaclass__ = ABCMeta
def __init__(self, *args, **kwargs):
print("Init called.")
def __enter__(self, *args, **kwargs):
print("enter called")
def __exit__(self, type, value, traceback):
print("Exit called")
with Test() as t:
time.sleep(100)
print("Should never get here.")
I run this code from PyCharm, and while it is in the sleep statement I press the stop button in pycharm. Here is the output from both:
Macosx:
Init called.
enter called
Exit called
Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1591, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1018, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Users/.../Library/Preferences/PyCharmCE2017.1/scratches/scratch_25.py", line 22, in <module>
time.sleep(100)
KeyboardInterrupt
Windows
Init called.
enter called
Process finished with exit code 1
I found a workaround on the PyCharm bug tracker:
https://youtrack.jetbrains.com/issue/PY-17252
In PyCharm go to help->Edit Custom Properties
Agree to create the idea.properties file (if it asks)
Add the following line: kill.windows.processes.softly=true
If you use Numpy or Scipy, you will also need to add the following environment variable:
os.environ['FOR_DISABLE_CONSOLE_CTRL_HANDLER'] = "1"
Restart Pycharm
Now when I run my test with this applied (on Windows!) I get the following output:
Init called.
enter called
Exit called
Traceback (most recent call last):
File "C:/Users/.../.PyCharmCE2017.1/config/scratches/scratch_3.py", line 20, in <module>
time.sleep(100)
KeyboardInterrupt
Process finished with exit code 1

convert a bash script to python3

I have a bash script, which is running perfectly:
gvim --servername "servername" $1
if [ -f ${1%.tex}.pdf ];
then
evince ${1%.tex}.pdf &
fi
evince_vim_dbus.py GVIM servername ${1%.tex}.pdf $1 &
I am trying to convert it to python as:
#!/usr/bin/env python3
from subprocess import call
import sys, os
inp_tex = sys.argv[1]
oup_pdf = os.path.splitext(sys.argv[1])[0]+".pdf"
print(oup_pdf)
call(["gvim", "--servername", "servername", sys.argv[1]])
if os.path.exists(oup_pdf):
call(["evince", oup_pdf])
call(["evince_vim_dbus.py", "GVIM", "servername", oup_pdf, inp_tex])
in the python, both gvim and evince window is open, but evince_vim_dbus.py line is not working. Not that it is giving any error, but it is not showing intended result, as it should, and is doing with the bash script.
trying with check_call (I have to kill it after a while, here's the traceback):
Traceback (most recent call last):
File "/home/rudra/vims.py", line 28, in <module>
check_call(["python","/home/rudra/bin/evince_vim_dbus.py", "GVIM", "servername", oup_pdf, inp_tex])
File "/usr/lib64/python3.5/subprocess.py", line 576, in check_call
retcode = call(*popenargs, **kwargs)
File "/usr/lib64/python3.5/subprocess.py", line 559, in call
return p.wait(timeout=timeout)
File "/usr/lib64/python3.5/subprocess.py", line 1658, in wait
(pid, sts) = self._try_wait(0)
File "/usr/lib64/python3.5/subprocess.py", line 1608, in _try_wait
(pid, sts) = os.waitpid(self.pid, wait_flags)
KeyboardInterrupt
I'm going to have a guess that your real problem isn't the evince_vim_dbus.py line itself, but rather the gvim line, because you pass it the server name 'servername' instead of simply servername, and so doesn't match the name on the line that runs evince_vim_dbus.py.
I'm not familiar with gvim or its server functionality, but I'm guessing the evince_vim_dbus.py program connects to gvim using the given name, in which case it's going to fail since the server of the right name isn't running.
If that's not it, then maybe the problem is that subprocess.call() runs the given program and waits for it to exit, whereas in your original bash script, you run evince with an ampersand, causing bash not to wait for it, so maybe the problem is that evince_vim_dbus.py never runs at all until you exit Evince.

Categories

Resources