How to handle a segmentation fault occurred in python - python-3.x

I have written a piece of code that checks in a system if there is a new entry in the database or not if a new entry is found it will fetch the data and client will try to send it to the server ... but if data is continuously found in every execution of threading.timer cycle it will try to send it to the server...
Now issue is if the server is unreachable then it will print that server is not alive etc. if this cycle goes for few minutes the script just crash and it shows Segmentation Fault ... i want to handle this exception and wanted to process something if Segmentation fault occurs
working environment:
linux
python3
sql-server
EDIT
This is what shows after script gets crashed...
Fatal Python error: Segmentation fault
Thread 0xb3dff460 (most recent call first):
File "/usr/lib/python3.4/threading.py", line 294 in wait File "/usr/lib/python3.4/threading.py", line 553 in wait File "/usr/lib/python3.4/threading.py", line 1184 in run File "/usr/lib/python3.4/threading.py", line 920 in _bootstrap_inner File "/usr/lib/python3.4/threading.py", line 888 in _bootstrap
Current thread 0xb33ff460 (most recent call first):
File "abc.py", line 343 in send_to_server
File "abc.py", line 244 in sql_connect1
File "/usr/lib/python3.4/threading.py", line 1186 in run
File "/usr/lib/python3.4/threading.py", line 920 in _bootstrap_inner
File "/usr/lib/python3.4/threading.py", line 888 in _bootstrap
Thread 0xb45ff460 (most recent call first):
File "abc.py", line 408 in send_to_server
File "abc.py", line 244 in sql_connect1
File "/usr/lib/python3.4/threading.py", line 1186 in run
File "/usr/lib/python3.4/threading.py", line 920 in _bootstrap_inner
File "/usr/lib/python3.4/threading.py", line 888 in _bootstrap
Thread 0xb4fff460 (most recent call first):
File "abc.py", line 343 in send_to_server
File "abc.py", line 244 in sql_connect1
File "/usr/lib/python3.4/threading.py", line 1186 in run
File "/usr/lib/python3.4/threading.py", line 920 in _bootstrap_inner
File "/usr/lib/python3.4/threading.py", line 888 in _bootstrap
Thread 0xb6f39300 (most recent call first):
File "/usr/lib/python3.4/threading.py", line 1076 in _wait_for_tstate_lock
File "/usr/lib/python3.4/threading.py", line 1060 in join
File "/usr/lib/python3.4/threading.py", line 1294 in _shutdown
Segmentation fault

well i didn't know a segmentation fault was even possible, a segmentation fault mean that somwhere you go too far in the memory to a place that is not mapped.
you should note the line where it is said the problem originally happened and check the lines.

It's hard to say, but :
you are using python3.4, but we don't know the exact version, so maybe upgrading to the last version of 3.4 (or better, upgrading to 3.6 or 3.7) could help.
we also don't know what version of pyodbc you are using. Maybe this bug has been resolved. But there are several seg fault reported in github
we don't see you're code. Maybe you make use of the driver incorrectly (can't see why but...)

Related

python, websocket exception with run_forever()

I have a piece of code that uses, websocket and run_forever()
the program runs fine and suddenly just crashes with the follwoing output.
I have no idea how to trace this error and it just keeps happening at will :)
Requesting any help on how to debug this.
Exception in thread WebSocketClient:
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\threading.py", line 1016, in _bootstrap_inner
self.run()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\threading.py", line 953, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\cgs\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ws4py\websocket.py", line 531, in run
self.terminate()
File "C:\Users\cgs\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ws4py\websocket.py", line 431, in terminate
self.closed(1006, "Going away")
TypeError: WsClient.closed() takes 1 positional argument but 3 were given

Pytorch 1.8 hangs by chance when calling loss.backward()

When I was training an LSTM on pytorch the training process hangs by chance and cannot be terminated by Crlt+C. Then I used faulthandler to locate the problem. The training parameters, environment and faulthandler traceback output are listed below. It seems to be some problem with the C++ backend or the CUDA or even my graphic card I do not know.
Trace output batch_size = 64/32/16, num_workers = 2, CUDA 11.1, pytorch 1.8.0, cuDNN 8.0.5/8.0.1:
Thread 0x000036e8 (most recent call first): File
"C:\Users\myUserName\anaconda3\lib\threading.py", line 302 in wait
File "C:\Users\myUserName\anaconda3\lib\multiprocessing\queues.py",
line 227 in _feed File
"C:\Users\myUserName\anaconda3\lib\threading.py", line 870 in run
File "C:\Users\myUserName\anaconda3\lib\threading.py", line 932 in
_bootstrap_inner File "C:\Users\myUserName\anaconda3\lib\threading.py", line 890 in
_bootstrap
Thread 0x00004644 (most recent call first): File
"C:\Users\myUserName\anaconda3\lib\threading.py", line 302 in wait
File "C:\Users\myUserName\anaconda3\lib\multiprocessing\queues.py",
line 227 in _feed File
"C:\Users\myUserName\anaconda3\lib\threading.py", line 870 in run
File "C:\Users\myUserName\anaconda3\lib\threading.py", line 932 in
_bootstrap_inner File "C:\Users\myUserName\anaconda3\lib\threading.py", line 890 in
_bootstrap
Thread 0x00000efc (most recent call first):
Thread 0x00000138 (most recent call first): File
"C:\Users\myUserName\anaconda3\lib\threading.py", line 306 in wait
File "C:\Users\myUserName\anaconda3\lib\threading.py", line 558 in
wait File
"C:\Users\myUserName\anaconda3\lib\site-packages\tqdm_monitor.py",
line 59 in run File
"C:\Users\myUserName\anaconda3\lib\threading.py", line 932 in
_bootstrap_inner File "C:\Users\myUserName\anaconda3\lib\threading.py", line 890 in
_bootstrap
Thread 0x00001644 (most recent call first): File
"C:\Users\myUserName\anaconda3\lib\threading.py", line 306 in wait
File "C:\Users\myUserName\anaconda3\lib\queue.py", line 179 in get
File
"C:\Users\myUserName\anaconda3\lib\site-packages\tensorboard\summary\writer\event_file_writer.py",
line 232 in run File
"C:\Users\myUserName\anaconda3\lib\threading.py", line 932 in
_bootstrap_inner File "C:\Users\myUserName\anaconda3\lib\threading.py", line 890 in
_bootstrap
Thread 0x0000443c (most recent call first): File
"C:\Users\myUserName\anaconda3\lib\site-packages\torch\autograd_init_.py",
line 145 in backward File
"C:\Users\myUserName\anaconda3\lib\site-packages\torch\tensor.py",
line 245 in backward File "train.py", line 129 in main File
"train.py", line 246 in
batch_size = 64, num_workers = 0, CUDA 11.1, pytorch 1.8.0, cuDNN
8.0.5 Thread 0x00003650 (most recent call first):
Thread 0x000043b4 (most recent call first): File
"C:\Users\myUserName\anaconda3\lib\threading.py", line 306 in wait File
"C:\Users\myUserName\anaconda3\lib\threading.py", line 558 in wait File
"C:\Users\myUserName\anaconda3\lib\site-packages\tqdm_monitor.py", line 59
in run File "C:\Users\myUserName\anaconda3\lib\threading.py", line 932 in
_bootstrap_inner File "C:\Users\myUserName\anaconda3\lib\threading.py", line 890 in _bootstrap
Thread 0x000017c4 (most recent call first): File
"C:\Users\myUserName\anaconda3\lib\threading.py", line 306 in wait File
"C:\Users\myUserName\anaconda3\lib\queue.py", line 179 in get File
"C:\Users\myUserName\anaconda3\lib\site-packages\tensorboard\summary\writer\event_file_writer.py",
line 232 in run File "C:\Users\myUserName\anaconda3\lib\threading.py",
line 932 in _bootstrap_inner File
"C:\Users\myUserName\anaconda3\lib\threading.py", line 890 in _bootstrap
Thread 0x00001458 (most recent call first): File
"C:\Users\myUserName\anaconda3\lib\site-packages\torch\autograd_init_.py",
line 145 in backward File
"C:\Users\myUserName\anaconda3\lib\site-packages\torch\tensor.py", line 245
in backward File "train.py", line 129 in main File "train.py",
line 246 in
When num_workers=0 the output is the same except for lacking two threads below that I think belong to the dataloader.
Thread 0x000036e8 (most recent call first): File
"C:\Users\myUserName\anaconda3\lib\threading.py", line 302 in wait
File "C:\Users\myUserName\anaconda3\lib\multiprocessing\queues.py",
line 227 in _feed File
"C:\Users\myUserName\anaconda3\lib\threading.py", line 870 in run File
"C:\Users\myUserName\anaconda3\lib\threading.py", line 932 in
_bootstrap_inner File "C:\Users\myUserName\anaconda3\lib\threading.py", line 890 in
_bootstrap
Thread 0x00004644 (most recent call first): File
"C:\Users\myUserName\anaconda3\lib\threading.py", line 302 in wait
File "C:\Users\myUserName\anaconda3\lib\multiprocessing\queues.py",
line 227 in _feed File
"C:\Users\myUserName\anaconda3\lib\threading.py", line 870 in run File
"C:\Users\myUserName\anaconda3\lib\threading.py", line 932 in
_bootstrap_inner File "C:\Users\myUserName\anaconda3\lib\threading.py", line 890 in
_bootstrap
The resource usage is also moderate, with around 20% CPU usage, 16/32GB memory, and 3.8/8GB graphics memory usage. The GPU usage is low when training RNNs. The script was run on Windows 10. The graphic card I use is RTX 3070. The driver version for my graphics card is 461.09.
More Information
When I started debugging code I was using the unmatching versions of CUDA 11.2 with pytorch 1.7.1 and cudnn 8.1.0. At that time I came into CUDA exceptions from time to time, with outputs like kernel launch failed or failed to synchronization, and things just hang without error after I changed my CUDA version.

Key board module not working (python3, MacOSX, pycharmCE)

I am currently working on a program that will react to the pressing of a key. I am currently using the module "keyboard".
`import keyboard
rk = keyboard.record(until='Esc')
keyboard.play(rk, speed_factor=1)
`
But when I run it, it gives me:
'Exception in thread Thread-1:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/Users/user/PycharmProject/AUTOCLICKER/venv/lib/python3.8/site-packages/keyboard/init.py", line 294, in listen
_os_keyboard.listen(self.direct_callback)
File "/Users/user/PycharmProject/AUTOCLICKER/venv/lib/python3.8/site-packages/keyboard/_darwinkeyboard.py", line 430, in listen
raise OSError("Error 13 - Must be run as administrator")
OSError: Error 13 - Must be run as administrator'
What could I do to fix this? Is there any easier module to work with?
thanks Triobro3
Run your script with administrator privileges with "sudo" command and see if it fixes your problem.

Having trouble catching an exception in python 3

Working with Python 3.7.3, still figuring out how exception handling works.
I'm writing an xmpp bot, using slixmpp. I'm trying to make it so that if it loses connection to the server, it will try to reconnect. There doesn't seem to be any way to do this built in to slixmpp, so I'm write something into my own code to do it.
I've imported slixmpp as xmpp, and using it's send_raw() method to test that we're still connected to the server.
while True:
time.sleep(5) # Send every 5 seconds just for testing purposes
xmpp.send_raw('aroo?')
When I sever the connection to the server, this is what it spits out:
Traceback (most recent call last):
File "C:\Program Files\Python37\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "testcom.py", line 19, in run
eval(self.thing)()
File "testcom.py", line 28, in check_conn
xmpp.send_raw('aroo?')
File "C:\Program Files\Python37\lib\site-packages\slixmpp\xmlstream\xmlstream.py", line 926, in send_raw
raise NotConnectedError
slixmpp.xmlstream.xmlstream.NotConnectedError
I'm assuming that "NotConnectedError" is the exception that I need to catch, so I put the code inside a try block, like so:
try:
while True:
time.sleep(5) # Send every 5 seconds just for testing purposes
xmpp.send_raw('aroo?')
except NotConnectedError:
# Do a thing
pass
And this is what I get:
Traceback (most recent call last):
File "testcom.py", line 28, in check_conn
xmpp.send_raw('aroo?')
File "C:\Program Files\Python37\lib\site-packages\slixmpp\xmlstream\xmlstream.py", line 926, in send_raw
raise NotConnectedError()
slixmpp.xmlstream.xmlstream.NotConnectedError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files\Python37\lib\threading.py", line 917, in _bootstrap_inner
self.run()
"testcom.py", line 19, in run
eval(self.thing)()
File "testcom.py", line 29, in check_conn
except NotConnectedError:
NameError: name 'NotConnectedError' is not defined
Can anyone tell me what I'm doing wrong here?
Thanks!
I can't see your imports but make sure you have from slixmpp.xmlstream.xmlstream import NotConnectedError otherwise it doesn't have a definition for NotConnectedError within the application. You could also change NotConnectedError to xmpp.xmlstream.xmlstream.NotConnectedError if you don't want to have it imported as well.

Silencing broken pipe errors (`[Errno 32] Broken pipe`) in python with WSGI+Pyramid

I have a rather simple, naive Python/WSGI/Pyramid web-server.
It's run using wsgiref.simple_server.make_server(), on a server built using pyramid.config.Configurator().make_wsgi_app(). This server works fine.
However, the application it's serving has a lot of javascript image mouseover popups. If you run the mouse across the page, it can generate 20+ image requests. This is fine as well (It's an internal thing, not a lot of users).
However, doing so causes the server to emit something like half a dozen error tracebacks:
10.1.1.4 - - [25/Apr/2014 01:56:42] "GET /*SNIP* 500 59
----------------------------------------
Exception happened during processing of request from ('10.1.1.4', 18338)
Traceback (most recent call last):
File "/usr/lib/python3.4/wsgiref/handlers.py", line 138, in run
self.finish_response()
File "/usr/lib/python3.4/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/usr/lib/python3.4/wsgiref/handlers.py", line 274, in write
self.send_headers()
File "/usr/lib/python3.4/wsgiref/handlers.py", line 333, in send_headers
self._write(bytes(self.headers))
File "/usr/lib/python3.4/wsgiref/handlers.py", line 453, in _write
self.stdout.write(data)
File "/usr/lib/python3.4/socket.py", line 391, in write
return self._sock.send(b)
BrokenPipeError: [Errno 32] Broken pipe
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.4/wsgiref/handlers.py", line 141, in run
self.handle_error()
File "/usr/lib/python3.4/wsgiref/handlers.py", line 368, in handle_error
self.finish_response()
File "/usr/lib/python3.4/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/usr/lib/python3.4/wsgiref/handlers.py", line 274, in write
self.send_headers()
File "/usr/lib/python3.4/wsgiref/handlers.py", line 331, in send_headers
if not self.origin_server or self.client_is_modern():
File "/usr/lib/python3.4/wsgiref/handlers.py", line 344, in client_is_modern
return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.4/socketserver.py", line 306, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python3.4/socketserver.py", line 332, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python3.4/socketserver.py", line 345, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python3.4/socketserver.py", line 666, in __init__
self.handle()
File "/usr/lib/python3.4/wsgiref/simple_server.py", line 126, in handle
handler.run(self.server.get_app())
File "/usr/lib/python3.4/wsgiref/handlers.py", line 144, in run
self.close()
File "/usr/lib/python3.4/wsgiref/simple_server.py", line 35, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
I understand why I'm getting broken pipe errors (the request for the image is canceled before the image has fully transfered, because the mouseover popup has closed), and it seems harmless.
However, I have no idea how to silence this traceback. There are thousands of them in my logs, and it makes debugging actual errors a nightmare. I don't care that I'm getting broken pipe errors, how can I catch them and swallow them silently?
It seems like wsgiref.simple_server.make_server() installs an internal handler that catches BrokenPipeError: [Errno 32] Broken pipe, prints the traceback, and then swallows the error. I've tried wrapping the run_server() call in a try-except clause, and it doesn't have any effect.
I wound up just switching to using the CherryPy WSGI Server. It doesn't suffer from the broken pipe log issues, and is probably far more robust as well.
It also uses a threadpool, so it's more performant as well (multiple requests aren't blocking!).
I didn't find a straightforward way for achieving this, however, you can always do some monkey patching:
from wsgiref.handlers import BaseHandler
import sys
def ignore_broken_pipes(self):
if sys.exc_info()[0] != BrokenPipeError: BaseHandler.__handle_error_original_(self)
BaseHandler.__handle_error_original_ = BaseHandler.handle_error
BaseHandler.handle_error = ignore_broken_pipes
You will not see these annoyances after running this code somewhere in the beginning anymore.
For me, it looks like a bug somewhere in wsgiref implementation of BaseHandler:
def handle_error(self):
"""Log current error, and send error output to client if possible"""
self.log_exception(sys.exc_info())
if not self.headers_sent:
self.result = self.error_output(self.environ, self.start_response)
self.finish_response()
# XXX else: attempt advanced recovery techniques for HTML or text?
if BrokenPipeError is handled by this method, finish_response crashes. Why do we ever want to finish response if pipe is broken? Where the data is sent to?

Resources