AREPL-vscode icon indicating copy to clipboard operation
AREPL-vscode copied to clipboard

certain numpy code causes arepl to crash without giving any crash indicator

Open Almenon opened this issue 7 years ago • 3 comments

import numpy as np
arr = np.array([str(i) for i in range(3)], dtype=np.object)
dtype = arr.dtype
shape = arr.shape
buf = arr.tobytes()
del arr
arr = np.ndarray(buffer=buf, dtype=dtype, shape=shape).copy()

This code causes arepl to hang indefinitely without giving any visual indicator that it crashed.

Opening up the dev console I see the following error:

 ERR process exited with code 3221225477: Error: process exited with code 3221225477
	at terminateIfNeeded (C:\Users\Almenon\.vscode\extensions\almenon.arepl-1.0.2\node_modules\python-shell\index.js:124:27)
	at ChildProcess.<anonymous> (C:\Users\Almenon\.vscode\extensions\almenon.arepl-1.0.2\node_modules\python-shell\index.js:113:13)
	at emitTwo (events.js:126:13)
	at ChildProcess.emit (events.js:214:7)
	at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12)

Almenon avatar Oct 13 '18 18:10 Almenon

I wrap excec in a try/catch BaseException, which is like the king 👑 of all try/catches. It will catch every possible exception no matter what. So I was confused as to why the segmentation fault generated by the numpy code didn't get caught by my kingly catch.

But apparently a segmentation fault is a signal (SIGSEGV) and signals ARE NOT caught by normal try/catches, as they are not technically exceptions. (even though they will happily crash your program just the same 💀)

In order to catch a signal you have to register a signal handler like so:

import os
import signal

def sig_handler(signum, frame):
    print("segfault")
signal.signal(signal.SIGSEGV, sig_handler)

os.kill(os.getpid(), signal.SIGSEGV)

live demo: https://repl.it/@almenon/signal-handler-py3

This works great if you are on ubuntu .... not so great if you are on windows. In fact, this does not work at all on windows!

Almenon avatar Oct 13 '18 20:10 Almenon

upgrading jsonpickle should have solved the issue but the process still crashes :/ I guess I should raise a bug in jsonpickle repo

Almenon avatar Oct 16 '18 02:10 Almenon

This still causes AREPL to crash, but at least there's a indicator now:

Error in the AREPL extension!
err code: 3221225477

python 3.12, numpy==2.1.3. I had to change np.object to object to fix a deprecation exception,

The error happens on the second run.

This will be fixed by https://github.com/Almenon/AREPL-vscode/issues/439

Almenon avatar Nov 12 '24 02:11 Almenon