Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: help(numpy) causes segfault on exit

I came across a strange occurrence: in the python interpreter, I do the following:

>>>import numpy
>>>help(numpy)

The help displays correctly, but as soon as I press q to return to the interpreter:

Segmentation fault (core dumped)

I have no idea what causes the segfault and it still happens if I import numpy with a local name (the typical np). This does not happen with any other libraries. I tried uninstalling and reinstalling numpy but that made no difference. Is numpy too large to handle that way or is something wrong with my interpreter? How can I fix this?

I am running Ubuntu 13.04 32-bit and using Python 2.7.4.

UPDATE: PYTHONSTARTUP and PYTHONPATH are both empty. Writing a script which does this has exactly the same effect.

Also, numpy and other libraries work perfectly, it's just the help call that messes up when used on numpy.

UPDATE 2:

For test.py:

if __name__ == "__main__":
    import numpy
    help(numpy)

Here is the gdb output:

$ gdb --args python test.py
GNU gdb (GDB) 7.5.91.20130417-cvs-ubuntu
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /usr/bin/python2.7...(no debugging symbols found)...done.
(gdb) run
Starting program: /usr/bin/python2.7 test.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1".

Program received signal SIGPIPE, Broken pipe.
0xb7fdd424 in __kernel_vsyscall ()
(gdb) where
#0  0xb7fdd424 in __kernel_vsyscall ()
#1  0xb7eb2cf3 in write () from /lib/i386-linux-gnu/libc.so.6
#2  0xb7e447e1 in _IO_file_write () from /lib/i386-linux-gnu/libc.so.6
#3  0xb7e446c6 in ?? () from /lib/i386-linux-gnu/libc.so.6
#4  0xb7e45422 in _IO_file_xsputn () from /lib/i386-linux-gnu/libc.so.6
#5  0xb7e3a025 in fwrite () from /lib/i386-linux-gnu/libc.so.6
#6  0x080ac05b in ?? ()
#7  0x080b6bde in PyEval_EvalFrameEx ()
#8  0x080b71c0 in PyEval_EvalFrameEx ()
#9  0x0811f939 in PyEval_EvalCodeEx ()
#10 0x080b80d4 in PyEval_EvalFrameEx ()
#11 0x080b71c0 in PyEval_EvalFrameEx ()
#12 0x0811f939 in PyEval_EvalCodeEx ()
#13 0x080b7265 in PyEval_EvalFrameEx ()
#14 0x080b71c0 in PyEval_EvalFrameEx ()
#15 0x0811f939 in PyEval_EvalCodeEx ()
#16 0x081203ee in ?? ()
#17 0x080e11f9 in ?? ()
#18 0x080e3daf in PyObject_Call ()
#19 0x08129483 in ?? ()
#20 0x080b8a3e in PyEval_EvalFrameEx ()
#21 0x0811f939 in PyEval_EvalCodeEx ()
#22 0x0812029e in ?? ()
---Type <return> to continue, or q <return> to quit---
#23 0x080e11f9 in ?? ()
#24 0x080e3daf in PyObject_Call ()
#25 0x0816dd88 in ?? ()
#26 0x080b7615 in PyEval_EvalFrameEx ()
#27 0x0811f939 in PyEval_EvalCodeEx ()
#28 0x081836c7 in PyEval_EvalCode ()
#29 0x08183b7d in ?? ()
#30 0x080a6032 in PyRun_FileExFlags ()
#31 0x080a65f8 in PyRun_SimpleFileExFlags ()
#32 0x080a74af in Py_Main ()
#33 0x080a7581 in main ()
(gdb) 

Update 3:

The output of $strace -f python test.py is very long, here are the last few lines from where it clones a child process:

[pid  2610] clone(Process 2611 attached child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0xb7527968) = 2611
[pid  2610] wait4(-1, Process 2610 suspended
<unfinished ...>
[pid  2611] execve("/usr/bin/less", ["less"], [/* 53 vars */]) = 0
[pid  2611] brk(0)                      = 0x89ed000
[pid  2611] access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
[pid  2611] mmap2(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7734000
[pid  2611] access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
[pid  2611] open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
[pid  2611] fstat64(3, {st_mode=S_IFREG|0644, st_size=93821, ...}) = 0
[pid  2611] mmap2(NULL, 93821, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb771d000
[pid  2611] close(3)                    = 0
[pid  2611] access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
[pid  2611] open("/lib/i386-linux-gnu/libtinfo.so.5", O_RDONLY|O_CLOEXEC) = 3
[pid  2611] read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\320W\0\0004\0\0\0"..., 512) = 512
[pid  2611] fstat64(3, {st_mode=S_IFREG|0644, st_size=121032, ...}) = 0
[pid  2611] mmap2(NULL, 124748, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb76fe000
[pid  2611] mmap2(0xb771a000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1b) = 0xb771a000
[pid  2611] close(3)                    = 0
[pid  2611] access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
[pid  2611] open("/lib/i386-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
[pid  2611] read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\220\232\1\0004\0\0\0"..., 512) = 512
[pid  2611] fstat64(3, {st_mode=S_IFREG|0755, st_size=1770984, ...}) = 0
[pid  2611] mmap2(NULL, 1780508, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb754b000
[pid  2611] mmap2(0xb76f8000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1ad) = 0xb76f8000
[pid  2611] mmap2(0xb76fb000, 11036, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb76fb000
[pid  2611] close(3)                    = 0
[pid  2611] mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb754a000
[pid  2611] mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7549000
[pid  2611] set_thread_area({entry_number:-1 -> 6, base_addr:0xb75496c0, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}) = 0
[pid  2611] mprotect(0xb76f8000, 8192, PROT_READ) = 0
[pid  2611] mprotect(0xb771a000, 8192, PROT_READ) = 0
[pid  2611] mprotect(0x8069000, 4096, PROT_READ) = 0
[pid  2611] mprotect(0xb7757000, 4096, PROT_READ) = 0
[pid  2611] munmap(0xb771d000, 93821)   = 0
[pid  2611] ioctl(1, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
[pid  2611] ioctl(1, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
[pid  2611] brk(0)                      = 0x89ed000
[pid  2611] brk(0x8a0e000)              = 0x8a0e000
[pid  2611] stat64("/home/marzul/.terminfo", 0xbfac7780) = -1 ENOENT (No such file or directory)
[pid  2611] stat64("/etc/terminfo", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
[pid  2611] access("/etc/terminfo/x/xterm", R_OK) = -1 ENOENT (No such file or directory)
[pid  2611] stat64("/lib/terminfo", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
[pid  2611] access("/lib/terminfo/x/xterm", R_OK) = 0
[pid  2611] open("/lib/terminfo/x/xterm", O_RDONLY|O_LARGEFILE) = 3
[pid  2611] fstat64(3, {st_mode=S_IFREG|0644, st_size=3315, ...}) = 0
[pid  2611] mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7733000
[pid  2611] read(3, "\32\1)\0&\0\17\0\235\1Z\5xterm|xterm-debian|X"..., 4096) = 3315
[pid  2611] read(3, "", 4096)           = 0
[pid  2611] close(3)                    = 0
[pid  2611] munmap(0xb7733000, 4096)    = 0
[pid  2611] ioctl(1, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
[pid  2611] ioctl(1, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
[pid  2611] ioctl(1, TIOCGWINSZ, {ws_row=24, ws_col=80, ws_xpixel=0, ws_ypixel=0}) = 0
[pid  2611] ioctl(2, TIOCGWINSZ, {ws_row=24, ws_col=80, ws_xpixel=0, ws_ypixel=0}) = 0
[pid  2611] open("/usr/bin/.sysless", O_RDONLY|O_LARGEFILE) = -1 ENOENT (No such file or directory)
[pid  2611] open("/etc/sysless", O_RDONLY|O_LARGEFILE) = -1 ENOENT (No such file or directory)
[pid  2611] open("/home/marzul/.less", O_RDONLY|O_LARGEFILE) = -1 ENOENT (No such file or directory)
[pid  2611] open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3
[pid  2611] fstat64(3, {st_mode=S_IFREG|0644, st_size=8748544, ...}) = 0
[pid  2611] mmap2(NULL, 2097152, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7349000
[pid  2611] mmap2(NULL, 4096, PROT_READ, MAP_PRIVATE, 3, 0x5e1) = 0xb7733000
[pid  2611] close(3)                    = 0
[pid  2611] open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3
[pid  2611] fstat64(3, {st_mode=S_IFREG|0644, st_size=8748544, ...}) = 0
[pid  2611] mmap2(NULL, 4096, PROT_READ, MAP_PRIVATE, 3, 0x5d8) = 0xb7732000
[pid  2611] close(3)                    = 0
[pid  2611] open("/home/marzul/.lesshst", O_RDONLY|O_LARGEFILE) = 3
[pid  2611] fstat64(3, {st_mode=S_IFREG|0600, st_size=129, ...}) = 0
[pid  2611] mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) =     0xb7731000
[pid  2611] read(3, ".less-history-file:\n.search\n\"pus"..., 4096) = 129
[pid  2611] read(3, "", 4096)           = 0
[pid  2611] close(3)                    = 0
[pid  2611] munmap(0xb7731000, 4096)    = 0
[pid  2611] open("/dev/tty", O_RDONLY|O_LARGEFILE) = 3
[pid  2611] ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
[pid  2611] fsync(3)                    = -1 EINVAL (Invalid argument)
[pid  2611] ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
[pid  2611] ioctl(3, SNDCTL_TMR_STOP or TCSETSW, {B38400 opost isig -icanon -echo ...}) = 0
[pid  2611] ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig -icanon -echo ...}) = 0
[pid  2611] rt_sigaction(SIGINT, {0x805cc00, [INT], SA_RESTART}, {SIG_DFL, [], 0}, 8) = 0
[pid  2611] rt_sigaction(SIGTSTP, {0x805cb80, [TSTP], SA_RESTART}, {SIG_DFL, [], 0}, 8) = 0
[pid  2611] rt_sigaction(SIGWINCH, {0x805cbc0, [WINCH], SA_RESTART}, {SIG_DFL, [], 0}, 8) = 0
[pid  2611] rt_sigaction(SIGQUIT, {SIG_IGN, [QUIT], SA_RESTART}, {SIG_DFL, [], 0}, 8) = 0
[pid  2611] ioctl(0, SNDCTL_TMR_TIMEBASE or TCGETS, 0xbfac9088) = -1 ENOTTY (Inappropriate ioctl for device)
[pid  2611] stat64("-", 0xbfac90a0)     = -1 ENOENT (No such file or directory)
[pid  2611] write(1, "\33[?1049h\33[?1h\33=\r", 16) = 19
[pid  2611] fsync(3)                    = -1 EINVAL (Invalid argument)
[pid  2611] ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig -icanon -echo ...}) = 0
[pid  2611] ioctl(3, SNDCTL_TMR_STOP or TCSETSW, {B38400 opost isig icanon echo ...}) = 0
[pid  2611] ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
[pid  2611] exit_group(0)               = ?
Process 2610 resumed
Process 2611 detached
[pid  2610] <... wait4 resumed> [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 2611
[pid  2610] --- SIGCHLD (Child exited) @ 0 (0) ---
[pid  2610] sigreturn()                 = ? (mask now [])
[pid  2610] exit_group(0)               = ?
Process 2610 detached
<... write resumed> )                   = 73728
--- SIGPIPE (Broken pipe) @ 0 (0) ---
--- SIGCHLD (Child exited) @ 0 (0) ---
write(4, "l the attributes of the ndarray "..., 3043328) = -1 EPIPE (Broken pipe)
--- SIGPIPE (Broken pipe) @ 0 (0) ---
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
+++ killed by SIGSEGV (core dumped) +++
Segmentation fault (core dumped)

Update 4:

$echo $LESS

$cat cat ~/.less
cat: /home/user/.less: No such file or directory

Apparently less isn't configured at all.

Searching what didn't get cut off of strace in the terminal for "= 4" gave the following segments:

read(3, "---------\n    a, b : ndarray\n\n  "..., 4096) = 4096
read(3, "epstr = \", \"\n    arglist = argum"..., 4096) = 4096
read(3, "* Total of %d references found. "..., 4096) = 4096
read(3, "`output` not supplied then sourc"..., 4096) = 4096
read(3, "      r += sum([200 for w in wha"..., 4096) = 4096
read(3, "                  except NameErr"..., 4096) = 4096
read(3, "          return tuple([self.vis"..., 4096) = 4018

and

[pid  3294] write(2, "Missing filename (\"less --help\" "..., 42) = 42

Update 5:

$strace -f python test.py 2>&1 | tee -a out.txt ; (exit ${PIPESTATUS})
$cat out.txt | grep "open(.*) = 4"

open("/usr/lib/python2.7/site.pyc", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/os.py", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/traceback.py", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/sysconfig.py", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/re.py", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/_sysconfigdata.py", O_RDONLY|O_LARGEFILE) = 4
open("/usr/local/lib/python2.7/dist-packages/easy-install.pth", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/dist-packages/PILcompat.pth", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/dist-packages/gtk-2.0-pysupport-compat.pth", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/dist-packages/pygtk.pth", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/dist-packages/python-support.pth", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/dist-packages/setuptools.pth", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/dist-packages/ubuntu-sso-client.pth", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/dist-packages/ubuntuone-client.pth", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/dist-packages/ubuntuone-control-panel.pth", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol.pth", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/dist-packages/wx.pth", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/dist-packages/zope.interface-4.0.5-nspkg.pth", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/sitecustomize.py", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/encodings/__init__.pyc", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/codecs.py", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/encodings/aliases.py", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/encodings/utf_8.pyc", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/dist-packages/numpy/__init__.pyc", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/dist-packages/numpy/__config__.py", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/dist-packages/numpy/version.py", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/dist-packages/numpy/_import_tools.py", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/dist-packages/numpy/fft/__init__.py", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/dist-packages/numpy/polynomial/__init__.py", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/dist-packages/numpy/random/__init__.py", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/dist-packages/numpy/ctypeslib.py", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/dist-packages/numpy/ma/__init__.py", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/inspect.py", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/pkgutil.py", O_RDONLY|O_LARGEFILE) = 4
open("/usr/lib/python2.7/repr.py", O_RDONLY|O_LARGEFILE) = 4

Update 6:

Without open(...) I also found:

ERR_PRINT = 4
FPE_UNDERFLOW = 4
like image 598
CrystalDuck Avatar asked Jun 27 '13 13:06

CrystalDuck


People also ask

What causes a segmentation fault in Python?

Overview. A segmentation fault (aka segfault) is a common condition that causes programs to crash; they are often associated with a file named core . Segfaults are caused by a program trying to read or write an illegal memory location.

What is segmentation fault core dumped?

Core Dump/Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you.” When a piece of code tries to do read and write operation in a read only location in memory or freed block of memory, it is known as core dump. It is an error indicating memory corruption.


1 Answers

From the Python side, I managed to track it down to this function in pydoc.py...

def pipepager(text, cmd):
    """Page through text by feeding it to another program."""
    pipe = os.popen(cmd, 'w')
    try:
        pipe.write(text)
        pipe.close()
    except IOError:
        pass # Ignore broken pipes caused by quitting the pager program.

...specifically the line pipe.write(text).

The problem goes away if you change that function to...

def pipepager(text, cmd):
    """Page through text by feeding it to another program."""
    import subprocess
    pipe = subprocess.Popen(cmd, stdin=subprocess.PIPE, shell=True).stdin
    try:
        pipe.write(text)
        pipe.close()
    except IOError:
        pass # Ignore broken pipes caused by quitting the pager program.

...which might be a reasonable workaround, although I'm still investigating why that fixes the problem - there's very little difference between the strace(1) output for the two cases.


Update

Leeward has filed a bug report for this.

like image 64
Aya Avatar answered Oct 20 '22 16:10

Aya