Python 解释器段错误

Python interpreter segfault

我有一个奇怪的问题,当我 运行 python shell:

$ python3

Python 3.4.2 (default, Oct  8 2014, 10:45:20) 
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('Hello') # Error appears for any command in shell

[1]    9021 segmentation fault  python3

但是当我 运行 python3 hello.py 它起作用了。

我已经尝试了 python2.7、python3.4 和 python3.5,但都出现了同样的错误。

OS Debian 8.4

更新 1

journalctl 输出:

-- Logs begin at нд 2016-05-22 14:57:54 EEST, end at вт 2016-05-24 15:56:53 EEST. --
тра 24 15:56:53 desktop kernel: python[21150]: segfault at 0 ip           (null) sp 00007ffda62fe218 error 14 in python3[400000+3b2000]

更新 2

问题仍然没有解决,但是我发现如果我运行它作为另一个用户它可以正常工作。我已经尝试删除 .cache 和一些 python 相关目录 (.jupyter, .ipython),但没有结果。

更新 3

GDB 输出

gdb python
GNU gdb (Debian 7.7.1+dfsg-5) 7.7.1
Copyright (C) 2014 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 "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from python...(no debugging symbols found)...done.
(gdb) run
Starting program: /usr/bin/python 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Python 2.7.9 (default, Mar  1 2015, 12:57:24) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello")


Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) 
(gdb) backtrace
#0  0x0000000000000000 in ?? ()
#1  0x00007ffff694892e in rl_callback_read_char () from /lib/x86_64-linux-gnu/libreadline.so.6
#2  0x00007ffff6b67cd5 in ?? () from /usr/lib/python2.7/lib-dynload/readline.x86_64-linux-gnu.so
#3  0x0000000000449d88 in PyOS_Readline ()
#4  0x0000000000422d2a in ?? ()
#5  0x00000000004c4e8a in ?? ()
#6  0x00000000004c460e in PyParser_ASTFromFile ()
#7  0x0000000000449b11 in PyRun_InteractiveOneFlags ()
#8  0x0000000000449937 in PyRun_InteractiveLoopFlags ()
#9  0x000000000042d96b in ?? ()
#10 0x00000000004982f2 in Py_Main ()
#11 0x00007ffff6f12b45 in __libc_start_main (main=0x497d80 <main>, argc=1, argv=0x7fffffffe748, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffe738) at libc-start.c:287
#12 0x0000000000497ca0 in _start ()
(gdb) 

更新 4

以下操作的 Strace 调试:

python
Python 2.7.9 (default, Mar  1 2015, 12:57:24) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello")

[1]    17271 segmentation fault  python

和跟踪日志:

 strace -p 17271
Process 17271 attached
select(1, [0], NULL, NULL, NULL)        = 1 (in [0])
rt_sigaction(SIGWINCH, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, {SIG_DFL, [], 0}, 8) = 0
read(0, "p", 1)                         = 1
write(1, "p", 1)                        = 1
rt_sigaction(SIGWINCH, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, 8) = 0
select(1, [0], NULL, NULL, NULL)        = 1 (in [0])
rt_sigaction(SIGWINCH, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, 8) = 0
read(0, "r", 1)                         = 1
write(1, "r", 1)                        = 1
rt_sigaction(SIGWINCH, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, 8) = 0
select(1, [0], NULL, NULL, NULL)        = 1 (in [0])
rt_sigaction(SIGWINCH, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, 8) = 0
read(0, "i", 1)                         = 1
write(1, "i", 1)                        = 1
rt_sigaction(SIGWINCH, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, 8) = 0
select(1, [0], NULL, NULL, NULL)        = 1 (in [0])
rt_sigaction(SIGWINCH, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, 8) = 0
read(0, "n", 1)                         = 1
write(1, "n", 1)                        = 1
rt_sigaction(SIGWINCH, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, 8) = 0
select(1, [0], NULL, NULL, NULL)        = 1 (in [0])
rt_sigaction(SIGWINCH, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, 8) = 0
read(0, "t", 1)                         = 1
write(1, "t", 1)                        = 1
rt_sigaction(SIGWINCH, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, 8) = 0
select(1, [0], NULL, NULL, NULL)        = 1 (in [0])
rt_sigaction(SIGWINCH, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, 8) = 0
read(0, "(", 1)                         = 1
write(1, "(", 1)                        = 1
rt_sigaction(SIGWINCH, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, 8) = 0
select(1, [0], NULL, NULL, NULL)        = 1 (in [0])
rt_sigaction(SIGWINCH, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, 8) = 0
read(0, ")", 1)                         = 1
write(1, ")", 1)                        = 1
rt_sigaction(SIGWINCH, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, 8) = 0
select(1, [0], NULL, NULL, NULL)        = 1 (in [0])
rt_sigaction(SIGWINCH, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, 8) = 0
read(0, "", 1)                       = 1
rt_sigaction(SIGWINCH, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, 8) = 0
select(1, [0], NULL, NULL, NULL)        = 1 (in [0])
rt_sigaction(SIGWINCH, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, 8) = 0
read(0, "[", 1)                         = 1
rt_sigaction(SIGWINCH, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, 8) = 0
select(1, [0], NULL, NULL, NULL)        = 1 (in [0])
rt_sigaction(SIGWINCH, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, 8) = 0
read(0, "D", 1)                         = 1
write(1, "", 1)                      = 1
rt_sigaction(SIGWINCH, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, 8) = 0
select(1, [0], NULL, NULL, NULL)        = 1 (in [0])
rt_sigaction(SIGWINCH, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, 8) = 0
read(0, "\"", 1)                        = 1
write(1, "\")", 3)                   = 3
rt_sigaction(SIGWINCH, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, 8) = 0
select(1, [0], NULL, NULL, NULL)        = 1 (in [0])
rt_sigaction(SIGWINCH, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, 8) = 0
read(0, "\"", 1)                        = 1
write(1, "\")", 3)                   = 3
rt_sigaction(SIGWINCH, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, 8) = 0
select(1, [0], NULL, NULL, NULL)        = 1 (in [0])
rt_sigaction(SIGWINCH, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, 8) = 0
read(0, "", 1)                       = 1
rt_sigaction(SIGWINCH, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, 8) = 0
select(1, [0], NULL, NULL, NULL)        = 1 (in [0])
rt_sigaction(SIGWINCH, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, 8) = 0
read(0, "[", 1)                         = 1
rt_sigaction(SIGWINCH, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, 8) = 0
select(1, [0], NULL, NULL, NULL)        = 1 (in [0])
rt_sigaction(SIGWINCH, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, 8) = 0
read(0, "D", 1)                         = 1
write(1, "", 1)                      = 1
rt_sigaction(SIGWINCH, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, 8) = 0
select(1, [0], NULL, NULL, NULL)        = 1 (in [0])
rt_sigaction(SIGWINCH, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, 8) = 0
read(0, "H", 1)                         = 1
write(1, "H\")", 5)               = 5
rt_sigaction(SIGWINCH, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, 8) = 0
select(1, [0], NULL, NULL, NULL)        = 1 (in [0])
rt_sigaction(SIGWINCH, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, 8) = 0
read(0, "e", 1)                         = 1
write(1, "e\")", 5)               = 5
rt_sigaction(SIGWINCH, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, 8) = 0
select(1, [0], NULL, NULL, NULL)        = 1 (in [0])
rt_sigaction(SIGWINCH, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, 8) = 0
read(0, "l", 1)                         = 1
write(1, "l\")", 5)               = 5
rt_sigaction(SIGWINCH, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, 8) = 0
select(1, [0], NULL, NULL, NULL)        = 1 (in [0])
rt_sigaction(SIGWINCH, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, 8) = 0
read(0, "l", 1)                         = 1
write(1, "l\")", 5)               = 5
rt_sigaction(SIGWINCH, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, 8) = 0
select(1, [0], NULL, NULL, NULL)        = 1 (in [0])
rt_sigaction(SIGWINCH, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, 8) = 0
read(0, "o", 1)                         = 1
write(1, "o\")", 5)               = 5
rt_sigaction(SIGWINCH, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, 8) = 0
select(1, [0], NULL, NULL, NULL)        = 1 (in [0])
rt_sigaction(SIGWINCH, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, 8) = 0
read(0, "\r", 1)                        = 1
rt_sigaction(SIGWINCH, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, 8) = 0
select(1, [0], NULL, NULL, NULL)        = 1 (in [0])
rt_sigaction(SIGWINCH, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, 8) = 0
read(0, "\r", 1)                        = 1
write(1, "\n", 1)                       = 1
ioctl(0, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, {B38400 opost isig -icanon -echo ...}) = 0
ioctl(0, SNDCTL_TMR_STOP or SNDRV_TIMER_IOCTL_GINFO or TCSETSW, {B38400 opost isig icanon echo ...}) = 0
ioctl(0, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
rt_sigaction(SIGWINCH, {SIG_DFL, [], SA_RESTORER, 0x7fd9005a58d0}, {0x7fd8ff2f8270, [], SA_RESTORER|SA_RESTART, 0x7fd9005a58d0}, 8) = 0
select(1, [0], NULL, [0], {0, 500000})  = 0 (Timeout)
write(1, "\n", 1)                       = 1
--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=0} ---
+++ killed by SIGSEGV +++

更新 5

环境变量差异(适用于 root 用户):

更新 6

作为普通用户:

ldd /usr/bin/python
    linux-vdso.so.1 (0x00007ffcb1648000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f316da1d000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f316d815000)
    libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007f316d60d000)
    libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f316d3ed000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f316d0e5000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f316cd35000)
    /lib64/ld-linux-x86-64.so.2 (0x000055ca7cb3e000)

作为根用户:

ldd /usr/bin/python
    linux-vdso.so.1 (0x00007fffc8ef0000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f79bf425000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f79bf21d000)
    libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007f79bf015000)
    libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f79bedf5000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f79beaed000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f79be73d000)
    /lib64/ld-linux-x86-64.so.2 (0x00005600afb99000)

分段错误可能有很多原因。

例如:

  • 内存不足
  • Ram 内存故障
  • 正在获取大于交换内存的大量数据
  • 错误代码
  • 多重递归

因为你只是想打印一些东西,我会说这可能是原因 1 或 2。

因此,经过一些调试(虽然评论现在看起来更像是聊天,但它们可以用作调试会话的示例),发现段错误的发生是由于 readline 设置的一些问题,即 user-特定的 ~/.inputrc 配置文件(我会说它可能是一个 readline 错误,因为无论配置是什么,它都不应该出现段错误)。

这表明我们的软件是多么的复杂和脆弱,因为乍一看 python 可能会崩溃,因为库的配置文件损坏,甚至没有多少人记得 python 使用(如果他们记得它存在的话)。

我有同样的问题,我通过将 LC_CTYPE env 变量从 iso 更改为 utf8 来解决它。