Almenon / AREPL-vscode

Program python in real-time
MIT License
267 stars 33 forks source link

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

Open Almenon opened 6 years ago

Almenon commented 6 years ago
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 commented 6 years ago

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 commented 6 years ago

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

Almenon commented 2 weeks ago

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