Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python segmentation fault when closing / quitting

Tags:

python

I have a strange error that is now duplicated on 2 machines. It may be limited to python 2.7 64 bit, but that's not clear yet. On Mac OS X 10.7, I get: Segmentation fault: 11 and on 10.6 I get a simple "segmentation fault" whenever I close python.

However, the error is hard to reproduce. Simple python scripts like: python -c "import sys; print sys.maxint" open and close without issue.

I had suspected an issue with matplotlib, but similarly python -c "import pylab; pylab.plot(); pylab.show()" doesn't suffer any issues either.

However, there are a number of different packages that DO cause segmentation faults when python closes, but not during an interactive session and not until the script is finished. So, the error doesn't affect any of my code - but it makes for a mess when running anything in 10.7 because focus-stealing error messages pop up.

I'll make an effort to list and document the scripts / modules that lead to the crashes, but I thought I'd throw up the question now to see if anyone else has come across a similar issue.

Here's a sample trackback (thanks for the suggestion Ned).

Process:         python2.7 [42731]
Path:            /usr/local/bin/python
Identifier:      python2.7
Version:         ??? (???)
Code Type:       X86-64 (Native)
Parent Process:  bash [1838]

Date/Time:       2011-09-16 20:38:52.847 -0600
OS Version:      Mac OS X 10.7.1 (11B2118)
Report Version:  9

Interval Since Last Report:          11388 sec
Crashes Since Last Report:           12
Per-App Crashes Since Last Report:   11
Anonymous UUID:                      CE462F4B-5DAF-482A-8901-28ADB7AB0AD6

Crashed Thread:  0  Dispatch queue: com.apple.main-thread

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000008

VM Regions Near 0x8:
--> 
    __TEXT                 0000000100000000-0000000100001000 [    4K] r-x/rwx SM=COW  /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7

Application Specific Information:
objc[42731]: garbage collection is OFF

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   org.python.python               0x00000001000c3f71 new_threadstate + 305
1   org.python.python               0x00000001000c3fe8 PyGILState_Ensure + 40
2   h5e.so                          0x0000000106558c21 __pyx_f_4h5py_3h5e_err_callback + 33 (h5e.c:2045)
3   libhdf5.7.dylib                 0x0000000106323940 H5E_dump_api_stack + 144
4   libhdf5.7.dylib                 0x0000000106320428 H5Eget_auto2 + 136
5   libhdf5.7.dylib                 0x00000001062bf2f0 H5_term_library + 64
6   libsystem_c.dylib               0x00007fff8ff1b7d0 __cxa_finalize + 282
7   libsystem_c.dylib               0x00007fff8ff1b652 exit + 18
8   python                          0x0000000100000e51 _start + 255
9   python                          0x0000000100000d51 start + 33

Thread 1:: Dispatch queue: com.apple.libdispatch-manager
0   libsystem_kernel.dylib          0x00007fff8bcfd7e6 kevent + 10
1   libdispatch.dylib               0x00007fff8bdf760e _dispatch_mgr_invoke + 923
2   libdispatch.dylib               0x00007fff8bdf619e _dispatch_mgr_thread + 54

Thread 2:
0   libsystem_kernel.dylib          0x00007fff8bcfd192 __workq_kernreturn + 10
1   libsystem_c.dylib               0x00007fff8ff2a594 _pthread_wqthread + 758
2   libsystem_c.dylib               0x00007fff8ff2bb85 start_wqthread + 13

Thread 3:
0   libsystem_kernel.dylib          0x00007fff8bcfd192 __workq_kernreturn + 10
1   libsystem_c.dylib               0x00007fff8ff2a594 _pthread_wqthread + 758
2   libsystem_c.dylib               0x00007fff8ff2bb85 start_wqthread + 13

Thread 4:
0   libsystem_kernel.dylib          0x00007fff8bcfd192 __workq_kernreturn + 10
1   libsystem_c.dylib               0x00007fff8ff2a594 _pthread_wqthread + 758
2   libsystem_c.dylib               0x00007fff8ff2bb85 start_wqthread + 13

Thread 5:
0   libsystem_kernel.dylib          0x00007fff8bcfcdf2 __select + 10
1   Tcl                             0x00000001022eb80b NotifierThreadProc + 558
2   libsystem_c.dylib               0x00007fff8ff288bf _pthread_start + 335
3   libsystem_c.dylib               0x00007fff8ff2bb75 thread_start + 13

Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x0000000000000001  rbx: 0x0000000000000001  rcx: 0x0000000000000b00  rdx: 0x00007fff5fbff034
  rdi: 0x00000a0000000b03  rsi: 0x00000b0000000b00  rbp: 0x00007fff5fbff0c0  rsp: 0x00007fff5fbff0a0
   r8: 0x00007fff5fbff03c   r9: 0x00007fff5fbff038  r10: 0x0000000000000081  r11: 0x00000000e33a913f
  r12: 0x00000001062bf2b0  r13: 0x0000000000000000  r14: 0x0000000000000000  r15: 0x0000000101bd05a0
  rip: 0x00000001000c3f71  rfl: 0x0000000000010202  cr2: 0x0000000000000008
Logical CPU: 0

Binary Images:
       0x100000000 -        0x100000fff +python (??? - ???) <BA9D2124-1753-3A21-91BE-15C8CD5FE7F3> /usr/local/bin/python
       0x100003000 -        0x100142fe7 +org.python.python (, [c] 2004-2011 Python Software Foundation. - ???) <C6142A6D-0D50-32D9-A058-2A2BA484D94E> /Library/Frameworks/Python.framework/Versions/2.7/Python
        --SNIP - TOO LONG--
    0x7fff98338000 -     0x7fff9841cdef  libobjc.A.dylib (228.0.0 - compatibility 1.0.0) <C5F2392D-B481-3A9D-91BE-3D039FFF4DEC> /usr/lib/libobjc.A.dylib

External Modification Summary:
  Calls made by other processes targeting this process:
    task_for_pid: 6
    thread_create: 0
    thread_set_state: 0
  Calls made by this process:
    task_for_pid: 0
    thread_create: 0
    thread_set_state: 0
  Calls made by all processes on this machine:
    task_for_pid: 1288178
    thread_create: 1
    thread_set_state: 0

VM Region Summary:
ReadOnly portion of Libraries: Total=170.9M resident=77.2M(45%) swapped_out_or_unallocated=93.7M(55%)
Writable regions: Total=169.1M written=87.2M(52%) resident=111.0M(66%) swapped_out=1536K(1%) unallocated=58.2M(34%)

REGION TYPE                      VIRTUAL
===========                      =======
CG image                              4K
CG raster data                       64K
CG shared images                   3416K
CoreGraphics                         16K
CoreServices                       1800K
MALLOC                            140.4M
MALLOC guard page                    48K
Memory tag=242                       12K
Memory tag=251                        8K
STACK GUARD                        56.0M
Stack                              10.1M
VM_ALLOCATE                        16.1M
__CI_BITMAP                          80K
__DATA                             15.6M
__IMAGE                            1256K
__LINKEDIT                         53.4M
__TEXT                            117.5M
__UNICODE                           544K
mapped file                        30.4M
shared memory                       308K
===========                      =======
TOTAL                             446.8M

C code that produces the same (approximately) error: helloworld.cpp -

#include<iostream>
using namespace std;

int main() {
    char *blah;
    cout << "Hello World" << endl << "I am the C sea" << endl;
    cin >> blah;
    cout << blah << endl;
    return 0;
}

compiled by:

g++ helloworld.cpp -o helloworld.out
./helloworld.out    

Error Report #2:

Process:         helloworld.out [10982]
Path:            /Users/USER/*/helloworld.out
Identifier:      helloworld.out
Version:         ??? (???)
Code Type:       X86-64 (Native)
Parent Process:  bash [10803]

Date/Time:       2011-09-24 09:36:43.979 -0600
OS Version:      Mac OS X 10.7.1 (11B2118)
Report Version:  9

Interval Since Last Report:          271170 sec
Crashes Since Last Report:           119
Per-App Crashes Since Last Report:   4
Anonymous UUID:                      CE462F4B-5DAF-482A-8901-28ADB7AB0AD6

Crashed Thread:  0  Dispatch queue: com.apple.main-thread

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: 0x000000000000000d, 0x0000000000000000

VM Regions Near 0:
--> 
    __TEXT                 0000000104ca3000-0000000104ca4000 [    4K] r-x/rwx SM=COW  /Users/USER/*/*.out

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   helloworld.out                  0x0000000104ca3d79 main + 169
1   ???                             0x0073657261757173 0 + 32481164257489267

Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x0000000000000000  rbx: 0x0000000000000000  rcx: 0x0000000000000008  rdx: 0x0000000000000000
  rdi: 0x00007fff7c8e24e8  rsi: 0x00007fab53000000  rbp: 0x00007fff648a28e0  rsp: 0x00007fff648a28d8
   r8: 0x00000000fb343fb8   r9: 0x0000000000000000  r10: 0x0000000053000000  r11: 0x0000000000000246
  r12: 0x0000000000000000  r13: 0x0000000000000000  r14: 0x0000000000000000  r15: 0x0000000000000000
  rip: 0x0000000104ca3d79  rfl: 0x0000000000010202  cr2: 0x00007fab53001000
Logical CPU: 1

Binary Images:
       0x104ca3000 -        0x104ca3ff7 +helloworld.out (??? - ???) <68FD4206-E8D6-340F-B25B-E2DE6FB1D3BC> /Users/USER/*/helloworld.out
    0x7fff648a3000 -     0x7fff648d7ac7  dyld (195.5 - ???) <4A6E2B28-C7A2-3528-ADB7-4076B9836041> /usr/lib/dyld
    0x7fff8bce6000 -     0x7fff8bd06fff  libsystem_kernel.dylib (1699.23.2 - compatibility 1.0.0) <55377210-60A0-3F33-9D45-B2D19AA91859> /usr/lib/system/libsystem_kernel.dylib
    0x7fff8bdf4000 -     0x7fff8be02fff  libdispatch.dylib (187.5.0 - compatibility 1.0.0) <698F8EFB-7075-3111-94E3-891156C88172> /usr/lib/system/libdispatch.dylib
    0x7fff8c415000 -     0x7fff8c41cfff  libcopyfile.dylib (85.1.0 - compatibility 1.0.0) <65602684-33B1-32DE-802B-050CE07659AC> /usr/lib/system/libcopyfile.dylib
    0x7fff8cf65000 -     0x7fff8cfa7ff7  libcommonCrypto.dylib (55010.0.0 - compatibility 1.0.0) <A5B9778E-11C3-3F61-B740-1F2114E967FB> /usr/lib/system/libcommonCrypto.dylib
    0x7fff8cfcc000 -     0x7fff8cfccfff  libkeymgr.dylib (23.0.0 - compatibility 1.0.0) <61EFED6A-A407-301E-B454-CD18314F0075> /usr/lib/system/libkeymgr.dylib
    0x7fff8cfcd000 -     0x7fff8cfd1fff  libmathCommon.A.dylib (2026.0.0 - compatibility 1.0.0) <FF83AFF7-42B2-306E-90AF-D539C51A4542> /usr/lib/system/libmathCommon.A.dylib
    0x7fff8d001000 -     0x7fff8d006fff  libcompiler_rt.dylib (6.0.0 - compatibility 1.0.0) <98ECD5F6-E85C-32A5-98CD-8911230CB66A> /usr/lib/system/libcompiler_rt.dylib
    0x7fff8e5bb000 -     0x7fff8e5c3fff  libsystem_dnssd.dylib (??? - ???) <1FA8F861-9D0D-33D2-939F-5BD382718B97> /usr/lib/system/libsystem_dnssd.dylib
    0x7fff8e5d2000 -     0x7fff8e5d6fff  libdyld.dylib (195.5.0 - compatibility 1.0.0) <F1903B7A-D3FF-3390-909A-B24E09BAD1A5> /usr/lib/system/libdyld.dylib
    0x7fff8e67d000 -     0x7fff8e69aff7  libxpc.dylib (77.16.0 - compatibility 1.0.0) <1FEBB79D-8537-38A4-882D-1E2AE212CD31> /usr/lib/system/libxpc.dylib
    0x7fff8e999000 -     0x7fff8e99fff7  libunwind.dylib (30.0.0 - compatibility 1.0.0) <1E9C6C8C-CBE8-3F4B-A5B5-E03E3AB53231> /usr/lib/system/libunwind.dylib
    0x7fff8ebdd000 -     0x7fff8ebe8ff7  libc++abi.dylib (14.0.0 - compatibility 1.0.0) <8FF3D766-D678-36F6-84AC-423C878E6D14> /usr/lib/libc++abi.dylib
    0x7fff8f110000 -     0x7fff8f111fff  libsystem_sandbox.dylib (??? - ???) <8D14139B-B671-35F4-9E5A-023B4C523C38> /usr/lib/system/libsystem_sandbox.dylib
    0x7fff8f21e000 -     0x7fff8f21ffff  libunc.dylib (24.0.0 - compatibility 1.0.0) <0482C762-746D-37EB-A8C9-E1048CF70462> /usr/lib/system/libunc.dylib
    0x7fff8f5d0000 -     0x7fff8f5d1ff7  libsystem_blocks.dylib (53.0.0 - compatibility 1.0.0) <8BCA214A-8992-34B2-A8B9-B74DEACA1869> /usr/lib/system/libsystem_blocks.dylib
    0x7fff8feda000 -     0x7fff8ffb7fef  libsystem_c.dylib (763.11.0 - compatibility 1.0.0) <D48352AB-D1F1-3D8F-875C-2C807425CB8C> /usr/lib/system/libsystem_c.dylib
    0x7fff907e1000 -     0x7fff907e6fff  libcache.dylib (47.0.0 - compatibility 1.0.0) <3D114C8A-AD1F-3C78-9E8C-B8F3810740E5> /usr/lib/system/libcache.dylib
    0x7fff908ec000 -     0x7fff9095ffff  libstdc++.6.dylib (52.0.0 - compatibility 7.0.0) <6BDD43E4-A4B1-379E-9ED5-8C713653DFF2> /usr/lib/libstdc++.6.dylib
    0x7fff90e9b000 -     0x7fff90ec8fe7  libSystem.B.dylib (159.0.0 - compatibility 1.0.0) <B4654B67-7521-3CD2-A67D-E1393C459D23> /usr/lib/libSystem.B.dylib
    0x7fff9165c000 -     0x7fff9165dfff  libdnsinfo.dylib (395.7.0 - compatibility 1.0.0) <37FEFE78-BCB5-37EC-8E99-747469BCA4C7> /usr/lib/system/libdnsinfo.dylib
    0x7fff91704000 -     0x7fff91740fff  libsystem_info.dylib (??? - ???) <BC49C624-1DAB-3A37-890F-6EFD46538424> /usr/lib/system/libsystem_info.dylib
    0x7fff920e2000 -     0x7fff920e3ff7  libremovefile.dylib (21.0.0 - compatibility 1.0.0) <AACAD200-A346-3653-89E2-D9D10FE4DC06> /usr/lib/system/libremovefile.dylib
    0x7fff92227000 -     0x7fff92230fff  libnotify.dylib (80.0.0 - compatibility 1.0.0) <BD08553D-8088-38A8-8007-CF5C0B8F0404> /usr/lib/system/libnotify.dylib
    0x7fff92e3e000 -     0x7fff92e40fff  libquarantine.dylib (36.0.0 - compatibility 1.0.0) <A6B3CF07-A0D0-3C56-9BEC-33CBDD63CB89> /usr/lib/system/libquarantine.dylib
    0x7fff9385e000 -     0x7fff93864fff  libmacho.dylib (800.0.0 - compatibility 1.0.0) <548BAEB6-8C4C-3B0F-AB0C-7E1C960BCAB5> /usr/lib/system/libmacho.dylib
    0x7fff95371000 -     0x7fff9537bff7  liblaunch.dylib (392.18.0 - compatibility 1.0.0) <EB5C4B29-D3B7-38AC-A646-3D445C767F03> /usr/lib/system/liblaunch.dylib
    0x7fff9777d000 -     0x7fff97782ff7  libsystem_network.dylib (??? - ???) <4ABCEEF3-A3F9-3E06-9682-CE00F17138B7> /usr/lib/system/libsystem_network.dylib

External Modification Summary:
  Calls made by other processes targeting this process:
    task_for_pid: 0
    thread_create: 0
    thread_set_state: 0
  Calls made by this process:
    task_for_pid: 0
    thread_create: 0
    thread_set_state: 0
  Calls made by all processes on this machine:
    task_for_pid: 8407398
    thread_create: 1
    thread_set_state: 0

VM Region Summary:
ReadOnly portion of Libraries: Total=50.0M resident=14.0M(28%) swapped_out_or_unallocated=36.1M(72%)
Writable regions: Total=17.4M written=60K(0%) resident=104K(1%) swapped_out=0K(0%) unallocated=17.3M(99%)

REGION TYPE                      VIRTUAL
===========                      =======
MALLOC                             9396K
MALLOC guard page                    16K
STACK GUARD                        56.0M
Stack                              8192K
__DATA                              516K
__LINKEDIT                         47.1M
__TEXT                             3000K
shared memory                        12K
===========                      =======
TOTAL                             123.8M

Model: MacBookAir4,2, BootROM MBA41.0077.B00, 2 processors, Intel Core i7, 1.8 GHz, 4 GB, SMC 1.73f63
Graphics: Intel HD Graphics 3000, Intel HD Graphics 3000, Built-In, 384 MB
Memory Module: BANK 0/DIMM0, 2 GB, DDR3, 1333 MHz, 0x80AD, 0x484D54333235533642465238432D48392020
Memory Module: BANK 1/DIMM0, 2 GB, DDR3, 1333 MHz, 0x80AD, 0x484D54333235533642465238432D48392020
AirPort: spairport_wireless_card_type_airport_extreme (0x14E4, 0xE9), Broadcom BCM43xx 1.0 (5.100.98.75.10)
Bluetooth: Version 4.0.0f4, 2 service, 11 devices, 1 incoming serial ports
Serial ATA Device: APPLE SSD SM256C, 251 GB
USB Device: FaceTime Camera (Built-in), apple_vendor_id, 0x850a, 0xfa200000 / 3
USB Device: hub_device, 0x0424  (SMSC), 0x2513, 0xfa100000 / 2
USB Device: BRCM20702 Hub, 0x0a5c  (Broadcom Corp.), 0x4500, 0xfa110000 / 5
USB Device: Bluetooth USB Host Controller, apple_vendor_id, 0x821f, 0xfa113000 / 6
USB Device: Apple Internal Keyboard / Trackpad, apple_vendor_id, 0x024c, 0xfa120000 / 4
USB Device: hub_device, 0x0424  (SMSC), 0x2513, 0xfd100000 / 2
USB Device: Internal Memory Card Reader, apple_vendor_id, 0x8404, 0xfd110000 / 3
like image 306
keflavich Avatar asked Sep 16 '11 22:09

keflavich


People also ask

What causes a python segmentation fault?

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.

How do you fix a segmentation fault?

It can be resolved by having a base condition to return from the recursive function. A pointer must point to valid memory before accessing it.

Why is my program showing segmentation fault?

A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system).


2 Answers

From the stack trace, your Python interpreter is crashing in the 'atexit' handlers that the C library runs when a process exits. Specifically, it looks like the code that cleans up the "HDF5" library is hitting an error. I believe the crash comes about because the the Python runtime is mostly cleaned up and when the library tries to invoke some error handler, the handler is already gone.

Your C app is the "same" problem only in that it is also use of an uninitialized variable.

If Python always crashes with a very similar stack trace (Look for "Crashed Thread: X" in the dump report, then find the X thread's crashed backtrace below that in the dump), then there is something wrong with how HDF5 is being used. Perhaps this is a known bug in the Mac Python port? (I'm not really sure what the hdf5 library is.)

like image 178
P.T. Avatar answered Oct 29 '22 18:10

P.T.


Could also be this:

http://bugs.python.org/issue1856

fixed in new enough 3.2 and 3.3, not fixed in 2.x

like image 30
Dima Tisnek Avatar answered Oct 29 '22 19:10

Dima Tisnek